blob: 1d0b37af2444df6e3358dceb1e1c03522de4a731 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * NSA Security-Enhanced Linux (SELinux) security module
3 *
4 * This file contains the SELinux hook function implementations.
5 *
Stephen Smalley7efbb602017-08-17 13:32:36 -04006 * Authors: Stephen Smalley, <sds@tycho.nsa.gov>
Eric Paris828dfe12008-04-17 13:17:49 -04007 * Chris Vance, <cvance@nai.com>
8 * Wayne Salamon, <wsalamon@nai.com>
9 * James Morris <jmorris@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * Copyright (C) 2001,2002 Networks Associates Technology, Inc.
Eric Paris2069f452008-07-04 09:47:13 +100012 * Copyright (C) 2003-2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
13 * Eric Paris <eparis@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
Eric Paris828dfe12008-04-17 13:17:49 -040015 * <dgoeddel@trustedcs.com>
Paul Mooreed6d76e2009-08-28 18:12:49 -040016 * Copyright (C) 2006, 2007, 2009 Hewlett-Packard Development Company, L.P.
Paul Moore82c21bf2011-08-01 11:10:33 +000017 * Paul Moore <paul@paul-moore.com>
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +090018 * Copyright (C) 2007 Hitachi Software Engineering Co., Ltd.
Eric Paris828dfe12008-04-17 13:17:49 -040019 * Yuichi Nakamura <ynakam@hitachisoft.jp>
Daniel Jurgens3a976fa2017-05-19 15:48:56 +030020 * Copyright (C) 2016 Mellanox Technologies
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 *
22 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License version 2,
Eric Paris828dfe12008-04-17 13:17:49 -040024 * as published by the Free Software Foundation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 */
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/init.h>
Eric Paris0b24dcb2011-02-25 15:39:20 -050028#include <linux/kd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/kernel.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070030#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/errno.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010032#include <linux/sched/signal.h>
Ingo Molnar29930022017-02-08 18:51:36 +010033#include <linux/sched/task.h>
Casey Schaufler3c4ed7b2015-05-02 15:10:46 -070034#include <linux/lsm_hooks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/xattr.h>
36#include <linux/capability.h>
37#include <linux/unistd.h>
38#include <linux/mm.h>
39#include <linux/mman.h>
40#include <linux/slab.h>
41#include <linux/pagemap.h>
Eric Paris0b24dcb2011-02-25 15:39:20 -050042#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/spinlock.h>
45#include <linux/syscalls.h>
Eric Paris2a7dba32011-02-01 11:05:39 -050046#include <linux/dcache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040048#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/namei.h>
50#include <linux/mount.h>
David Howells442155c2018-11-01 23:07:24 +000051#include <linux/fs_context.h>
52#include <linux/fs_parser.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/netfilter_ipv4.h>
54#include <linux/netfilter_ipv6.h>
55#include <linux/tty.h>
56#include <net/icmp.h>
Stephen Hemminger227b60f2007-10-10 17:30:46 -070057#include <net/ip.h> /* for local_port_range[] */
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */
Paul Moore47180062013-12-04 16:10:45 -050059#include <net/inet_connection_sock.h>
Paul Moore220deb92008-01-29 08:38:23 -050060#include <net/net_namespace.h>
Paul Moored621d352008-01-29 08:43:36 -050061#include <net/netlabel.h>
Eric Parisf5269712008-05-14 11:27:45 -040062#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <asm/ioctls.h>
Arun Sharma600634972011-07-26 16:09:06 -070064#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/bitops.h>
66#include <linux/interrupt.h>
67#include <linux/netdevice.h> /* for network interface checks */
Hong zhi guo77954982013-03-27 06:49:35 +000068#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <linux/tcp.h>
70#include <linux/udp.h>
James Morris2ee92d42006-11-13 16:09:01 -080071#include <linux/dccp.h>
Richard Hainesd4529302018-02-13 20:57:18 +000072#include <linux/sctp.h>
73#include <net/sctp/structs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include <linux/quota.h>
75#include <linux/un.h> /* for Unix socket types */
76#include <net/af_unix.h> /* for Unix socket types */
77#include <linux/parser.h>
78#include <linux/nfs_mount.h>
79#include <net/ipv6.h>
80#include <linux/hugetlb.h>
81#include <linux/personality.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#include <linux/audit.h>
Eric Paris6931dfc2005-06-30 02:58:51 -070083#include <linux/string.h>
Eric Paris23970742006-09-25 23:32:01 -070084#include <linux/mutex.h>
Frank Mayharf06febc2008-09-12 09:54:39 -070085#include <linux/posix-timers.h>
Kees Cook00234592010-02-03 15:36:43 -080086#include <linux/syslog.h>
Serge E. Hallyn34867402011-03-23 16:43:17 -070087#include <linux/user_namespace.h>
Paul Gortmaker44fc7ea2011-05-26 20:52:10 -040088#include <linux/export.h>
Al Viro40401532012-02-13 03:58:52 +000089#include <linux/msg.h>
90#include <linux/shm.h>
Chenbo Fengec27c352017-10-18 13:00:25 -070091#include <linux/bpf.h>
David Howellse262e32d2018-11-01 23:07:23 +000092#include <uapi/linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94#include "avc.h"
95#include "objsec.h"
96#include "netif.h"
Paul Moore224dfbd2008-01-29 08:38:13 -050097#include "netnode.h"
Paul Moore3e1121722008-04-10 10:48:14 -040098#include "netport.h"
Daniel Jurgens409dcf32017-05-19 15:48:59 +030099#include "ibpkey.h"
Trent Jaegerd28d1e02005-12-13 23:12:40 -0800100#include "xfrm.h"
Paul Moorec60475b2007-02-28 15:14:23 -0500101#include "netlabel.h"
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +0200102#include "audit.h"
James Morris7b98a582011-08-30 12:52:32 +1000103#include "avc_ss.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500105struct selinux_state selinux_state;
106
Paul Moored621d352008-01-29 08:43:36 -0500107/* SECMARK reference count */
James Morris56a4ca92011-08-17 11:08:43 +1000108static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
Paul Moored621d352008-01-29 08:43:36 -0500109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500111static int selinux_enforcing_boot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113static int __init enforcing_setup(char *str)
114{
Eric Parisf5269712008-05-14 11:27:45 -0400115 unsigned long enforcing;
Jingoo Han29707b22014-02-05 15:13:14 +0900116 if (!kstrtoul(str, 0, &enforcing))
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500117 selinux_enforcing_boot = enforcing ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return 1;
119}
120__setup("enforcing=", enforcing_setup);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500121#else
122#define selinux_enforcing_boot 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123#endif
124
Kees Cookbe6ec882018-10-01 17:08:57 -0700125int selinux_enabled __lsm_ro_after_init = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126#ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127static int __init selinux_enabled_setup(char *str)
128{
Eric Parisf5269712008-05-14 11:27:45 -0400129 unsigned long enabled;
Jingoo Han29707b22014-02-05 15:13:14 +0900130 if (!kstrtoul(str, 0, &enabled))
Eric Parisf5269712008-05-14 11:27:45 -0400131 selinux_enabled = enabled ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return 1;
133}
134__setup("selinux=", selinux_enabled_setup);
135#endif
136
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500137static unsigned int selinux_checkreqprot_boot =
138 CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
139
140static int __init checkreqprot_setup(char *str)
141{
142 unsigned long checkreqprot;
143
144 if (!kstrtoul(str, 0, &checkreqprot))
145 selinux_checkreqprot_boot = checkreqprot ? 1 : 0;
146 return 1;
147}
148__setup("checkreqprot=", checkreqprot_setup);
149
Paul Moored621d352008-01-29 08:43:36 -0500150/**
151 * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
152 *
153 * Description:
154 * This function checks the SECMARK reference counter to see if any SECMARK
155 * targets are currently configured, if the reference counter is greater than
156 * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is
Chris PeBenito2be4d742013-05-03 09:05:39 -0400157 * enabled, false (0) if SECMARK is disabled. If the always_check_network
158 * policy capability is enabled, SECMARK is always considered enabled.
Paul Moored621d352008-01-29 08:43:36 -0500159 *
160 */
161static int selinux_secmark_enabled(void)
162{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500163 return (selinux_policycap_alwaysnetwork() ||
164 atomic_read(&selinux_secmark_refcount));
Chris PeBenito2be4d742013-05-03 09:05:39 -0400165}
166
167/**
168 * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled
169 *
170 * Description:
171 * This function checks if NetLabel or labeled IPSEC is enabled. Returns true
172 * (1) if any are enabled or false (0) if neither are enabled. If the
173 * always_check_network policy capability is enabled, peer labeling
174 * is always considered enabled.
175 *
176 */
177static int selinux_peerlbl_enabled(void)
178{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500179 return (selinux_policycap_alwaysnetwork() ||
180 netlbl_enabled() || selinux_xfrm_enabled());
Paul Moored621d352008-01-29 08:43:36 -0500181}
182
Paul Moore615e51f2014-06-26 14:33:56 -0400183static int selinux_netcache_avc_callback(u32 event)
184{
185 if (event == AVC_CALLBACK_RESET) {
186 sel_netif_flush();
187 sel_netnode_flush();
188 sel_netport_flush();
189 synchronize_net();
190 }
191 return 0;
192}
193
Daniel Jurgens8f408ab2017-05-19 15:48:53 +0300194static int selinux_lsm_notifier_avc_callback(u32 event)
195{
Daniel Jurgens409dcf32017-05-19 15:48:59 +0300196 if (event == AVC_CALLBACK_RESET) {
197 sel_ib_pkey_flush();
Daniel Jurgens8f408ab2017-05-19 15:48:53 +0300198 call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
Daniel Jurgens409dcf32017-05-19 15:48:59 +0300199 }
Daniel Jurgens8f408ab2017-05-19 15:48:53 +0300200
201 return 0;
202}
203
David Howellsd84f4f92008-11-14 10:39:23 +1100204/*
205 * initialise the security for the init task
206 */
207static void cred_init_security(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
David Howells3b11a1d2008-11-14 10:39:26 +1100209 struct cred *cred = (struct cred *) current->real_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 struct task_security_struct *tsec;
211
Casey Schauflerbbd36622018-11-12 09:30:56 -0800212 tsec = selinux_cred(cred);
David Howellsd84f4f92008-11-14 10:39:23 +1100213 tsec->osid = tsec->sid = SECINITSID_KERNEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
David Howells275bb412008-11-14 10:39:19 +1100216/*
David Howells88e67f32008-11-14 10:39:21 +1100217 * get the security ID of a set of credentials
218 */
219static inline u32 cred_sid(const struct cred *cred)
220{
221 const struct task_security_struct *tsec;
222
Casey Schaufler0c6cfa62018-09-21 17:17:16 -0700223 tsec = selinux_cred(cred);
David Howells88e67f32008-11-14 10:39:21 +1100224 return tsec->sid;
225}
226
227/*
David Howells3b11a1d2008-11-14 10:39:26 +1100228 * get the objective security ID of a task
David Howells275bb412008-11-14 10:39:19 +1100229 */
230static inline u32 task_sid(const struct task_struct *task)
231{
David Howells275bb412008-11-14 10:39:19 +1100232 u32 sid;
233
234 rcu_read_lock();
David Howells88e67f32008-11-14 10:39:21 +1100235 sid = cred_sid(__task_cred(task));
David Howells275bb412008-11-14 10:39:19 +1100236 rcu_read_unlock();
237 return sid;
238}
239
David Howells88e67f32008-11-14 10:39:21 +1100240/* Allocate and free functions for each kind of security blob. */
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242static int inode_alloc_security(struct inode *inode)
243{
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700244 struct inode_security_struct *isec = selinux_inode(inode);
David Howells275bb412008-11-14 10:39:19 +1100245 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +0100247 spin_lock_init(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 INIT_LIST_HEAD(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 isec->inode = inode;
250 isec->sid = SECINITSID_UNLABELED;
251 isec->sclass = SECCLASS_FILE;
David Howells275bb412008-11-14 10:39:19 +1100252 isec->task_sid = sid;
Andreas Gruenbacher42059112016-11-10 22:18:27 +0100253 isec->initialized = LABEL_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 return 0;
256}
257
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500258static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
259
260/*
261 * Try reloading inode security labels that have been marked as invalid. The
262 * @may_sleep parameter indicates when sleeping and thus reloading labels is
Andreas Gruenbacher42059112016-11-10 22:18:27 +0100263 * allowed; when set to false, returns -ECHILD when the label is
Al Viroe9193282018-04-24 21:31:02 -0400264 * invalid. The @dentry parameter should be set to a dentry of the inode.
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500265 */
266static int __inode_security_revalidate(struct inode *inode,
Al Viroe9193282018-04-24 21:31:02 -0400267 struct dentry *dentry,
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500268 bool may_sleep)
269{
Casey Schaufler80788c22018-09-21 17:19:11 -0700270 struct inode_security_struct *isec = selinux_inode(inode);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500271
272 might_sleep_if(may_sleep);
273
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500274 if (selinux_state.initialized &&
275 isec->initialized != LABEL_INITIALIZED) {
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500276 if (!may_sleep)
277 return -ECHILD;
278
279 /*
280 * Try reloading the inode security label. This will fail if
281 * @opt_dentry is NULL and no dentry for this inode can be
282 * found; in that case, continue using the old label.
283 */
Al Viroe9193282018-04-24 21:31:02 -0400284 inode_doinit_with_dentry(inode, dentry);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500285 }
286 return 0;
287}
288
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500289static struct inode_security_struct *inode_security_novalidate(struct inode *inode)
290{
Casey Schaufler80788c22018-09-21 17:19:11 -0700291 return selinux_inode(inode);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500292}
293
294static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu)
295{
296 int error;
297
298 error = __inode_security_revalidate(inode, NULL, !rcu);
299 if (error)
300 return ERR_PTR(error);
Casey Schaufler80788c22018-09-21 17:19:11 -0700301 return selinux_inode(inode);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500302}
303
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500304/*
305 * Get the security label of an inode.
306 */
307static struct inode_security_struct *inode_security(struct inode *inode)
308{
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500309 __inode_security_revalidate(inode, NULL, true);
Casey Schaufler80788c22018-09-21 17:19:11 -0700310 return selinux_inode(inode);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500311}
312
Paul Moore2c971652016-04-19 16:36:28 -0400313static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry)
314{
315 struct inode *inode = d_backing_inode(dentry);
316
Casey Schaufler80788c22018-09-21 17:19:11 -0700317 return selinux_inode(inode);
Paul Moore2c971652016-04-19 16:36:28 -0400318}
319
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500320/*
321 * Get the security label of a dentry's backing inode.
322 */
323static struct inode_security_struct *backing_inode_security(struct dentry *dentry)
324{
325 struct inode *inode = d_backing_inode(dentry);
326
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500327 __inode_security_revalidate(inode, dentry, true);
Casey Schaufler80788c22018-09-21 17:19:11 -0700328 return selinux_inode(inode);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500329}
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331static void inode_free_security(struct inode *inode)
332{
Casey Schaufler80788c22018-09-21 17:19:11 -0700333 struct inode_security_struct *isec = selinux_inode(inode);
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700334 struct superblock_security_struct *sbsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700336 if (!isec)
337 return;
338 sbsec = inode->i_sb->s_security;
Waiman Long9629d042015-07-10 17:19:56 -0400339 /*
340 * As not all inode security structures are in a list, we check for
341 * empty list outside of the lock to make sure that we won't waste
342 * time taking a lock doing nothing.
343 *
344 * The list_del_init() function can be safely called more than once.
345 * It should not be possible for this function to be called with
346 * concurrent list_add(), but for better safety against future changes
347 * in the code, we use list_empty_careful() here.
348 */
349 if (!list_empty_careful(&isec->list)) {
350 spin_lock(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 list_del_init(&isec->list);
Waiman Long9629d042015-07-10 17:19:56 -0400352 spin_unlock(&sbsec->isec_lock);
353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
356static int file_alloc_security(struct file *file)
357{
Casey Schaufler33bf60c2018-11-12 12:02:49 -0800358 struct file_security_struct *fsec = selinux_file(file);
David Howells275bb412008-11-14 10:39:19 +1100359 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
David Howells275bb412008-11-14 10:39:19 +1100361 fsec->sid = sid;
362 fsec->fown_sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 return 0;
365}
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367static int superblock_alloc_security(struct super_block *sb)
368{
369 struct superblock_security_struct *sbsec;
370
James Morris89d155e2005-10-30 14:59:21 -0800371 sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (!sbsec)
373 return -ENOMEM;
374
Eric Parisbc7e9822006-09-25 23:32:02 -0700375 mutex_init(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 INIT_LIST_HEAD(&sbsec->isec_head);
377 spin_lock_init(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 sbsec->sb = sb;
379 sbsec->sid = SECINITSID_UNLABELED;
380 sbsec->def_sid = SECINITSID_FILE;
Eric Parisc312feb2006-07-10 04:43:53 -0700381 sbsec->mntpoint_sid = SECINITSID_UNLABELED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 sb->s_security = sbsec;
383
384 return 0;
385}
386
387static void superblock_free_security(struct super_block *sb)
388{
389 struct superblock_security_struct *sbsec = sb->s_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 sb->s_security = NULL;
391 kfree(sbsec);
392}
393
Al Virobd323652018-12-13 15:04:59 -0500394struct selinux_mnt_opts {
395 const char *fscontext, *context, *rootcontext, *defcontext;
396};
397
Al Viro204cc0c2018-12-13 13:41:47 -0500398static void selinux_free_mnt_opts(void *mnt_opts)
399{
Al Virobd323652018-12-13 15:04:59 -0500400 struct selinux_mnt_opts *opts = mnt_opts;
401 kfree(opts->fscontext);
402 kfree(opts->context);
403 kfree(opts->rootcontext);
404 kfree(opts->defcontext);
Al Viro204cc0c2018-12-13 13:41:47 -0500405 kfree(opts);
406}
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408static inline int inode_doinit(struct inode *inode)
409{
410 return inode_doinit_with_dentry(inode, NULL);
411}
412
413enum {
Eric Paris31e87932007-09-19 17:19:12 -0400414 Opt_error = -1,
David Howells442155c2018-11-01 23:07:24 +0000415 Opt_context = 0,
416 Opt_defcontext = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 Opt_fscontext = 2,
David Howells442155c2018-11-01 23:07:24 +0000418 Opt_rootcontext = 3,
419 Opt_seclabel = 4,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420};
421
Al Viroda3d76a2018-12-17 10:14:16 -0500422#define A(s, has_arg) {#s, sizeof(#s) - 1, Opt_##s, has_arg}
Al Viro169d68efb2018-12-14 22:44:50 -0500423static struct {
424 const char *name;
425 int len;
426 int opt;
427 bool has_arg;
428} tokens[] = {
Al Viroda3d76a2018-12-17 10:14:16 -0500429 A(context, true),
430 A(fscontext, true),
431 A(defcontext, true),
432 A(rootcontext, true),
433 A(seclabel, false),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434};
Al Viro169d68efb2018-12-14 22:44:50 -0500435#undef A
436
437static int match_opt_prefix(char *s, int l, char **arg)
438{
439 int i;
440
441 for (i = 0; i < ARRAY_SIZE(tokens); i++) {
442 size_t len = tokens[i].len;
443 if (len > l || memcmp(s, tokens[i].name, len))
444 continue;
445 if (tokens[i].has_arg) {
446 if (len == l || s[len] != '=')
447 continue;
448 *arg = s + len + 1;
449 } else if (len != l)
450 continue;
451 return tokens[i].opt;
452 }
453 return Opt_error;
454}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456#define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
457
Eric Parisc312feb2006-07-10 04:43:53 -0700458static int may_context_mount_sb_relabel(u32 sid,
459 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100460 const struct cred *cred)
Eric Parisc312feb2006-07-10 04:43:53 -0700461{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -0700462 const struct task_security_struct *tsec = selinux_cred(cred);
Eric Parisc312feb2006-07-10 04:43:53 -0700463 int rc;
464
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500465 rc = avc_has_perm(&selinux_state,
466 tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Parisc312feb2006-07-10 04:43:53 -0700467 FILESYSTEM__RELABELFROM, NULL);
468 if (rc)
469 return rc;
470
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500471 rc = avc_has_perm(&selinux_state,
472 tsec->sid, sid, SECCLASS_FILESYSTEM,
Eric Parisc312feb2006-07-10 04:43:53 -0700473 FILESYSTEM__RELABELTO, NULL);
474 return rc;
475}
476
Eric Paris08089252006-07-10 04:43:55 -0700477static int may_context_mount_inode_relabel(u32 sid,
478 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100479 const struct cred *cred)
Eric Paris08089252006-07-10 04:43:55 -0700480{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -0700481 const struct task_security_struct *tsec = selinux_cred(cred);
Eric Paris08089252006-07-10 04:43:55 -0700482 int rc;
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500483 rc = avc_has_perm(&selinux_state,
484 tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Paris08089252006-07-10 04:43:55 -0700485 FILESYSTEM__RELABELFROM, NULL);
486 if (rc)
487 return rc;
488
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500489 rc = avc_has_perm(&selinux_state,
490 sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Paris08089252006-07-10 04:43:55 -0700491 FILESYSTEM__ASSOCIATE, NULL);
492 return rc;
493}
494
Ondrej Mosnaceka83d6dd2018-12-21 21:18:52 +0100495static int selinux_is_genfs_special_handling(struct super_block *sb)
Eric Parisb43e7252012-10-10 14:27:35 -0400496{
Ondrej Mosnaceka83d6dd2018-12-21 21:18:52 +0100497 /* Special handling. Genfs but also in-core setxattr handler */
498 return !strcmp(sb->s_type->name, "sysfs") ||
Mark Salyzynd5f3a5f2015-02-04 11:34:30 -0500499 !strcmp(sb->s_type->name, "pstore") ||
500 !strcmp(sb->s_type->name, "debugfs") ||
Yongqin Liua2c7c6f2017-01-09 10:07:30 -0500501 !strcmp(sb->s_type->name, "tracefs") ||
Stephen Smalley2651225b2017-02-28 10:35:56 -0500502 !strcmp(sb->s_type->name, "rootfs") ||
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500503 (selinux_policycap_cgroupseclabel() &&
Stephen Smalley2651225b2017-02-28 10:35:56 -0500504 (!strcmp(sb->s_type->name, "cgroup") ||
505 !strcmp(sb->s_type->name, "cgroup2")));
Eric Parisb43e7252012-10-10 14:27:35 -0400506}
507
Ondrej Mosnaceka83d6dd2018-12-21 21:18:52 +0100508static int selinux_is_sblabel_mnt(struct super_block *sb)
509{
510 struct superblock_security_struct *sbsec = sb->s_security;
511
512 /*
513 * IMPORTANT: Double-check logic in this function when adding a new
514 * SECURITY_FS_USE_* definition!
515 */
516 BUILD_BUG_ON(SECURITY_FS_USE_MAX != 7);
517
518 switch (sbsec->behavior) {
519 case SECURITY_FS_USE_XATTR:
520 case SECURITY_FS_USE_TRANS:
521 case SECURITY_FS_USE_TASK:
522 case SECURITY_FS_USE_NATIVE:
523 return 1;
524
525 case SECURITY_FS_USE_GENFS:
526 return selinux_is_genfs_special_handling(sb);
527
528 /* Never allow relabeling on context mounts */
529 case SECURITY_FS_USE_MNTPOINT:
530 case SECURITY_FS_USE_NONE:
531 default:
532 return 0;
533 }
534}
535
Eric Parisc9180a52007-11-30 13:00:35 -0500536static int sb_finish_set_opts(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
538 struct superblock_security_struct *sbsec = sb->s_security;
539 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000540 struct inode *root_inode = d_backing_inode(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 int rc = 0;
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
544 /* Make sure that the xattr handler exists and that no
545 error other than -ENODATA is returned by getxattr on
546 the root directory. -ENODATA is ok, as this may be
547 the first boot of the SELinux kernel before we have
548 assigned xattr values to the filesystem. */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200549 if (!(root_inode->i_opflags & IOP_XATTR)) {
peter enderborgc103a912018-06-12 10:09:03 +0200550 pr_warn("SELinux: (dev %s, type %s) has no "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800551 "xattr support\n", sb->s_id, sb->s_type->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 rc = -EOPNOTSUPP;
553 goto out;
554 }
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200555
556 rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (rc < 0 && rc != -ENODATA) {
558 if (rc == -EOPNOTSUPP)
peter enderborgc103a912018-06-12 10:09:03 +0200559 pr_warn("SELinux: (dev %s, type "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800560 "%s) has no security xattr handler\n",
561 sb->s_id, sb->s_type->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 else
peter enderborgc103a912018-06-12 10:09:03 +0200563 pr_warn("SELinux: (dev %s, type "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800564 "%s) getxattr errno %d\n", sb->s_id,
565 sb->s_type->name, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 goto out;
567 }
568 }
569
Eric Pariseadcabc2012-08-24 15:59:14 -0400570 sbsec->flags |= SE_SBINITIALIZED;
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400571
572 /*
573 * Explicitly set or clear SBLABEL_MNT. It's not sufficient to simply
574 * leave the flag untouched because sb_clone_mnt_opts might be handing
575 * us a superblock that needs the flag to be cleared.
576 */
Eric Parisb43e7252012-10-10 14:27:35 -0400577 if (selinux_is_sblabel_mnt(sb))
Eric Paris12f348b2012-10-09 10:56:25 -0400578 sbsec->flags |= SBLABEL_MNT;
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400579 else
580 sbsec->flags &= ~SBLABEL_MNT;
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 /* Initialize the root inode. */
Eric Parisc9180a52007-11-30 13:00:35 -0500583 rc = inode_doinit_with_dentry(root_inode, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 /* Initialize any other inodes associated with the superblock, e.g.
586 inodes created prior to initial policy load or inodes created
587 during get_sb by a pseudo filesystem that directly
588 populates itself. */
589 spin_lock(&sbsec->isec_lock);
Al Viro8d641242018-12-10 15:34:12 -0500590 while (!list_empty(&sbsec->isec_head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 struct inode_security_struct *isec =
Al Viro8d641242018-12-10 15:34:12 -0500592 list_first_entry(&sbsec->isec_head,
Eric Parisc9180a52007-11-30 13:00:35 -0500593 struct inode_security_struct, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 struct inode *inode = isec->inode;
Stephen Smalley923190d2014-10-06 16:32:52 -0400595 list_del_init(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 spin_unlock(&sbsec->isec_lock);
597 inode = igrab(inode);
598 if (inode) {
Eric Parisc9180a52007-11-30 13:00:35 -0500599 if (!IS_PRIVATE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 inode_doinit(inode);
601 iput(inode);
602 }
603 spin_lock(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
605 spin_unlock(&sbsec->isec_lock);
606out:
Eric Parisc9180a52007-11-30 13:00:35 -0500607 return rc;
608}
609
Eric Parisc9180a52007-11-30 13:00:35 -0500610static int bad_option(struct superblock_security_struct *sbsec, char flag,
611 u32 old_sid, u32 new_sid)
612{
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500613 char mnt_flags = sbsec->flags & SE_MNTMASK;
614
Eric Parisc9180a52007-11-30 13:00:35 -0500615 /* check if the old mount command had the same options */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500616 if (sbsec->flags & SE_SBINITIALIZED)
Eric Parisc9180a52007-11-30 13:00:35 -0500617 if (!(sbsec->flags & flag) ||
618 (old_sid != new_sid))
619 return 1;
620
621 /* check if we were passed the same options twice,
622 * aka someone passed context=a,context=b
623 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500624 if (!(sbsec->flags & SE_SBINITIALIZED))
625 if (mnt_flags & flag)
Eric Parisc9180a52007-11-30 13:00:35 -0500626 return 1;
627 return 0;
628}
Eric Parise0007522008-03-05 10:31:54 -0500629
Al Virobd323652018-12-13 15:04:59 -0500630static int parse_sid(struct super_block *sb, const char *s, u32 *sid)
631{
632 int rc = security_context_str_to_sid(&selinux_state, s,
633 sid, GFP_KERNEL);
634 if (rc)
635 pr_warn("SELinux: security_context_str_to_sid"
636 "(%s) failed for (dev %s, type %s) errno=%d\n",
637 s, sb->s_id, sb->s_type->name, rc);
638 return rc;
639}
640
Eric Parisc9180a52007-11-30 13:00:35 -0500641/*
642 * Allow filesystems with binary mount data to explicitly set mount point
643 * labeling information.
644 */
Eric Parise0007522008-03-05 10:31:54 -0500645static int selinux_set_mnt_opts(struct super_block *sb,
Al Viro204cc0c2018-12-13 13:41:47 -0500646 void *mnt_opts,
David Quigley649f6e72013-05-22 12:50:36 -0400647 unsigned long kern_flags,
648 unsigned long *set_kern_flags)
Eric Parisc9180a52007-11-30 13:00:35 -0500649{
David Howells275bb412008-11-14 10:39:19 +1100650 const struct cred *cred = current_cred();
Eric Parisc9180a52007-11-30 13:00:35 -0500651 struct superblock_security_struct *sbsec = sb->s_security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500652 struct dentry *root = sbsec->sb->s_root;
Al Virobd323652018-12-13 15:04:59 -0500653 struct selinux_mnt_opts *opts = mnt_opts;
Paul Moore2c971652016-04-19 16:36:28 -0400654 struct inode_security_struct *root_isec;
Eric Parisc9180a52007-11-30 13:00:35 -0500655 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
656 u32 defcontext_sid = 0;
Al Virobd323652018-12-13 15:04:59 -0500657 int rc = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500658
659 mutex_lock(&sbsec->lock);
660
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500661 if (!selinux_state.initialized) {
Al Virobd323652018-12-13 15:04:59 -0500662 if (!opts) {
Eric Parisc9180a52007-11-30 13:00:35 -0500663 /* Defer initialization until selinux_complete_init,
664 after the initial policy is loaded and the security
665 server is ready to handle calls. */
Eric Parisc9180a52007-11-30 13:00:35 -0500666 goto out;
667 }
668 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200669 pr_warn("SELinux: Unable to set superblock options "
Eric Paris744ba352008-04-17 11:52:44 -0400670 "before the security server is initialized\n");
Eric Parisc9180a52007-11-30 13:00:35 -0500671 goto out;
672 }
David Quigley649f6e72013-05-22 12:50:36 -0400673 if (kern_flags && !set_kern_flags) {
674 /* Specifying internal flags without providing a place to
675 * place the results is not allowed */
676 rc = -EINVAL;
677 goto out;
678 }
Eric Parisc9180a52007-11-30 13:00:35 -0500679
680 /*
Eric Parise0007522008-03-05 10:31:54 -0500681 * Binary mount data FS will come through this function twice. Once
682 * from an explicit call and once from the generic calls from the vfs.
683 * Since the generic VFS calls will not contain any security mount data
684 * we need to skip the double mount verification.
685 *
686 * This does open a hole in which we will not notice if the first
687 * mount using this sb set explict options and a second mount using
688 * this sb does not set any security options. (The first options
689 * will be used for both mounts)
690 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500691 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
Al Virobd323652018-12-13 15:04:59 -0500692 && !opts)
Eric Parisf5269712008-05-14 11:27:45 -0400693 goto out;
Eric Parise0007522008-03-05 10:31:54 -0500694
Paul Moore2c971652016-04-19 16:36:28 -0400695 root_isec = backing_inode_security_novalidate(root);
696
Eric Parise0007522008-03-05 10:31:54 -0500697 /*
Eric Parisc9180a52007-11-30 13:00:35 -0500698 * parse the mount options, check if they are valid sids.
699 * also check if someone is trying to mount the same sb more
700 * than once with different security options.
701 */
Al Virobd323652018-12-13 15:04:59 -0500702 if (opts) {
703 if (opts->fscontext) {
704 rc = parse_sid(sb, opts->fscontext, &fscontext_sid);
705 if (rc)
706 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500707 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
708 fscontext_sid))
709 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500710 sbsec->flags |= FSCONTEXT_MNT;
Al Virobd323652018-12-13 15:04:59 -0500711 }
712 if (opts->context) {
713 rc = parse_sid(sb, opts->context, &context_sid);
714 if (rc)
715 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500716 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
717 context_sid))
718 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500719 sbsec->flags |= CONTEXT_MNT;
Al Virobd323652018-12-13 15:04:59 -0500720 }
721 if (opts->rootcontext) {
722 rc = parse_sid(sb, opts->rootcontext, &rootcontext_sid);
723 if (rc)
724 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500725 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
726 rootcontext_sid))
727 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500728 sbsec->flags |= ROOTCONTEXT_MNT;
Al Virobd323652018-12-13 15:04:59 -0500729 }
730 if (opts->defcontext) {
731 rc = parse_sid(sb, opts->defcontext, &defcontext_sid);
732 if (rc)
733 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500734 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
735 defcontext_sid))
736 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500737 sbsec->flags |= DEFCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500738 }
739 }
740
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500741 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Parisc9180a52007-11-30 13:00:35 -0500742 /* previously mounted with options, but not on this attempt? */
Al Virobd323652018-12-13 15:04:59 -0500743 if ((sbsec->flags & SE_MNTMASK) && !opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500744 goto out_double_mount;
745 rc = 0;
746 goto out;
747 }
748
James Morris089be432008-07-15 18:32:49 +1000749 if (strcmp(sb->s_type->name, "proc") == 0)
Stephen Smalley134509d2015-06-04 16:22:17 -0400750 sbsec->flags |= SE_SBPROC | SE_SBGENFS;
751
Stephen Smalley8e014722015-06-04 16:22:17 -0400752 if (!strcmp(sb->s_type->name, "debugfs") ||
Jeff Vander Stoep6a391182017-06-20 09:35:33 -0700753 !strcmp(sb->s_type->name, "tracefs") ||
Stephen Smalley8e014722015-06-04 16:22:17 -0400754 !strcmp(sb->s_type->name, "sysfs") ||
Antonio Murdaca901ef842017-02-09 17:02:42 +0100755 !strcmp(sb->s_type->name, "pstore") ||
756 !strcmp(sb->s_type->name, "cgroup") ||
757 !strcmp(sb->s_type->name, "cgroup2"))
Stephen Smalley134509d2015-06-04 16:22:17 -0400758 sbsec->flags |= SE_SBGENFS;
Eric Parisc9180a52007-11-30 13:00:35 -0500759
David Quigleyeb9ae682013-05-22 12:50:37 -0400760 if (!sbsec->behavior) {
761 /*
762 * Determine the labeling behavior to use for this
763 * filesystem type.
764 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500765 rc = security_fs_use(&selinux_state, sb);
David Quigleyeb9ae682013-05-22 12:50:37 -0400766 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +0200767 pr_warn("%s: security_fs_use(%s) returned %d\n",
David Quigleyeb9ae682013-05-22 12:50:37 -0400768 __func__, sb->s_type->name, rc);
769 goto out;
770 }
Eric Parisc9180a52007-11-30 13:00:35 -0500771 }
Seth Forsheeaad82892016-04-26 14:36:20 -0500772
773 /*
Stephen Smalley01593d32017-01-09 10:07:31 -0500774 * If this is a user namespace mount and the filesystem type is not
775 * explicitly whitelisted, then no contexts are allowed on the command
776 * line and security labels must be ignored.
Seth Forsheeaad82892016-04-26 14:36:20 -0500777 */
Stephen Smalley01593d32017-01-09 10:07:31 -0500778 if (sb->s_user_ns != &init_user_ns &&
779 strcmp(sb->s_type->name, "tmpfs") &&
780 strcmp(sb->s_type->name, "ramfs") &&
781 strcmp(sb->s_type->name, "devpts")) {
Seth Forsheeaad82892016-04-26 14:36:20 -0500782 if (context_sid || fscontext_sid || rootcontext_sid ||
783 defcontext_sid) {
784 rc = -EACCES;
785 goto out;
786 }
787 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
788 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500789 rc = security_transition_sid(&selinux_state,
790 current_sid(),
791 current_sid(),
Seth Forsheeaad82892016-04-26 14:36:20 -0500792 SECCLASS_FILE, NULL,
793 &sbsec->mntpoint_sid);
794 if (rc)
795 goto out;
796 }
797 goto out_set_opts;
798 }
799
Eric Parisc9180a52007-11-30 13:00:35 -0500800 /* sets the context of the superblock for the fs being mounted. */
801 if (fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100802 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500803 if (rc)
804 goto out;
805
806 sbsec->sid = fscontext_sid;
807 }
808
809 /*
810 * Switch to using mount point labeling behavior.
811 * sets the label used on all file below the mountpoint, and will set
812 * the superblock context if not already set.
813 */
David Quigleyeb9ae682013-05-22 12:50:37 -0400814 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
815 sbsec->behavior = SECURITY_FS_USE_NATIVE;
816 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
817 }
818
Eric Parisc9180a52007-11-30 13:00:35 -0500819 if (context_sid) {
820 if (!fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100821 rc = may_context_mount_sb_relabel(context_sid, sbsec,
822 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500823 if (rc)
824 goto out;
825 sbsec->sid = context_sid;
826 } else {
David Howells275bb412008-11-14 10:39:19 +1100827 rc = may_context_mount_inode_relabel(context_sid, sbsec,
828 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500829 if (rc)
830 goto out;
831 }
832 if (!rootcontext_sid)
833 rootcontext_sid = context_sid;
834
835 sbsec->mntpoint_sid = context_sid;
836 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
837 }
838
839 if (rootcontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100840 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
841 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500842 if (rc)
843 goto out;
844
845 root_isec->sid = rootcontext_sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -0500846 root_isec->initialized = LABEL_INITIALIZED;
Eric Parisc9180a52007-11-30 13:00:35 -0500847 }
848
849 if (defcontext_sid) {
David Quigleyeb9ae682013-05-22 12:50:37 -0400850 if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
851 sbsec->behavior != SECURITY_FS_USE_NATIVE) {
Eric Parisc9180a52007-11-30 13:00:35 -0500852 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200853 pr_warn("SELinux: defcontext option is "
Eric Parisc9180a52007-11-30 13:00:35 -0500854 "invalid for this filesystem type\n");
855 goto out;
856 }
857
858 if (defcontext_sid != sbsec->def_sid) {
859 rc = may_context_mount_inode_relabel(defcontext_sid,
David Howells275bb412008-11-14 10:39:19 +1100860 sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500861 if (rc)
862 goto out;
863 }
864
865 sbsec->def_sid = defcontext_sid;
866 }
867
Seth Forsheeaad82892016-04-26 14:36:20 -0500868out_set_opts:
Eric Parisc9180a52007-11-30 13:00:35 -0500869 rc = sb_finish_set_opts(sb);
870out:
Eric Parisbc7e9822006-09-25 23:32:02 -0700871 mutex_unlock(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500873out_double_mount:
874 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200875 pr_warn("SELinux: mount invalid. Same superblock, different "
Al Virobd323652018-12-13 15:04:59 -0500876 "security settings for (dev %s, type %s)\n", sb->s_id,
877 sb->s_type->name);
Eric Parisc9180a52007-11-30 13:00:35 -0500878 goto out;
879}
880
Jeff Layton094f7b62013-04-01 08:14:24 -0400881static int selinux_cmp_sb_context(const struct super_block *oldsb,
882 const struct super_block *newsb)
883{
884 struct superblock_security_struct *old = oldsb->s_security;
885 struct superblock_security_struct *new = newsb->s_security;
886 char oldflags = old->flags & SE_MNTMASK;
887 char newflags = new->flags & SE_MNTMASK;
888
889 if (oldflags != newflags)
890 goto mismatch;
891 if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
892 goto mismatch;
893 if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
894 goto mismatch;
895 if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
896 goto mismatch;
897 if (oldflags & ROOTCONTEXT_MNT) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500898 struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root);
899 struct inode_security_struct *newroot = backing_inode_security(newsb->s_root);
Jeff Layton094f7b62013-04-01 08:14:24 -0400900 if (oldroot->sid != newroot->sid)
901 goto mismatch;
902 }
903 return 0;
904mismatch:
peter enderborgc103a912018-06-12 10:09:03 +0200905 pr_warn("SELinux: mount invalid. Same superblock, "
Jeff Layton094f7b62013-04-01 08:14:24 -0400906 "different security settings for (dev %s, "
907 "type %s)\n", newsb->s_id, newsb->s_type->name);
908 return -EBUSY;
909}
910
911static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400912 struct super_block *newsb,
913 unsigned long kern_flags,
914 unsigned long *set_kern_flags)
Eric Parisc9180a52007-11-30 13:00:35 -0500915{
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400916 int rc = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500917 const struct superblock_security_struct *oldsbsec = oldsb->s_security;
918 struct superblock_security_struct *newsbsec = newsb->s_security;
919
920 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
921 int set_context = (oldsbsec->flags & CONTEXT_MNT);
922 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
923
Eric Paris0f5e6422008-04-21 16:24:11 -0400924 /*
925 * if the parent was able to be mounted it clearly had no special lsm
Al Viroe8c26252010-03-23 06:36:54 -0400926 * mount options. thus we can safely deal with this superblock later
Eric Paris0f5e6422008-04-21 16:24:11 -0400927 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500928 if (!selinux_state.initialized)
Jeff Layton094f7b62013-04-01 08:14:24 -0400929 return 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500930
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400931 /*
932 * Specifying internal flags without providing a place to
933 * place the results is not allowed.
934 */
935 if (kern_flags && !set_kern_flags)
936 return -EINVAL;
937
Eric Parisc9180a52007-11-30 13:00:35 -0500938 /* how can we clone if the old one wasn't set up?? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500939 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
Eric Parisc9180a52007-11-30 13:00:35 -0500940
Jeff Layton094f7b62013-04-01 08:14:24 -0400941 /* if fs is reusing a sb, make sure that the contexts match */
J. Bruce Fields3815a242019-03-05 16:17:58 -0500942 if (newsbsec->flags & SE_SBINITIALIZED) {
943 if ((kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context)
944 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
Jeff Layton094f7b62013-04-01 08:14:24 -0400945 return selinux_cmp_sb_context(oldsb, newsb);
J. Bruce Fields3815a242019-03-05 16:17:58 -0500946 }
Eric Paris5a552612008-04-09 14:08:35 -0400947
Eric Parisc9180a52007-11-30 13:00:35 -0500948 mutex_lock(&newsbsec->lock);
949
950 newsbsec->flags = oldsbsec->flags;
951
952 newsbsec->sid = oldsbsec->sid;
953 newsbsec->def_sid = oldsbsec->def_sid;
954 newsbsec->behavior = oldsbsec->behavior;
955
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400956 if (newsbsec->behavior == SECURITY_FS_USE_NATIVE &&
957 !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500958 rc = security_fs_use(&selinux_state, newsb);
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400959 if (rc)
960 goto out;
961 }
962
963 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) {
964 newsbsec->behavior = SECURITY_FS_USE_NATIVE;
965 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
966 }
967
Eric Parisc9180a52007-11-30 13:00:35 -0500968 if (set_context) {
969 u32 sid = oldsbsec->mntpoint_sid;
970
971 if (!set_fscontext)
972 newsbsec->sid = sid;
973 if (!set_rootcontext) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500974 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
Eric Parisc9180a52007-11-30 13:00:35 -0500975 newisec->sid = sid;
976 }
977 newsbsec->mntpoint_sid = sid;
978 }
979 if (set_rootcontext) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500980 const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root);
981 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
Eric Parisc9180a52007-11-30 13:00:35 -0500982
983 newisec->sid = oldisec->sid;
984 }
985
986 sb_finish_set_opts(newsb);
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400987out:
Eric Parisc9180a52007-11-30 13:00:35 -0500988 mutex_unlock(&newsbsec->lock);
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400989 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500990}
991
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;
Eric Parisc9180a52007-11-30 13:00:35 -0500995
Al Viroda3d76a2018-12-17 10:14:16 -0500996 if (token == Opt_seclabel) /* eaten and completely ignored */
Al Viro169d68efb2018-12-14 22:44:50 -0500997 return 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500998
Al Viroba641862018-12-14 20:28:15 -0500999 if (!opts) {
1000 opts = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL);
1001 if (!opts)
1002 return -ENOMEM;
1003 *mnt_opts = opts;
1004 }
1005 if (!s)
1006 return -ENOMEM;
1007 switch (token) {
1008 case Opt_context:
1009 if (opts->context || opts->defcontext)
1010 goto Einval;
1011 opts->context = s;
1012 break;
1013 case Opt_fscontext:
1014 if (opts->fscontext)
1015 goto Einval;
1016 opts->fscontext = s;
1017 break;
1018 case Opt_rootcontext:
1019 if (opts->rootcontext)
1020 goto Einval;
1021 opts->rootcontext = s;
1022 break;
1023 case Opt_defcontext:
1024 if (opts->context || opts->defcontext)
1025 goto Einval;
1026 opts->defcontext = s;
1027 break;
1028 }
1029 return 0;
1030Einval:
1031 pr_warn(SEL_MOUNT_FAIL_MSG);
Al Viroba641862018-12-14 20:28:15 -05001032 return -EINVAL;
1033}
Eric Parisc9180a52007-11-30 13:00:35 -05001034
Al Viro757cbe52018-12-14 23:42:21 -05001035static int selinux_add_mnt_opt(const char *option, const char *val, int len,
1036 void **mnt_opts)
Eric Parisc9180a52007-11-30 13:00:35 -05001037{
Al Viro757cbe52018-12-14 23:42:21 -05001038 int token = Opt_error;
1039 int rc, i;
Eric Parisc9180a52007-11-30 13:00:35 -05001040
Al Viro757cbe52018-12-14 23:42:21 -05001041 for (i = 0; i < ARRAY_SIZE(tokens); i++) {
1042 if (strcmp(option, tokens[i].name) == 0) {
1043 token = tokens[i].opt;
Eric Parisc9180a52007-11-30 13:00:35 -05001044 break;
Eric Parisc9180a52007-11-30 13:00:35 -05001045 }
1046 }
1047
Al Viro757cbe52018-12-14 23:42:21 -05001048 if (token == Opt_error)
1049 return -EINVAL;
Eric Parise0007522008-03-05 10:31:54 -05001050
Al Viro757cbe52018-12-14 23:42:21 -05001051 if (token != Opt_seclabel)
1052 val = kmemdup_nul(val, len, GFP_KERNEL);
1053 rc = selinux_add_opt(token, val, mnt_opts);
1054 if (unlikely(rc)) {
1055 kfree(val);
1056 if (*mnt_opts) {
1057 selinux_free_mnt_opts(*mnt_opts);
1058 *mnt_opts = NULL;
1059 }
1060 }
1061 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Al Viroe3489f82018-12-13 00:24:36 -05001064static int show_sid(struct seq_file *m, u32 sid)
Eric Paris2069f452008-07-04 09:47:13 +10001065{
Al Viroe3489f82018-12-13 00:24:36 -05001066 char *context = NULL;
1067 u32 len;
1068 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Al Viroe3489f82018-12-13 00:24:36 -05001070 rc = security_sid_to_context(&selinux_state, sid,
1071 &context, &len);
1072 if (!rc) {
1073 bool has_comma = context && strchr(context, ',');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
David Howells442155c2018-11-01 23:07:24 +00001075 seq_putc(m, '=');
Eric Paris2069f452008-07-04 09:47:13 +10001076 if (has_comma)
1077 seq_putc(m, '\"');
Al Viroe3489f82018-12-13 00:24:36 -05001078 seq_escape(m, context, "\"\n\\");
Eric Paris2069f452008-07-04 09:47:13 +10001079 if (has_comma)
1080 seq_putc(m, '\"');
1081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 kfree(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 return rc;
1084}
Eric Paris2069f452008-07-04 09:47:13 +10001085
1086static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1087{
Al Viroe3489f82018-12-13 00:24:36 -05001088 struct superblock_security_struct *sbsec = sb->s_security;
Eric Paris2069f452008-07-04 09:47:13 +10001089 int rc;
1090
Al Viroe3489f82018-12-13 00:24:36 -05001091 if (!(sbsec->flags & SE_SBINITIALIZED))
1092 return 0;
1093
1094 if (!selinux_state.initialized)
1095 return 0;
1096
1097 if (sbsec->flags & FSCONTEXT_MNT) {
1098 seq_putc(m, ',');
1099 seq_puts(m, FSCONTEXT_STR);
1100 rc = show_sid(m, sbsec->sid);
1101 if (rc)
1102 return rc;
Eric Paris383795c2008-07-29 17:07:26 -04001103 }
Al Viroe3489f82018-12-13 00:24:36 -05001104 if (sbsec->flags & CONTEXT_MNT) {
1105 seq_putc(m, ',');
1106 seq_puts(m, CONTEXT_STR);
1107 rc = show_sid(m, sbsec->mntpoint_sid);
1108 if (rc)
1109 return rc;
1110 }
1111 if (sbsec->flags & DEFCONTEXT_MNT) {
1112 seq_putc(m, ',');
1113 seq_puts(m, DEFCONTEXT_STR);
1114 rc = show_sid(m, sbsec->def_sid);
1115 if (rc)
1116 return rc;
1117 }
1118 if (sbsec->flags & ROOTCONTEXT_MNT) {
1119 struct dentry *root = sbsec->sb->s_root;
1120 struct inode_security_struct *isec = backing_inode_security(root);
1121 seq_putc(m, ',');
1122 seq_puts(m, ROOTCONTEXT_STR);
1123 rc = show_sid(m, isec->sid);
1124 if (rc)
1125 return rc;
1126 }
1127 if (sbsec->flags & SBLABEL_MNT) {
1128 seq_putc(m, ',');
David Howells442155c2018-11-01 23:07:24 +00001129 seq_puts(m, SECLABEL_STR);
Al Viroe3489f82018-12-13 00:24:36 -05001130 }
1131 return 0;
Eric Paris2069f452008-07-04 09:47:13 +10001132}
1133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134static inline u16 inode_mode_to_security_class(umode_t mode)
1135{
1136 switch (mode & S_IFMT) {
1137 case S_IFSOCK:
1138 return SECCLASS_SOCK_FILE;
1139 case S_IFLNK:
1140 return SECCLASS_LNK_FILE;
1141 case S_IFREG:
1142 return SECCLASS_FILE;
1143 case S_IFBLK:
1144 return SECCLASS_BLK_FILE;
1145 case S_IFDIR:
1146 return SECCLASS_DIR;
1147 case S_IFCHR:
1148 return SECCLASS_CHR_FILE;
1149 case S_IFIFO:
1150 return SECCLASS_FIFO_FILE;
1151
1152 }
1153
1154 return SECCLASS_FILE;
1155}
1156
James Morris13402582005-09-30 14:24:34 -04001157static inline int default_protocol_stream(int protocol)
1158{
1159 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
1160}
1161
1162static inline int default_protocol_dgram(int protocol)
1163{
1164 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1165}
1166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1168{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001169 int extsockclass = selinux_policycap_extsockclass();
Stephen Smalleyda69a532017-01-09 10:07:30 -05001170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 switch (family) {
1172 case PF_UNIX:
1173 switch (type) {
1174 case SOCK_STREAM:
1175 case SOCK_SEQPACKET:
1176 return SECCLASS_UNIX_STREAM_SOCKET;
1177 case SOCK_DGRAM:
Luis Ressel2a764b52017-07-25 15:13:41 -04001178 case SOCK_RAW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 return SECCLASS_UNIX_DGRAM_SOCKET;
1180 }
1181 break;
1182 case PF_INET:
1183 case PF_INET6:
1184 switch (type) {
1185 case SOCK_STREAM:
Stephen Smalleyda69a532017-01-09 10:07:30 -05001186 case SOCK_SEQPACKET:
James Morris13402582005-09-30 14:24:34 -04001187 if (default_protocol_stream(protocol))
1188 return SECCLASS_TCP_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001189 else if (extsockclass && protocol == IPPROTO_SCTP)
1190 return SECCLASS_SCTP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001191 else
1192 return SECCLASS_RAWIP_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 case SOCK_DGRAM:
James Morris13402582005-09-30 14:24:34 -04001194 if (default_protocol_dgram(protocol))
1195 return SECCLASS_UDP_SOCKET;
Stephen Smalleyef379792017-01-09 10:07:31 -05001196 else if (extsockclass && (protocol == IPPROTO_ICMP ||
1197 protocol == IPPROTO_ICMPV6))
Stephen Smalleyda69a532017-01-09 10:07:30 -05001198 return SECCLASS_ICMP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001199 else
1200 return SECCLASS_RAWIP_SOCKET;
James Morris2ee92d42006-11-13 16:09:01 -08001201 case SOCK_DCCP:
1202 return SECCLASS_DCCP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001203 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 return SECCLASS_RAWIP_SOCKET;
1205 }
1206 break;
1207 case PF_NETLINK:
1208 switch (protocol) {
1209 case NETLINK_ROUTE:
1210 return SECCLASS_NETLINK_ROUTE_SOCKET;
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001211 case NETLINK_SOCK_DIAG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1213 case NETLINK_NFLOG:
1214 return SECCLASS_NETLINK_NFLOG_SOCKET;
1215 case NETLINK_XFRM:
1216 return SECCLASS_NETLINK_XFRM_SOCKET;
1217 case NETLINK_SELINUX:
1218 return SECCLASS_NETLINK_SELINUX_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001219 case NETLINK_ISCSI:
1220 return SECCLASS_NETLINK_ISCSI_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 case NETLINK_AUDIT:
1222 return SECCLASS_NETLINK_AUDIT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001223 case NETLINK_FIB_LOOKUP:
1224 return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET;
1225 case NETLINK_CONNECTOR:
1226 return SECCLASS_NETLINK_CONNECTOR_SOCKET;
1227 case NETLINK_NETFILTER:
1228 return SECCLASS_NETLINK_NETFILTER_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 case NETLINK_DNRTMSG:
1230 return SECCLASS_NETLINK_DNRT_SOCKET;
James Morris0c9b7942005-04-16 15:24:13 -07001231 case NETLINK_KOBJECT_UEVENT:
1232 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001233 case NETLINK_GENERIC:
1234 return SECCLASS_NETLINK_GENERIC_SOCKET;
1235 case NETLINK_SCSITRANSPORT:
1236 return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET;
1237 case NETLINK_RDMA:
1238 return SECCLASS_NETLINK_RDMA_SOCKET;
1239 case NETLINK_CRYPTO:
1240 return SECCLASS_NETLINK_CRYPTO_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 default:
1242 return SECCLASS_NETLINK_SOCKET;
1243 }
1244 case PF_PACKET:
1245 return SECCLASS_PACKET_SOCKET;
1246 case PF_KEY:
1247 return SECCLASS_KEY_SOCKET;
Christopher J. PeBenito3e3ff152006-06-09 00:25:03 -07001248 case PF_APPLETALK:
1249 return SECCLASS_APPLETALK_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
1251
Stephen Smalleyda69a532017-01-09 10:07:30 -05001252 if (extsockclass) {
1253 switch (family) {
1254 case PF_AX25:
1255 return SECCLASS_AX25_SOCKET;
1256 case PF_IPX:
1257 return SECCLASS_IPX_SOCKET;
1258 case PF_NETROM:
1259 return SECCLASS_NETROM_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001260 case PF_ATMPVC:
1261 return SECCLASS_ATMPVC_SOCKET;
1262 case PF_X25:
1263 return SECCLASS_X25_SOCKET;
1264 case PF_ROSE:
1265 return SECCLASS_ROSE_SOCKET;
1266 case PF_DECnet:
1267 return SECCLASS_DECNET_SOCKET;
1268 case PF_ATMSVC:
1269 return SECCLASS_ATMSVC_SOCKET;
1270 case PF_RDS:
1271 return SECCLASS_RDS_SOCKET;
1272 case PF_IRDA:
1273 return SECCLASS_IRDA_SOCKET;
1274 case PF_PPPOX:
1275 return SECCLASS_PPPOX_SOCKET;
1276 case PF_LLC:
1277 return SECCLASS_LLC_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001278 case PF_CAN:
1279 return SECCLASS_CAN_SOCKET;
1280 case PF_TIPC:
1281 return SECCLASS_TIPC_SOCKET;
1282 case PF_BLUETOOTH:
1283 return SECCLASS_BLUETOOTH_SOCKET;
1284 case PF_IUCV:
1285 return SECCLASS_IUCV_SOCKET;
1286 case PF_RXRPC:
1287 return SECCLASS_RXRPC_SOCKET;
1288 case PF_ISDN:
1289 return SECCLASS_ISDN_SOCKET;
1290 case PF_PHONET:
1291 return SECCLASS_PHONET_SOCKET;
1292 case PF_IEEE802154:
1293 return SECCLASS_IEEE802154_SOCKET;
1294 case PF_CAIF:
1295 return SECCLASS_CAIF_SOCKET;
1296 case PF_ALG:
1297 return SECCLASS_ALG_SOCKET;
1298 case PF_NFC:
1299 return SECCLASS_NFC_SOCKET;
1300 case PF_VSOCK:
1301 return SECCLASS_VSOCK_SOCKET;
1302 case PF_KCM:
1303 return SECCLASS_KCM_SOCKET;
1304 case PF_QIPCRTR:
1305 return SECCLASS_QIPCRTR_SOCKET;
Linus Torvalds3051bf32017-02-22 10:15:09 -08001306 case PF_SMC:
1307 return SECCLASS_SMC_SOCKET;
Björn Töpel68e8b842018-05-02 13:01:22 +02001308 case PF_XDP:
1309 return SECCLASS_XDP_SOCKET;
1310#if PF_MAX > 45
Stephen Smalleyda69a532017-01-09 10:07:30 -05001311#error New address family defined, please update this function.
1312#endif
1313 }
1314 }
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 return SECCLASS_SOCKET;
1317}
1318
Stephen Smalley134509d2015-06-04 16:22:17 -04001319static int selinux_genfs_get_sid(struct dentry *dentry,
1320 u16 tclass,
1321 u16 flags,
1322 u32 *sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001324 int rc;
Al Virofc640052016-04-10 01:33:30 -04001325 struct super_block *sb = dentry->d_sb;
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001326 char *buffer, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Eric Paris828dfe12008-04-17 13:17:49 -04001328 buffer = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 if (!buffer)
1330 return -ENOMEM;
1331
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001332 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1333 if (IS_ERR(path))
1334 rc = PTR_ERR(path);
1335 else {
Stephen Smalley134509d2015-06-04 16:22:17 -04001336 if (flags & SE_SBPROC) {
1337 /* each process gets a /proc/PID/ entry. Strip off the
1338 * PID part to get a valid selinux labeling.
1339 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1340 while (path[1] >= '0' && path[1] <= '9') {
1341 path[1] = '/';
1342 path++;
1343 }
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001344 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001345 rc = security_genfs_sid(&selinux_state, sb->s_type->name,
1346 path, tclass, sid);
Stephen Smalley7bb185e2018-09-04 16:51:36 -04001347 if (rc == -ENOENT) {
1348 /* No match in policy, mark as unlabeled. */
1349 *sid = SECINITSID_UNLABELED;
1350 rc = 0;
1351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 free_page((unsigned long)buffer);
1354 return rc;
1355}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357/* The inode's security attributes must be initialized before first use. */
1358static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1359{
1360 struct superblock_security_struct *sbsec = NULL;
Casey Schaufler80788c22018-09-21 17:19:11 -07001361 struct inode_security_struct *isec = selinux_inode(inode);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001362 u32 task_sid, sid = 0;
1363 u16 sclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 struct dentry *dentry;
1365#define INITCONTEXTLEN 255
1366 char *context = NULL;
1367 unsigned len = 0;
1368 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001370 if (isec->initialized == LABEL_INITIALIZED)
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001371 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001373 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001374 if (isec->initialized == LABEL_INITIALIZED)
Eric Paris23970742006-09-25 23:32:01 -07001375 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001377 if (isec->sclass == SECCLASS_FILE)
1378 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 sbsec = inode->i_sb->s_security;
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001381 if (!(sbsec->flags & SE_SBINITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 /* Defer initialization until selinux_complete_init,
1383 after the initial policy is loaded and the security
1384 server is ready to handle calls. */
1385 spin_lock(&sbsec->isec_lock);
1386 if (list_empty(&isec->list))
1387 list_add(&isec->list, &sbsec->isec_head);
1388 spin_unlock(&sbsec->isec_lock);
Eric Paris23970742006-09-25 23:32:01 -07001389 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
1391
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001392 sclass = isec->sclass;
1393 task_sid = isec->task_sid;
1394 sid = isec->sid;
1395 isec->initialized = LABEL_PENDING;
1396 spin_unlock(&isec->lock);
1397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 switch (sbsec->behavior) {
David Quigleyeb9ae682013-05-22 12:50:37 -04001399 case SECURITY_FS_USE_NATIVE:
1400 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 case SECURITY_FS_USE_XATTR:
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001402 if (!(inode->i_opflags & IOP_XATTR)) {
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001403 sid = sbsec->def_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 break;
1405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 /* Need a dentry, since the xattr API requires one.
1407 Life would be simpler if we could just pass the inode. */
1408 if (opt_dentry) {
1409 /* Called from d_instantiate or d_splice_alias. */
1410 dentry = dget(opt_dentry);
1411 } else {
Al Virob1271252018-04-25 10:28:38 -04001412 /*
1413 * Called from selinux_complete_init, try to find a dentry.
1414 * Some filesystems really want a connected one, so try
1415 * that first. We could split SECURITY_FS_USE_XATTR in
1416 * two, depending upon that...
1417 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001419 if (!dentry)
1420 dentry = d_find_any_alias(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 }
1422 if (!dentry) {
Eric Parisdf7f54c2009-03-09 14:35:58 -04001423 /*
1424 * this is can be hit on boot when a file is accessed
1425 * before the policy is loaded. When we load policy we
1426 * may find inodes that have no dentry on the
1427 * sbsec->isec_head list. No reason to complain as these
1428 * will get fixed up the next time we go through
1429 * inode_doinit with a dentry, before these inodes could
1430 * be used again by userspace.
1431 */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001432 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 }
1434
1435 len = INITCONTEXTLEN;
Eric Paris4cb912f2009-02-12 14:50:05 -05001436 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 if (!context) {
1438 rc = -ENOMEM;
1439 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001440 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001442 context[len] = '\0';
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001443 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 if (rc == -ERANGE) {
James Morris314dabb2009-08-10 22:00:13 +10001445 kfree(context);
1446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 /* Need a larger buffer. Query for the right size. */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001448 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 if (rc < 0) {
1450 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001451 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 len = rc;
Eric Paris4cb912f2009-02-12 14:50:05 -05001454 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 if (!context) {
1456 rc = -ENOMEM;
1457 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001458 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001460 context[len] = '\0';
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001461 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 }
1463 dput(dentry);
1464 if (rc < 0) {
1465 if (rc != -ENODATA) {
peter enderborgc103a912018-06-12 10:09:03 +02001466 pr_warn("SELinux: %s: getxattr returned "
Harvey Harrisondd6f9532008-03-06 10:03:59 +11001467 "%d for dev=%s ino=%ld\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 -rc, inode->i_sb->s_id, inode->i_ino);
1469 kfree(context);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001470 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 }
1472 /* Map ENODATA to the default file SID */
1473 sid = sbsec->def_sid;
1474 rc = 0;
1475 } else {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001476 rc = security_context_to_sid_default(&selinux_state,
1477 context, rc, &sid,
Stephen Smalley869ab512008-04-04 08:46:05 -04001478 sbsec->def_sid,
1479 GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 if (rc) {
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001481 char *dev = inode->i_sb->s_id;
1482 unsigned long ino = inode->i_ino;
1483
1484 if (rc == -EINVAL) {
1485 if (printk_ratelimit())
peter enderborgc103a912018-06-12 10:09:03 +02001486 pr_notice("SELinux: inode=%lu on dev=%s was found to have an invalid "
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001487 "context=%s. This indicates you may need to relabel the inode or the "
1488 "filesystem in question.\n", ino, dev, context);
1489 } else {
peter enderborgc103a912018-06-12 10:09:03 +02001490 pr_warn("SELinux: %s: context_to_sid(%s) "
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001491 "returned %d for dev=%s ino=%ld\n",
1492 __func__, context, -rc, dev, ino);
1493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 kfree(context);
1495 /* Leave with the unlabeled SID */
1496 rc = 0;
1497 break;
1498 }
1499 }
1500 kfree(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 break;
1502 case SECURITY_FS_USE_TASK:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001503 sid = task_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 break;
1505 case SECURITY_FS_USE_TRANS:
1506 /* Default to the fs SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001507 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
1509 /* Try to obtain a transition SID. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001510 rc = security_transition_sid(&selinux_state, task_sid, sid,
1511 sclass, NULL, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 if (rc)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001513 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 break;
Eric Parisc312feb2006-07-10 04:43:53 -07001515 case SECURITY_FS_USE_MNTPOINT:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001516 sid = sbsec->mntpoint_sid;
Eric Parisc312feb2006-07-10 04:43:53 -07001517 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 default:
Eric Parisc312feb2006-07-10 04:43:53 -07001519 /* Default to the fs superblock SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001520 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Stephen Smalley134509d2015-06-04 16:22:17 -04001522 if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) {
Paul Mooref64410e2014-03-19 16:46:18 -04001523 /* We must have a dentry to determine the label on
1524 * procfs inodes */
Al Virob1271252018-04-25 10:28:38 -04001525 if (opt_dentry) {
Paul Mooref64410e2014-03-19 16:46:18 -04001526 /* Called from d_instantiate or
1527 * d_splice_alias. */
1528 dentry = dget(opt_dentry);
Al Virob1271252018-04-25 10:28:38 -04001529 } else {
Paul Mooref64410e2014-03-19 16:46:18 -04001530 /* Called from selinux_complete_init, try to
Al Virob1271252018-04-25 10:28:38 -04001531 * find a dentry. Some filesystems really want
1532 * a connected one, so try that first.
1533 */
Paul Mooref64410e2014-03-19 16:46:18 -04001534 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001535 if (!dentry)
1536 dentry = d_find_any_alias(inode);
1537 }
Paul Mooref64410e2014-03-19 16:46:18 -04001538 /*
1539 * This can be hit on boot when a file is accessed
1540 * before the policy is loaded. When we load policy we
1541 * may find inodes that have no dentry on the
1542 * sbsec->isec_head list. No reason to complain as
1543 * these will get fixed up the next time we go through
1544 * inode_doinit() with a dentry, before these inodes
1545 * could be used again by userspace.
1546 */
1547 if (!dentry)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001548 goto out;
1549 rc = selinux_genfs_get_sid(dentry, sclass,
Stephen Smalley134509d2015-06-04 16:22:17 -04001550 sbsec->flags, &sid);
Paul Mooref64410e2014-03-19 16:46:18 -04001551 dput(dentry);
1552 if (rc)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001553 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 }
1555 break;
1556 }
1557
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001558out:
1559 spin_lock(&isec->lock);
1560 if (isec->initialized == LABEL_PENDING) {
1561 if (!sid || rc) {
1562 isec->initialized = LABEL_INVALID;
1563 goto out_unlock;
1564 }
1565
1566 isec->initialized = LABEL_INITIALIZED;
1567 isec->sid = sid;
1568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Eric Paris23970742006-09-25 23:32:01 -07001570out_unlock:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001571 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 return rc;
1573}
1574
1575/* Convert a Linux signal to an access vector. */
1576static inline u32 signal_to_av(int sig)
1577{
1578 u32 perm = 0;
1579
1580 switch (sig) {
1581 case SIGCHLD:
1582 /* Commonly granted from child to parent. */
1583 perm = PROCESS__SIGCHLD;
1584 break;
1585 case SIGKILL:
1586 /* Cannot be caught or ignored */
1587 perm = PROCESS__SIGKILL;
1588 break;
1589 case SIGSTOP:
1590 /* Cannot be caught or ignored */
1591 perm = PROCESS__SIGSTOP;
1592 break;
1593 default:
1594 /* All other signals. */
1595 perm = PROCESS__SIGNAL;
1596 break;
1597 }
1598
1599 return perm;
1600}
1601
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001602#if CAP_LAST_CAP > 63
1603#error Fix SELinux to handle capabilities > 63.
1604#endif
1605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606/* Check whether a task is allowed to use a capability. */
Eric Paris6a9de492012-01-03 12:25:14 -05001607static int cred_has_capability(const struct cred *cred,
Micah Mortonc1a85a02019-01-07 16:10:53 -08001608 int cap, unsigned int opts, bool initns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609{
Thomas Liu2bf49692009-07-14 12:14:09 -04001610 struct common_audit_data ad;
Eric Paris06112162008-11-11 22:02:50 +11001611 struct av_decision avd;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001612 u16 sclass;
David Howells3699c532009-01-06 22:27:01 +00001613 u32 sid = cred_sid(cred);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001614 u32 av = CAP_TO_MASK(cap);
Eric Paris06112162008-11-11 22:02:50 +11001615 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
Eric Paris50c205f2012-04-04 15:01:43 -04001617 ad.type = LSM_AUDIT_DATA_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 ad.u.cap = cap;
1619
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001620 switch (CAP_TO_INDEX(cap)) {
1621 case 0:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001622 sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001623 break;
1624 case 1:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001625 sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001626 break;
1627 default:
peter enderborgc103a912018-06-12 10:09:03 +02001628 pr_err("SELinux: out of range capability %d\n", cap);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001629 BUG();
Eric Parisa35c6c832011-04-20 10:21:28 -04001630 return -EINVAL;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001631 }
Eric Paris06112162008-11-11 22:02:50 +11001632
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001633 rc = avc_has_perm_noaudit(&selinux_state,
1634 sid, sid, sclass, av, 0, &avd);
Micah Mortonc1a85a02019-01-07 16:10:53 -08001635 if (!(opts & CAP_OPT_NOAUDIT)) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001636 int rc2 = avc_audit(&selinux_state,
1637 sid, sid, sclass, av, &avd, rc, &ad, 0);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001638 if (rc2)
1639 return rc2;
1640 }
Eric Paris06112162008-11-11 22:02:50 +11001641 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642}
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644/* Check whether a task has a particular permission to an inode.
1645 The 'adp' parameter is optional and allows other audit
1646 data to be passed (e.g. the dentry). */
David Howells88e67f32008-11-14 10:39:21 +11001647static int inode_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 struct inode *inode,
1649 u32 perms,
Linus Torvalds19e49832013-10-04 12:54:11 -07001650 struct common_audit_data *adp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11001653 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
David Howellse0e81732009-09-02 09:13:40 +01001655 validate_creds(cred);
1656
Eric Paris828dfe12008-04-17 13:17:49 -04001657 if (unlikely(IS_PRIVATE(inode)))
Stephen Smalleybbaca6c2007-02-14 00:34:16 -08001658 return 0;
1659
David Howells88e67f32008-11-14 10:39:21 +11001660 sid = cred_sid(cred);
Casey Schaufler80788c22018-09-21 17:19:11 -07001661 isec = selinux_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001663 return avc_has_perm(&selinux_state,
1664 sid, isec->sid, isec->sclass, perms, adp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665}
1666
1667/* Same as inode_has_perm, but pass explicit audit data containing
1668 the dentry to help the auditing code to more easily generate the
1669 pathname if needed. */
David Howells88e67f32008-11-14 10:39:21 +11001670static inline int dentry_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 struct dentry *dentry,
1672 u32 av)
1673{
David Howellsc6f493d2015-03-17 22:26:22 +00001674 struct inode *inode = d_backing_inode(dentry);
Thomas Liu2bf49692009-07-14 12:14:09 -04001675 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001676
Eric Paris50c205f2012-04-04 15:01:43 -04001677 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Paris2875fa02011-04-28 16:04:24 -04001678 ad.u.dentry = dentry;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001679 __inode_security_revalidate(inode, dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001680 return inode_has_perm(cred, inode, av, &ad);
Eric Paris2875fa02011-04-28 16:04:24 -04001681}
1682
1683/* Same as inode_has_perm, but pass explicit audit data containing
1684 the path to help the auditing code to more easily generate the
1685 pathname if needed. */
1686static inline int path_has_perm(const struct cred *cred,
Al Viro3f7036a2015-03-08 19:28:30 -04001687 const struct path *path,
Eric Paris2875fa02011-04-28 16:04:24 -04001688 u32 av)
1689{
David Howellsc6f493d2015-03-17 22:26:22 +00001690 struct inode *inode = d_backing_inode(path->dentry);
Eric Paris2875fa02011-04-28 16:04:24 -04001691 struct common_audit_data ad;
1692
Eric Paris50c205f2012-04-04 15:01:43 -04001693 ad.type = LSM_AUDIT_DATA_PATH;
Eric Paris2875fa02011-04-28 16:04:24 -04001694 ad.u.path = *path;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001695 __inode_security_revalidate(inode, path->dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001696 return inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697}
1698
David Howells13f8e982013-06-13 23:37:55 +01001699/* Same as path_has_perm, but uses the inode from the file struct. */
1700static inline int file_path_has_perm(const struct cred *cred,
1701 struct file *file,
1702 u32 av)
1703{
1704 struct common_audit_data ad;
1705
Vivek Goyal43af5de2016-09-09 11:37:49 -04001706 ad.type = LSM_AUDIT_DATA_FILE;
1707 ad.u.file = file;
Linus Torvalds19e49832013-10-04 12:54:11 -07001708 return inode_has_perm(cred, file_inode(file), av, &ad);
David Howells13f8e982013-06-13 23:37:55 +01001709}
1710
Chenbo Fengf66e4482017-10-18 13:00:26 -07001711#ifdef CONFIG_BPF_SYSCALL
1712static int bpf_fd_pass(struct file *file, u32 sid);
1713#endif
1714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715/* Check whether a task can use an open file descriptor to
1716 access an inode in a given way. Check access to the
1717 descriptor itself, and then use dentry_has_perm to
1718 check a particular permission to the file.
1719 Access to the descriptor is implicitly granted if it
1720 has the same SID as the process. If av is zero, then
1721 access to the file is not checked, e.g. for cases
1722 where only the descriptor is affected like seek. */
David Howells88e67f32008-11-14 10:39:21 +11001723static int file_has_perm(const struct cred *cred,
1724 struct file *file,
1725 u32 av)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07001727 struct file_security_struct *fsec = selinux_file(file);
Al Viro496ad9a2013-01-23 17:07:38 -05001728 struct inode *inode = file_inode(file);
Thomas Liu2bf49692009-07-14 12:14:09 -04001729 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001730 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 int rc;
1732
Vivek Goyal43af5de2016-09-09 11:37:49 -04001733 ad.type = LSM_AUDIT_DATA_FILE;
1734 ad.u.file = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
David Howells275bb412008-11-14 10:39:19 +11001736 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001737 rc = avc_has_perm(&selinux_state,
1738 sid, fsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 SECCLASS_FD,
1740 FD__USE,
1741 &ad);
1742 if (rc)
David Howells88e67f32008-11-14 10:39:21 +11001743 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 }
1745
Chenbo Fengf66e4482017-10-18 13:00:26 -07001746#ifdef CONFIG_BPF_SYSCALL
1747 rc = bpf_fd_pass(file, cred_sid(cred));
1748 if (rc)
1749 return rc;
1750#endif
1751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 /* av is zero if only checking access to the descriptor. */
David Howells88e67f32008-11-14 10:39:21 +11001753 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 if (av)
Linus Torvalds19e49832013-10-04 12:54:11 -07001755 rc = inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
David Howells88e67f32008-11-14 10:39:21 +11001757out:
1758 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759}
1760
David Howellsc3c188b2015-07-10 17:19:58 -04001761/*
1762 * Determine the label for an inode that might be unioned.
1763 */
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001764static int
1765selinux_determine_inode_label(const struct task_security_struct *tsec,
1766 struct inode *dir,
1767 const struct qstr *name, u16 tclass,
1768 u32 *_new_isid)
David Howellsc3c188b2015-07-10 17:19:58 -04001769{
1770 const struct superblock_security_struct *sbsec = dir->i_sb->s_security;
David Howellsc3c188b2015-07-10 17:19:58 -04001771
1772 if ((sbsec->flags & SE_SBINITIALIZED) &&
1773 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) {
1774 *_new_isid = sbsec->mntpoint_sid;
1775 } else if ((sbsec->flags & SBLABEL_MNT) &&
1776 tsec->create_sid) {
1777 *_new_isid = tsec->create_sid;
1778 } else {
Paul Moore20cdef82016-04-04 14:14:42 -04001779 const struct inode_security_struct *dsec = inode_security(dir);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001780 return security_transition_sid(&selinux_state, tsec->sid,
1781 dsec->sid, tclass,
David Howellsc3c188b2015-07-10 17:19:58 -04001782 name, _new_isid);
1783 }
1784
1785 return 0;
1786}
1787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788/* Check whether a task can create a file. */
1789static int may_create(struct inode *dir,
1790 struct dentry *dentry,
1791 u16 tclass)
1792{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07001793 const struct task_security_struct *tsec = selinux_cred(current_cred());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 struct inode_security_struct *dsec;
1795 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11001796 u32 sid, newsid;
Thomas Liu2bf49692009-07-14 12:14:09 -04001797 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 int rc;
1799
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001800 dsec = inode_security(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 sbsec = dir->i_sb->s_security;
1802
David Howells275bb412008-11-14 10:39:19 +11001803 sid = tsec->sid;
David Howells275bb412008-11-14 10:39:19 +11001804
Eric Paris50c205f2012-04-04 15:01:43 -04001805 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001806 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001808 rc = avc_has_perm(&selinux_state,
1809 sid, dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 DIR__ADD_NAME | DIR__SEARCH,
1811 &ad);
1812 if (rc)
1813 return rc;
1814
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07001815 rc = selinux_determine_inode_label(selinux_cred(current_cred()), dir,
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001816 &dentry->d_name, tclass, &newsid);
David Howellsc3c188b2015-07-10 17:19:58 -04001817 if (rc)
1818 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001820 rc = avc_has_perm(&selinux_state,
1821 sid, newsid, tclass, FILE__CREATE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 if (rc)
1823 return rc;
1824
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001825 return avc_has_perm(&selinux_state,
1826 newsid, sbsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 SECCLASS_FILESYSTEM,
1828 FILESYSTEM__ASSOCIATE, &ad);
1829}
1830
Eric Paris828dfe12008-04-17 13:17:49 -04001831#define MAY_LINK 0
1832#define MAY_UNLINK 1
1833#define MAY_RMDIR 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
1835/* Check whether a task can link, unlink, or rmdir a file/directory. */
1836static int may_link(struct inode *dir,
1837 struct dentry *dentry,
1838 int kind)
1839
1840{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 struct inode_security_struct *dsec, *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001842 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001843 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 u32 av;
1845 int rc;
1846
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001847 dsec = inode_security(dir);
1848 isec = backing_inode_security(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Eric Paris50c205f2012-04-04 15:01:43 -04001850 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001851 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
1853 av = DIR__SEARCH;
1854 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001855 rc = avc_has_perm(&selinux_state,
1856 sid, dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 if (rc)
1858 return rc;
1859
1860 switch (kind) {
1861 case MAY_LINK:
1862 av = FILE__LINK;
1863 break;
1864 case MAY_UNLINK:
1865 av = FILE__UNLINK;
1866 break;
1867 case MAY_RMDIR:
1868 av = DIR__RMDIR;
1869 break;
1870 default:
peter enderborgc103a912018-06-12 10:09:03 +02001871 pr_warn("SELinux: %s: unrecognized kind %d\n",
Eric Paris744ba352008-04-17 11:52:44 -04001872 __func__, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 return 0;
1874 }
1875
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001876 rc = avc_has_perm(&selinux_state,
1877 sid, isec->sid, isec->sclass, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 return rc;
1879}
1880
1881static inline int may_rename(struct inode *old_dir,
1882 struct dentry *old_dentry,
1883 struct inode *new_dir,
1884 struct dentry *new_dentry)
1885{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001887 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001888 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 u32 av;
1890 int old_is_dir, new_is_dir;
1891 int rc;
1892
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001893 old_dsec = inode_security(old_dir);
1894 old_isec = backing_inode_security(old_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00001895 old_is_dir = d_is_dir(old_dentry);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001896 new_dsec = inode_security(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Eric Paris50c205f2012-04-04 15:01:43 -04001898 ad.type = LSM_AUDIT_DATA_DENTRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Eric Parisa2694342011-04-25 13:10:27 -04001900 ad.u.dentry = old_dentry;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001901 rc = avc_has_perm(&selinux_state,
1902 sid, old_dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1904 if (rc)
1905 return rc;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001906 rc = avc_has_perm(&selinux_state,
1907 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 Smalley6b6bc622018-03-05 11:47:56 -05001912 rc = avc_has_perm(&selinux_state,
1913 sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 old_isec->sclass, DIR__REPARENT, &ad);
1915 if (rc)
1916 return rc;
1917 }
1918
Eric Parisa2694342011-04-25 13:10:27 -04001919 ad.u.dentry = new_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 av = DIR__ADD_NAME | DIR__SEARCH;
David Howells2c616d42015-01-29 12:02:33 +00001921 if (d_is_positive(new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 av |= DIR__REMOVE_NAME;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001923 rc = avc_has_perm(&selinux_state,
1924 sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 if (rc)
1926 return rc;
David Howells2c616d42015-01-29 12:02:33 +00001927 if (d_is_positive(new_dentry)) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001928 new_isec = backing_inode_security(new_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00001929 new_is_dir = d_is_dir(new_dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001930 rc = avc_has_perm(&selinux_state,
1931 sid, new_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 new_isec->sclass,
1933 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1934 if (rc)
1935 return rc;
1936 }
1937
1938 return 0;
1939}
1940
1941/* Check whether a task can perform a filesystem operation. */
David Howells88e67f32008-11-14 10:39:21 +11001942static int superblock_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 struct super_block *sb,
1944 u32 perms,
Thomas Liu2bf49692009-07-14 12:14:09 -04001945 struct common_audit_data *ad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 struct superblock_security_struct *sbsec;
David Howells88e67f32008-11-14 10:39:21 +11001948 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 sbsec = sb->s_security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001951 return avc_has_perm(&selinux_state,
1952 sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953}
1954
1955/* Convert a Linux mode and permission mask to an access vector. */
1956static inline u32 file_mask_to_av(int mode, int mask)
1957{
1958 u32 av = 0;
1959
Al Virodba19c62011-07-25 20:49:29 -04001960 if (!S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 if (mask & MAY_EXEC)
1962 av |= FILE__EXECUTE;
1963 if (mask & MAY_READ)
1964 av |= FILE__READ;
1965
1966 if (mask & MAY_APPEND)
1967 av |= FILE__APPEND;
1968 else if (mask & MAY_WRITE)
1969 av |= FILE__WRITE;
1970
1971 } else {
1972 if (mask & MAY_EXEC)
1973 av |= DIR__SEARCH;
1974 if (mask & MAY_WRITE)
1975 av |= DIR__WRITE;
1976 if (mask & MAY_READ)
1977 av |= DIR__READ;
1978 }
1979
1980 return av;
1981}
1982
1983/* Convert a Linux file to an access vector. */
1984static inline u32 file_to_av(struct file *file)
1985{
1986 u32 av = 0;
1987
1988 if (file->f_mode & FMODE_READ)
1989 av |= FILE__READ;
1990 if (file->f_mode & FMODE_WRITE) {
1991 if (file->f_flags & O_APPEND)
1992 av |= FILE__APPEND;
1993 else
1994 av |= FILE__WRITE;
1995 }
Stephen Smalley0794c662008-03-17 08:55:18 -04001996 if (!av) {
1997 /*
1998 * Special file opened with flags 3 for ioctl-only use.
1999 */
2000 av = FILE__IOCTL;
2001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
2003 return av;
2004}
2005
Eric Paris8b6a5a32008-10-29 17:06:46 -04002006/*
2007 * Convert a file to an access vector and include the correct open
2008 * open permission.
2009 */
2010static inline u32 open_file_to_av(struct file *file)
2011{
2012 u32 av = file_to_av(file);
Stephen Smalleyccb54472017-05-12 12:41:24 -04002013 struct inode *inode = file_inode(file);
Eric Paris8b6a5a32008-10-29 17:06:46 -04002014
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002015 if (selinux_policycap_openperm() &&
2016 inode->i_sb->s_magic != SOCKFS_MAGIC)
Eric Paris49b7b8d2010-07-23 11:44:09 -04002017 av |= FILE__OPEN;
2018
Eric Paris8b6a5a32008-10-29 17:06:46 -04002019 return av;
2020}
2021
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022/* Hook functions begin here. */
2023
Stephen Smalley79af7302015-01-21 10:54:10 -05002024static int selinux_binder_set_context_mgr(struct task_struct *mgr)
2025{
2026 u32 mysid = current_sid();
2027 u32 mgrsid = task_sid(mgr);
2028
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002029 return avc_has_perm(&selinux_state,
2030 mysid, mgrsid, SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002031 BINDER__SET_CONTEXT_MGR, NULL);
2032}
2033
2034static int selinux_binder_transaction(struct task_struct *from,
2035 struct task_struct *to)
2036{
2037 u32 mysid = current_sid();
2038 u32 fromsid = task_sid(from);
2039 u32 tosid = task_sid(to);
2040 int rc;
2041
2042 if (mysid != fromsid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002043 rc = avc_has_perm(&selinux_state,
2044 mysid, fromsid, SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002045 BINDER__IMPERSONATE, NULL);
2046 if (rc)
2047 return rc;
2048 }
2049
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002050 return avc_has_perm(&selinux_state,
2051 fromsid, tosid, SECCLASS_BINDER, BINDER__CALL,
Stephen Smalley79af7302015-01-21 10:54:10 -05002052 NULL);
2053}
2054
2055static int selinux_binder_transfer_binder(struct task_struct *from,
2056 struct task_struct *to)
2057{
2058 u32 fromsid = task_sid(from);
2059 u32 tosid = task_sid(to);
2060
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002061 return avc_has_perm(&selinux_state,
2062 fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002063 NULL);
2064}
2065
2066static int selinux_binder_transfer_file(struct task_struct *from,
2067 struct task_struct *to,
2068 struct file *file)
2069{
2070 u32 sid = task_sid(to);
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07002071 struct file_security_struct *fsec = selinux_file(file);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002072 struct dentry *dentry = file->f_path.dentry;
Paul Moore20cdef82016-04-04 14:14:42 -04002073 struct inode_security_struct *isec;
Stephen Smalley79af7302015-01-21 10:54:10 -05002074 struct common_audit_data ad;
2075 int rc;
2076
2077 ad.type = LSM_AUDIT_DATA_PATH;
2078 ad.u.path = file->f_path;
2079
2080 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002081 rc = avc_has_perm(&selinux_state,
2082 sid, fsec->sid,
Stephen Smalley79af7302015-01-21 10:54:10 -05002083 SECCLASS_FD,
2084 FD__USE,
2085 &ad);
2086 if (rc)
2087 return rc;
2088 }
2089
Chenbo Fengf66e4482017-10-18 13:00:26 -07002090#ifdef CONFIG_BPF_SYSCALL
2091 rc = bpf_fd_pass(file, sid);
2092 if (rc)
2093 return rc;
2094#endif
2095
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002096 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
Stephen Smalley79af7302015-01-21 10:54:10 -05002097 return 0;
2098
Paul Moore20cdef82016-04-04 14:14:42 -04002099 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002100 return avc_has_perm(&selinux_state,
2101 sid, isec->sid, isec->sclass, file_to_av(file),
Stephen Smalley79af7302015-01-21 10:54:10 -05002102 &ad);
2103}
2104
Ingo Molnar9e488582009-05-07 19:26:19 +10002105static int selinux_ptrace_access_check(struct task_struct *child,
David Howells5cd9c582008-08-14 11:37:28 +01002106 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002108 u32 sid = current_sid();
2109 u32 csid = task_sid(child);
Stephen Smalley006ebb42008-05-19 08:32:49 -04002110
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002111 if (mode & PTRACE_MODE_READ)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002112 return avc_has_perm(&selinux_state,
2113 sid, csid, SECCLASS_FILE, FILE__READ, NULL);
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002114
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002115 return avc_has_perm(&selinux_state,
2116 sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01002117}
2118
2119static int selinux_ptrace_traceme(struct task_struct *parent)
2120{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002121 return avc_has_perm(&selinux_state,
2122 task_sid(parent), current_sid(), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002123 PROCESS__PTRACE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124}
2125
2126static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
Eric Paris828dfe12008-04-17 13:17:49 -04002127 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002129 return avc_has_perm(&selinux_state,
2130 current_sid(), task_sid(target), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002131 PROCESS__GETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132}
2133
David Howellsd84f4f92008-11-14 10:39:23 +11002134static int selinux_capset(struct cred *new, const struct cred *old,
2135 const kernel_cap_t *effective,
2136 const kernel_cap_t *inheritable,
2137 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002139 return avc_has_perm(&selinux_state,
2140 cred_sid(old), cred_sid(new), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002141 PROCESS__SETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142}
2143
James Morris5626d3e2009-01-30 10:05:06 +11002144/*
2145 * (This comment used to live with the selinux_task_setuid hook,
2146 * which was removed).
2147 *
2148 * Since setuid only affects the current process, and since the SELinux
2149 * controls are not based on the Linux identity attributes, SELinux does not
2150 * need to control this operation. However, SELinux does control the use of
2151 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
2152 */
2153
Eric Paris6a9de492012-01-03 12:25:14 -05002154static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
Micah Mortonc1a85a02019-01-07 16:10:53 -08002155 int cap, unsigned int opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
Micah Mortonc1a85a02019-01-07 16:10:53 -08002157 return cred_has_capability(cred, cap, opts, ns == &init_user_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158}
2159
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
2161{
David Howells88e67f32008-11-14 10:39:21 +11002162 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 int rc = 0;
2164
2165 if (!sb)
2166 return 0;
2167
2168 switch (cmds) {
Eric Paris828dfe12008-04-17 13:17:49 -04002169 case Q_SYNC:
2170 case Q_QUOTAON:
2171 case Q_QUOTAOFF:
2172 case Q_SETINFO:
2173 case Q_SETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002174 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002175 break;
2176 case Q_GETFMT:
2177 case Q_GETINFO:
2178 case Q_GETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002179 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002180 break;
2181 default:
2182 rc = 0; /* let the kernel handle invalid cmds */
2183 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 }
2185 return rc;
2186}
2187
2188static int selinux_quota_on(struct dentry *dentry)
2189{
David Howells88e67f32008-11-14 10:39:21 +11002190 const struct cred *cred = current_cred();
2191
Eric Paris2875fa02011-04-28 16:04:24 -04002192 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193}
2194
Eric Paris12b30522010-11-15 18:36:29 -05002195static int selinux_syslog(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08002198 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2199 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002200 return avc_has_perm(&selinux_state,
2201 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002202 SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL);
Kees Cookd78ca3c2010-02-03 15:37:13 -08002203 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2204 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2205 /* Set level of messages printed to console */
2206 case SYSLOG_ACTION_CONSOLE_LEVEL:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002207 return avc_has_perm(&selinux_state,
2208 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002209 SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE,
2210 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 }
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002212 /* All other syslog types */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002213 return avc_has_perm(&selinux_state,
2214 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002215 SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216}
2217
2218/*
2219 * Check that a process has enough memory to allocate a new virtual
2220 * mapping. 0 means there is enough memory for the allocation to
2221 * succeed and -ENOMEM implies there is not.
2222 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 * Do not audit the selinux permission check, as this is applied to all
2224 * processes that allocate mappings.
2225 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07002226static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227{
2228 int rc, cap_sys_admin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002230 rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
Micah Mortonc1a85a02019-01-07 16:10:53 -08002231 CAP_OPT_NOAUDIT, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 if (rc == 0)
2233 cap_sys_admin = 1;
2234
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002235 return cap_sys_admin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236}
2237
2238/* binprm security operations */
2239
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002240static u32 ptrace_parent_sid(void)
Paul Moore0c6181c2016-03-30 21:41:21 -04002241{
2242 u32 sid = 0;
2243 struct task_struct *tracer;
2244
2245 rcu_read_lock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002246 tracer = ptrace_parent(current);
Paul Moore0c6181c2016-03-30 21:41:21 -04002247 if (tracer)
2248 sid = task_sid(tracer);
2249 rcu_read_unlock();
2250
2251 return sid;
2252}
2253
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002254static int check_nnp_nosuid(const struct linux_binprm *bprm,
2255 const struct task_security_struct *old_tsec,
2256 const struct task_security_struct *new_tsec)
2257{
2258 int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
Andy Lutomirski380cf5b2016-06-23 16:41:05 -05002259 int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002260 int rc;
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002261 u32 av;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002262
2263 if (!nnp && !nosuid)
2264 return 0; /* neither NNP nor nosuid */
2265
2266 if (new_tsec->sid == old_tsec->sid)
2267 return 0; /* No change in credentials */
2268
2269 /*
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002270 * If the policy enables the nnp_nosuid_transition policy capability,
2271 * then we permit transitions under NNP or nosuid if the
2272 * policy allows the corresponding permission between
2273 * the old and new contexts.
2274 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002275 if (selinux_policycap_nnp_nosuid_transition()) {
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002276 av = 0;
2277 if (nnp)
2278 av |= PROCESS2__NNP_TRANSITION;
2279 if (nosuid)
2280 av |= PROCESS2__NOSUID_TRANSITION;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002281 rc = avc_has_perm(&selinux_state,
2282 old_tsec->sid, new_tsec->sid,
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002283 SECCLASS_PROCESS2, av, NULL);
2284 if (!rc)
2285 return 0;
2286 }
2287
2288 /*
2289 * We also permit NNP or nosuid transitions to bounded SIDs,
2290 * i.e. SIDs that are guaranteed to only be allowed a subset
2291 * of the permissions of the current SID.
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002292 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002293 rc = security_bounded_transition(&selinux_state, old_tsec->sid,
2294 new_tsec->sid);
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002295 if (!rc)
2296 return 0;
2297
2298 /*
2299 * On failure, preserve the errno values for NNP vs nosuid.
2300 * NNP: Operation not permitted for caller.
2301 * nosuid: Permission denied to file.
2302 */
2303 if (nnp)
2304 return -EPERM;
2305 return -EACCES;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002306}
2307
David Howellsa6f76f22008-11-14 10:39:24 +11002308static int selinux_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309{
David Howellsa6f76f22008-11-14 10:39:24 +11002310 const struct task_security_struct *old_tsec;
2311 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 struct inode_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002313 struct common_audit_data ad;
Al Viro496ad9a2013-01-23 17:07:38 -05002314 struct inode *inode = file_inode(bprm->file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 int rc;
2316
David Howellsa6f76f22008-11-14 10:39:24 +11002317 /* SELinux context only depends on initial program or script and not
2318 * the script interpreter */
Kees Cookddb4a142017-07-18 15:25:23 -07002319 if (bprm->called_set_creds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 return 0;
2321
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002322 old_tsec = selinux_cred(current_cred());
2323 new_tsec = selinux_cred(bprm->cred);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002324 isec = inode_security(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
2326 /* Default to the current task SID. */
David Howellsa6f76f22008-11-14 10:39:24 +11002327 new_tsec->sid = old_tsec->sid;
2328 new_tsec->osid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Michael LeMay28eba5b2006-06-27 02:53:42 -07002330 /* Reset fs, key, and sock SIDs on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002331 new_tsec->create_sid = 0;
2332 new_tsec->keycreate_sid = 0;
2333 new_tsec->sockcreate_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
David Howellsa6f76f22008-11-14 10:39:24 +11002335 if (old_tsec->exec_sid) {
2336 new_tsec->sid = old_tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 /* Reset exec SID on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002338 new_tsec->exec_sid = 0;
Andy Lutomirski259e5e62012-04-12 16:47:50 -05002339
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002340 /* Fail on NNP or nosuid if not an allowed transition. */
2341 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2342 if (rc)
2343 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 } else {
2345 /* Check for a default transition on this program. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002346 rc = security_transition_sid(&selinux_state, old_tsec->sid,
2347 isec->sid, SECCLASS_PROCESS, NULL,
Eric Paris652bb9b2011-02-01 11:05:40 -05002348 &new_tsec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 if (rc)
2350 return rc;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002351
2352 /*
2353 * Fallback to old SID on NNP or nosuid if not an allowed
2354 * transition.
2355 */
2356 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2357 if (rc)
2358 new_tsec->sid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 }
2360
Vivek Goyal43af5de2016-09-09 11:37:49 -04002361 ad.type = LSM_AUDIT_DATA_FILE;
2362 ad.u.file = bprm->file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
David Howellsa6f76f22008-11-14 10:39:24 +11002364 if (new_tsec->sid == old_tsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002365 rc = avc_has_perm(&selinux_state,
2366 old_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2368 if (rc)
2369 return rc;
2370 } else {
2371 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002372 rc = avc_has_perm(&selinux_state,
2373 old_tsec->sid, new_tsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2375 if (rc)
2376 return rc;
2377
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002378 rc = avc_has_perm(&selinux_state,
2379 new_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2381 if (rc)
2382 return rc;
2383
David Howellsa6f76f22008-11-14 10:39:24 +11002384 /* Check for shared state */
2385 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002386 rc = avc_has_perm(&selinux_state,
2387 old_tsec->sid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002388 SECCLASS_PROCESS, PROCESS__SHARE,
2389 NULL);
2390 if (rc)
2391 return -EPERM;
2392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
David Howellsa6f76f22008-11-14 10:39:24 +11002394 /* Make sure that anyone attempting to ptrace over a task that
2395 * changes its SID has the appropriate permit */
Eric W. Biederman9227dd22017-01-23 17:26:31 +13002396 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002397 u32 ptsid = ptrace_parent_sid();
David Howellsa6f76f22008-11-14 10:39:24 +11002398 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002399 rc = avc_has_perm(&selinux_state,
2400 ptsid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002401 SECCLASS_PROCESS,
2402 PROCESS__PTRACE, NULL);
2403 if (rc)
2404 return -EPERM;
2405 }
2406 }
2407
2408 /* Clear any possibly unsafe personality bits on exec: */
2409 bprm->per_clear |= PER_CLEAR_ON_SETID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 /* Enable secure mode for SIDs transitions unless
2412 the noatsecure permission is granted between
2413 the two SIDs, i.e. ahp returns 0. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002414 rc = avc_has_perm(&selinux_state,
2415 old_tsec->sid, new_tsec->sid,
Kees Cook62874c32017-07-18 15:25:25 -07002416 SECCLASS_PROCESS, PROCESS__NOATSECURE,
2417 NULL);
2418 bprm->secureexec |= !!rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 }
2420
Kees Cook62874c32017-07-18 15:25:25 -07002421 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422}
2423
Al Viroc3c073f2012-08-21 22:32:06 -04002424static int match_file(const void *p, struct file *file, unsigned fd)
2425{
2426 return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2427}
2428
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429/* Derived from fs/exec.c:flush_old_files. */
David Howells745ca242008-11-14 10:39:22 +11002430static inline void flush_unauthorized_files(const struct cred *cred,
2431 struct files_struct *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 struct file *file, *devnull = NULL;
Stephen Smalleyb20c8122006-09-25 23:32:03 -07002434 struct tty_struct *tty;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002435 int drop_tty = 0;
Al Viroc3c073f2012-08-21 22:32:06 -04002436 unsigned n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002438 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 if (tty) {
Peter Hurley4a510962016-01-09 21:35:23 -08002440 spin_lock(&tty->files_lock);
Eric Paris37dd0bd2008-10-31 17:40:00 -04002441 if (!list_empty(&tty->tty_files)) {
Nick Piggind996b622010-08-18 04:37:36 +10002442 struct tty_file_private *file_priv;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002443
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 /* Revalidate access to controlling tty.
David Howells13f8e982013-06-13 23:37:55 +01002445 Use file_path_has_perm on the tty path directly
2446 rather than using file_has_perm, as this particular
2447 open file may belong to another process and we are
2448 only interested in the inode-based check here. */
Nick Piggind996b622010-08-18 04:37:36 +10002449 file_priv = list_first_entry(&tty->tty_files,
2450 struct tty_file_private, list);
2451 file = file_priv->file;
David Howells13f8e982013-06-13 23:37:55 +01002452 if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002453 drop_tty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 }
Peter Hurley4a510962016-01-09 21:35:23 -08002455 spin_unlock(&tty->files_lock);
Alan Cox452a00d2008-10-13 10:39:13 +01002456 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 }
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07002458 /* Reset controlling tty. */
2459 if (drop_tty)
2460 no_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
2462 /* Revalidate access to inherited open files. */
Al Viroc3c073f2012-08-21 22:32:06 -04002463 n = iterate_fd(files, 0, match_file, cred);
2464 if (!n) /* none found? */
2465 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
Al Viroc3c073f2012-08-21 22:32:06 -04002467 devnull = dentry_open(&selinux_null, O_RDWR, cred);
Al Viro45525b22012-10-16 13:30:07 -04002468 if (IS_ERR(devnull))
2469 devnull = NULL;
2470 /* replace all the matching ones with this */
2471 do {
2472 replace_fd(n - 1, devnull, 0);
2473 } while ((n = iterate_fd(files, n, match_file, cred)) != 0);
2474 if (devnull)
Al Viroc3c073f2012-08-21 22:32:06 -04002475 fput(devnull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476}
2477
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478/*
David Howellsa6f76f22008-11-14 10:39:24 +11002479 * Prepare a process for imminent new credential changes due to exec
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 */
David Howellsa6f76f22008-11-14 10:39:24 +11002481static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482{
David Howellsa6f76f22008-11-14 10:39:24 +11002483 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 struct rlimit *rlim, *initrlim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 int rc, i;
2486
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002487 new_tsec = selinux_cred(bprm->cred);
David Howellsa6f76f22008-11-14 10:39:24 +11002488 if (new_tsec->sid == new_tsec->osid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 return;
2490
2491 /* Close files for which the new task SID is not authorized. */
David Howellsa6f76f22008-11-14 10:39:24 +11002492 flush_unauthorized_files(bprm->cred, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
David Howellsa6f76f22008-11-14 10:39:24 +11002494 /* Always clear parent death signal on SID transitions. */
2495 current->pdeath_signal = 0;
2496
2497 /* Check whether the new SID can inherit resource limits from the old
2498 * SID. If not, reset all soft limits to the lower of the current
2499 * task's hard limit and the init task's soft limit.
2500 *
2501 * Note that the setting of hard limits (even to lower them) can be
2502 * controlled by the setrlimit check. The inclusion of the init task's
2503 * soft limit into the computation is to avoid resetting soft limits
2504 * higher than the default soft limit for cases where the default is
2505 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2506 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002507 rc = avc_has_perm(&selinux_state,
2508 new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
David Howellsa6f76f22008-11-14 10:39:24 +11002509 PROCESS__RLIMITINH, NULL);
2510 if (rc) {
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002511 /* protect against do_prlimit() */
2512 task_lock(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002513 for (i = 0; i < RLIM_NLIMITS; i++) {
2514 rlim = current->signal->rlim + i;
2515 initrlim = init_task.signal->rlim + i;
2516 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2517 }
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002518 task_unlock(current);
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05002519 if (IS_ENABLED(CONFIG_POSIX_TIMERS))
2520 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
David Howellsa6f76f22008-11-14 10:39:24 +11002521 }
2522}
2523
2524/*
2525 * Clean up the process immediately after the installation of new credentials
2526 * due to exec
2527 */
2528static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2529{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002530 const struct task_security_struct *tsec = selinux_cred(current_cred());
David Howellsa6f76f22008-11-14 10:39:24 +11002531 struct itimerval itimer;
David Howellsa6f76f22008-11-14 10:39:24 +11002532 u32 osid, sid;
2533 int rc, i;
David Howellsa6f76f22008-11-14 10:39:24 +11002534
David Howellsa6f76f22008-11-14 10:39:24 +11002535 osid = tsec->osid;
2536 sid = tsec->sid;
2537
2538 if (sid == osid)
2539 return;
2540
2541 /* Check whether the new SID can inherit signal state from the old SID.
2542 * If not, clear itimers to avoid subsequent signal generation and
2543 * flush and unblock signals.
2544 *
2545 * This must occur _after_ the task SID has been updated so that any
2546 * kill done after the flush will be checked against the new SID.
2547 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002548 rc = avc_has_perm(&selinux_state,
2549 osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 if (rc) {
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05002551 if (IS_ENABLED(CONFIG_POSIX_TIMERS)) {
2552 memset(&itimer, 0, sizeof itimer);
2553 for (i = 0; i < 3; i++)
2554 do_setitimer(i, &itimer, NULL);
2555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002557 if (!fatal_signal_pending(current)) {
2558 flush_sigqueue(&current->pending);
2559 flush_sigqueue(&current->signal->shared_pending);
David Howells3bcac022009-04-29 13:45:05 +01002560 flush_signal_handlers(current, 1);
2561 sigemptyset(&current->blocked);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002562 recalc_sigpending();
David Howells3bcac022009-04-29 13:45:05 +01002563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 spin_unlock_irq(&current->sighand->siglock);
2565 }
2566
David Howellsa6f76f22008-11-14 10:39:24 +11002567 /* Wake up the parent if it is waiting so that it can recheck
2568 * wait permission to the new task SID. */
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002569 read_lock(&tasklist_lock);
Oleg Nesterov0b7570e2009-09-23 15:56:46 -07002570 __wake_up_parent(current, current->real_parent);
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002571 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572}
2573
2574/* superblock security operations */
2575
2576static int selinux_sb_alloc_security(struct super_block *sb)
2577{
2578 return superblock_alloc_security(sb);
2579}
2580
2581static void selinux_sb_free_security(struct super_block *sb)
2582{
2583 superblock_free_security(sb);
2584}
2585
Al Viro99dbbb52018-12-14 21:56:23 -05002586static inline int opt_len(const char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587{
Al Viro99dbbb52018-12-14 21:56:23 -05002588 bool open_quote = false;
2589 int len;
2590 char c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
Al Viro99dbbb52018-12-14 21:56:23 -05002592 for (len = 0; (c = s[len]) != '\0'; len++) {
2593 if (c == '"')
Cory Olmo3528a952006-09-29 01:58:44 -07002594 open_quote = !open_quote;
Al Viro99dbbb52018-12-14 21:56:23 -05002595 if (c == ',' && !open_quote)
2596 break;
2597 }
2598 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599}
2600
Al Viro204cc0c2018-12-13 13:41:47 -05002601static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
Eric Paris026eb162011-03-03 16:09:14 -05002602{
Al Viro99dbbb52018-12-14 21:56:23 -05002603 char *from = options;
2604 char *to = options;
2605 bool first = true;
Al Viro5b400232018-12-12 20:13:29 -05002606
Al Viro99dbbb52018-12-14 21:56:23 -05002607 while (1) {
2608 int len = opt_len(from);
2609 int token, rc;
2610 char *arg = NULL;
2611
2612 token = match_opt_prefix(from, len, &arg);
2613
2614 if (token != Opt_error) {
2615 char *p, *q;
2616
2617 /* strip quotes */
2618 if (arg) {
2619 for (p = q = arg; p < from + len; p++) {
2620 char c = *p;
2621 if (c != '"')
2622 *q++ = c;
2623 }
2624 arg = kmemdup_nul(arg, q - arg, GFP_KERNEL);
2625 }
2626 rc = selinux_add_opt(token, arg, mnt_opts);
2627 if (unlikely(rc)) {
2628 kfree(arg);
2629 if (*mnt_opts) {
2630 selinux_free_mnt_opts(*mnt_opts);
2631 *mnt_opts = NULL;
2632 }
2633 return rc;
2634 }
2635 } else {
2636 if (!first) { // copy with preceding comma
2637 from--;
2638 len++;
2639 }
2640 if (to != from)
2641 memmove(to, from, len);
2642 to += len;
2643 first = false;
2644 }
2645 if (!from[len])
2646 break;
2647 from += len + 1;
2648 }
2649 *to = '\0';
2650 return 0;
Al Viro5b400232018-12-12 20:13:29 -05002651}
2652
Al Viro204cc0c2018-12-13 13:41:47 -05002653static int selinux_sb_remount(struct super_block *sb, void *mnt_opts)
Eric Paris026eb162011-03-03 16:09:14 -05002654{
Al Virobd323652018-12-13 15:04:59 -05002655 struct selinux_mnt_opts *opts = mnt_opts;
Eric Paris026eb162011-03-03 16:09:14 -05002656 struct superblock_security_struct *sbsec = sb->s_security;
Al Virobd323652018-12-13 15:04:59 -05002657 u32 sid;
2658 int rc;
Eric Paris026eb162011-03-03 16:09:14 -05002659
2660 if (!(sbsec->flags & SE_SBINITIALIZED))
2661 return 0;
2662
Al Viro204cc0c2018-12-13 13:41:47 -05002663 if (!opts)
Eric Paris026eb162011-03-03 16:09:14 -05002664 return 0;
2665
Al Virobd323652018-12-13 15:04:59 -05002666 if (opts->fscontext) {
2667 rc = parse_sid(sb, opts->fscontext, &sid);
2668 if (rc)
Al Viroc039bc32018-12-01 23:06:57 -05002669 return rc;
Al Virobd323652018-12-13 15:04:59 -05002670 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2671 goto out_bad_option;
Eric Paris026eb162011-03-03 16:09:14 -05002672 }
Al Virobd323652018-12-13 15:04:59 -05002673 if (opts->context) {
2674 rc = parse_sid(sb, opts->context, &sid);
2675 if (rc)
2676 return rc;
2677 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2678 goto out_bad_option;
2679 }
2680 if (opts->rootcontext) {
2681 struct inode_security_struct *root_isec;
2682 root_isec = backing_inode_security(sb->s_root);
2683 rc = parse_sid(sb, opts->rootcontext, &sid);
2684 if (rc)
2685 return rc;
2686 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2687 goto out_bad_option;
2688 }
2689 if (opts->defcontext) {
2690 rc = parse_sid(sb, opts->defcontext, &sid);
2691 if (rc)
2692 return rc;
2693 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2694 goto out_bad_option;
Eric Paris026eb162011-03-03 16:09:14 -05002695 }
Al Viroc039bc32018-12-01 23:06:57 -05002696 return 0;
Eric Paris026eb162011-03-03 16:09:14 -05002697
Eric Paris026eb162011-03-03 16:09:14 -05002698out_bad_option:
peter enderborgc103a912018-06-12 10:09:03 +02002699 pr_warn("SELinux: unable to change security options "
Linus Torvalds29b1deb2013-12-15 11:17:45 -08002700 "during remount (dev %s, type=%s)\n", sb->s_id,
2701 sb->s_type->name);
Al Viroc039bc32018-12-01 23:06:57 -05002702 return -EINVAL;
Eric Paris026eb162011-03-03 16:09:14 -05002703}
2704
Al Viroa10d7c22018-12-05 11:58:35 -05002705static int selinux_sb_kern_mount(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706{
David Howells88e67f32008-11-14 10:39:21 +11002707 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002708 struct common_audit_data ad;
James Morris74192242008-12-19 11:41:10 +11002709
Eric Paris50c205f2012-04-04 15:01:43 -04002710 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002711 ad.u.dentry = sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002712 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713}
2714
David Howells726c3342006-06-23 02:02:58 -07002715static int selinux_sb_statfs(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716{
David Howells88e67f32008-11-14 10:39:21 +11002717 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002718 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
Eric Paris50c205f2012-04-04 15:01:43 -04002720 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002721 ad.u.dentry = dentry->d_sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002722 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723}
2724
Al Viro808d4e32012-10-11 11:42:01 -04002725static int selinux_mount(const char *dev_name,
Al Viro8a04c432016-03-25 14:52:53 -04002726 const struct path *path,
Al Viro808d4e32012-10-11 11:42:01 -04002727 const char *type,
Eric Paris828dfe12008-04-17 13:17:49 -04002728 unsigned long flags,
2729 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730{
David Howells88e67f32008-11-14 10:39:21 +11002731 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732
2733 if (flags & MS_REMOUNT)
Al Virod8c95842011-12-07 18:16:57 -05002734 return superblock_has_perm(cred, path->dentry->d_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002735 FILESYSTEM__REMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 else
Eric Paris2875fa02011-04-28 16:04:24 -04002737 return path_has_perm(cred, path, FILE__MOUNTON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738}
2739
2740static int selinux_umount(struct vfsmount *mnt, int flags)
2741{
David Howells88e67f32008-11-14 10:39:21 +11002742 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
David Howells88e67f32008-11-14 10:39:21 +11002744 return superblock_has_perm(cred, mnt->mnt_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002745 FILESYSTEM__UNMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746}
2747
Al Viro0b520752018-12-23 16:02:47 -05002748static int selinux_fs_context_dup(struct fs_context *fc,
2749 struct fs_context *src_fc)
2750{
2751 const struct selinux_mnt_opts *src = src_fc->security;
2752 struct selinux_mnt_opts *opts;
2753
2754 if (!src)
2755 return 0;
2756
2757 fc->security = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL);
2758 if (!fc->security)
2759 return -ENOMEM;
2760
2761 opts = fc->security;
2762
2763 if (src->fscontext) {
2764 opts->fscontext = kstrdup(src->fscontext, GFP_KERNEL);
2765 if (!opts->fscontext)
2766 return -ENOMEM;
2767 }
2768 if (src->context) {
2769 opts->context = kstrdup(src->context, GFP_KERNEL);
2770 if (!opts->context)
2771 return -ENOMEM;
2772 }
2773 if (src->rootcontext) {
2774 opts->rootcontext = kstrdup(src->rootcontext, GFP_KERNEL);
2775 if (!opts->rootcontext)
2776 return -ENOMEM;
2777 }
2778 if (src->defcontext) {
2779 opts->defcontext = kstrdup(src->defcontext, GFP_KERNEL);
2780 if (!opts->defcontext)
2781 return -ENOMEM;
2782 }
2783 return 0;
2784}
2785
David Howells442155c2018-11-01 23:07:24 +00002786static const struct fs_parameter_spec selinux_param_specs[] = {
2787 fsparam_string(CONTEXT_STR, Opt_context),
2788 fsparam_string(DEFCONTEXT_STR, Opt_defcontext),
2789 fsparam_string(FSCONTEXT_STR, Opt_fscontext),
2790 fsparam_string(ROOTCONTEXT_STR, Opt_rootcontext),
2791 fsparam_flag (SECLABEL_STR, Opt_seclabel),
2792 {}
2793};
2794
2795static const struct fs_parameter_description selinux_fs_parameters = {
2796 .name = "SELinux",
2797 .specs = selinux_param_specs,
2798};
2799
2800static int selinux_fs_context_parse_param(struct fs_context *fc,
2801 struct fs_parameter *param)
2802{
2803 struct fs_parse_result result;
2804 int opt, rc;
2805
2806 opt = fs_parse(fc, &selinux_fs_parameters, param, &result);
2807 if (opt < 0)
2808 return opt;
2809
2810 rc = selinux_add_opt(opt, param->string, &fc->security);
2811 if (!rc) {
2812 param->string = NULL;
2813 rc = 1;
2814 }
2815 return rc;
2816}
2817
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818/* inode security operations */
2819
2820static int selinux_inode_alloc_security(struct inode *inode)
2821{
2822 return inode_alloc_security(inode);
2823}
2824
2825static void selinux_inode_free_security(struct inode *inode)
2826{
2827 inode_free_security(inode);
2828}
2829
David Quigleyd47be3d2013-05-22 12:50:34 -04002830static int selinux_dentry_init_security(struct dentry *dentry, int mode,
Al Viro4f3ccd72016-07-20 16:06:15 -04002831 const struct qstr *name, void **ctx,
David Quigleyd47be3d2013-05-22 12:50:34 -04002832 u32 *ctxlen)
2833{
David Quigleyd47be3d2013-05-22 12:50:34 -04002834 u32 newsid;
2835 int rc;
2836
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002837 rc = selinux_determine_inode_label(selinux_cred(current_cred()),
Vivek Goyalc957f6d2016-07-13 10:44:51 -04002838 d_inode(dentry->d_parent), name,
David Howellsc3c188b2015-07-10 17:19:58 -04002839 inode_mode_to_security_class(mode),
2840 &newsid);
2841 if (rc)
2842 return rc;
David Quigleyd47be3d2013-05-22 12:50:34 -04002843
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002844 return security_sid_to_context(&selinux_state, newsid, (char **)ctx,
2845 ctxlen);
David Quigleyd47be3d2013-05-22 12:50:34 -04002846}
2847
Vivek Goyala518b0a2016-07-13 10:44:53 -04002848static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
2849 struct qstr *name,
2850 const struct cred *old,
2851 struct cred *new)
2852{
2853 u32 newsid;
2854 int rc;
2855 struct task_security_struct *tsec;
2856
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002857 rc = selinux_determine_inode_label(selinux_cred(old),
Vivek Goyala518b0a2016-07-13 10:44:53 -04002858 d_inode(dentry->d_parent), name,
2859 inode_mode_to_security_class(mode),
2860 &newsid);
2861 if (rc)
2862 return rc;
2863
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002864 tsec = selinux_cred(new);
Vivek Goyala518b0a2016-07-13 10:44:53 -04002865 tsec->create_sid = newsid;
2866 return 0;
2867}
2868
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002869static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +09002870 const struct qstr *qstr,
2871 const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -05002872 void **value, size_t *len)
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002873{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002874 const struct task_security_struct *tsec = selinux_cred(current_cred());
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002875 struct superblock_security_struct *sbsec;
Corentin LABBEc0d4f462017-10-04 20:32:17 +02002876 u32 newsid, clen;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002877 int rc;
Tetsuo Handa95489062013-07-25 05:44:02 +09002878 char *context;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002879
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002880 sbsec = dir->i_sb->s_security;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002881
David Howells275bb412008-11-14 10:39:19 +11002882 newsid = tsec->create_sid;
2883
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002884 rc = selinux_determine_inode_label(selinux_cred(current_cred()),
David Howellsc3c188b2015-07-10 17:19:58 -04002885 dir, qstr,
2886 inode_mode_to_security_class(inode->i_mode),
2887 &newsid);
2888 if (rc)
2889 return rc;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002890
Eric Paris296fddf2006-09-25 23:32:00 -07002891 /* Possibly defer initialization to selinux_complete_init. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05002892 if (sbsec->flags & SE_SBINITIALIZED) {
Casey Schaufler80788c22018-09-21 17:19:11 -07002893 struct inode_security_struct *isec = selinux_inode(inode);
Eric Paris296fddf2006-09-25 23:32:00 -07002894 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2895 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05002896 isec->initialized = LABEL_INITIALIZED;
Eric Paris296fddf2006-09-25 23:32:00 -07002897 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002898
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002899 if (!selinux_state.initialized || !(sbsec->flags & SBLABEL_MNT))
Stephen Smalley25a74f32005-11-08 21:34:33 -08002900 return -EOPNOTSUPP;
2901
Tetsuo Handa95489062013-07-25 05:44:02 +09002902 if (name)
2903 *name = XATTR_SELINUX_SUFFIX;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002904
2905 if (value && len) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002906 rc = security_sid_to_context_force(&selinux_state, newsid,
2907 &context, &clen);
Tetsuo Handa95489062013-07-25 05:44:02 +09002908 if (rc)
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002909 return rc;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002910 *value = context;
2911 *len = clen;
2912 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002913
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002914 return 0;
2915}
2916
Al Viro4acdaf22011-07-26 01:42:34 -04002917static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918{
2919 return may_create(dir, dentry, SECCLASS_FILE);
2920}
2921
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2923{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 return may_link(dir, old_dentry, MAY_LINK);
2925}
2926
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2928{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 return may_link(dir, dentry, MAY_UNLINK);
2930}
2931
2932static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2933{
2934 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2935}
2936
Al Viro18bb1db2011-07-26 01:41:39 -04002937static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938{
2939 return may_create(dir, dentry, SECCLASS_DIR);
2940}
2941
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2943{
2944 return may_link(dir, dentry, MAY_RMDIR);
2945}
2946
Al Viro1a67aaf2011-07-26 01:52:52 -04002947static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2950}
2951
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
Eric Paris828dfe12008-04-17 13:17:49 -04002953 struct inode *new_inode, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954{
2955 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2956}
2957
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958static int selinux_inode_readlink(struct dentry *dentry)
2959{
David Howells88e67f32008-11-14 10:39:21 +11002960 const struct cred *cred = current_cred();
2961
Eric Paris2875fa02011-04-28 16:04:24 -04002962 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963}
2964
NeilBrownbda0be72015-03-23 13:37:39 +11002965static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
2966 bool rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967{
David Howells88e67f32008-11-14 10:39:21 +11002968 const struct cred *cred = current_cred();
NeilBrownbda0be72015-03-23 13:37:39 +11002969 struct common_audit_data ad;
2970 struct inode_security_struct *isec;
2971 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
NeilBrownbda0be72015-03-23 13:37:39 +11002973 validate_creds(cred);
2974
2975 ad.type = LSM_AUDIT_DATA_DENTRY;
2976 ad.u.dentry = dentry;
2977 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05002978 isec = inode_security_rcu(inode, rcu);
2979 if (IS_ERR(isec))
2980 return PTR_ERR(isec);
NeilBrownbda0be72015-03-23 13:37:39 +11002981
Stephen Smalleye46e01e2018-12-12 10:10:56 -05002982 return avc_has_perm(&selinux_state,
2983 sid, isec->sid, isec->sclass, FILE__READ, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984}
2985
Eric Parisd4cf970d2012-04-04 15:01:42 -04002986static noinline int audit_inode_permission(struct inode *inode,
2987 u32 perms, u32 audited, u32 denied,
Stephen Smalley626b9742014-04-29 11:29:04 -07002988 int result,
Eric Parisd4cf970d2012-04-04 15:01:42 -04002989 unsigned flags)
2990{
2991 struct common_audit_data ad;
Casey Schaufler80788c22018-09-21 17:19:11 -07002992 struct inode_security_struct *isec = selinux_inode(inode);
Eric Parisd4cf970d2012-04-04 15:01:42 -04002993 int rc;
2994
Eric Paris50c205f2012-04-04 15:01:43 -04002995 ad.type = LSM_AUDIT_DATA_INODE;
Eric Parisd4cf970d2012-04-04 15:01:42 -04002996 ad.u.inode = inode;
2997
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002998 rc = slow_avc_audit(&selinux_state,
2999 current_sid(), isec->sid, isec->sclass, perms,
Stephen Smalley626b9742014-04-29 11:29:04 -07003000 audited, denied, result, &ad, flags);
Eric Parisd4cf970d2012-04-04 15:01:42 -04003001 if (rc)
3002 return rc;
3003 return 0;
3004}
3005
Al Viroe74f71e2011-06-20 19:38:15 -04003006static int selinux_inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007{
David Howells88e67f32008-11-14 10:39:21 +11003008 const struct cred *cred = current_cred();
Eric Parisb782e0a2010-07-23 11:44:03 -04003009 u32 perms;
3010 bool from_access;
Al Virocf1dd1d2011-06-20 19:44:08 -04003011 unsigned flags = mask & MAY_NOT_BLOCK;
Eric Paris2e334052012-04-04 15:01:42 -04003012 struct inode_security_struct *isec;
3013 u32 sid;
3014 struct av_decision avd;
3015 int rc, rc2;
3016 u32 audited, denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
Eric Parisb782e0a2010-07-23 11:44:03 -04003018 from_access = mask & MAY_ACCESS;
Eric Parisd09ca732010-07-23 11:43:57 -04003019 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
3020
Eric Parisb782e0a2010-07-23 11:44:03 -04003021 /* No permission to check. Existence test. */
3022 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
Eric Paris2e334052012-04-04 15:01:42 -04003025 validate_creds(cred);
Eric Parisb782e0a2010-07-23 11:44:03 -04003026
Eric Paris2e334052012-04-04 15:01:42 -04003027 if (unlikely(IS_PRIVATE(inode)))
3028 return 0;
Eric Parisb782e0a2010-07-23 11:44:03 -04003029
3030 perms = file_mask_to_av(inode->i_mode, mask);
3031
Eric Paris2e334052012-04-04 15:01:42 -04003032 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05003033 isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK);
3034 if (IS_ERR(isec))
3035 return PTR_ERR(isec);
Eric Paris2e334052012-04-04 15:01:42 -04003036
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003037 rc = avc_has_perm_noaudit(&selinux_state,
Stephen Smalley3a28cff2018-12-12 10:10:55 -05003038 sid, isec->sid, isec->sclass, perms,
3039 (flags & MAY_NOT_BLOCK) ? AVC_NONBLOCKING : 0,
3040 &avd);
Eric Paris2e334052012-04-04 15:01:42 -04003041 audited = avc_audit_required(perms, &avd, rc,
3042 from_access ? FILE__AUDIT_ACCESS : 0,
3043 &denied);
3044 if (likely(!audited))
3045 return rc;
3046
Stephen Smalley626b9742014-04-29 11:29:04 -07003047 rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags);
Eric Paris2e334052012-04-04 15:01:42 -04003048 if (rc2)
3049 return rc2;
3050 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051}
3052
3053static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
3054{
David Howells88e67f32008-11-14 10:39:21 +11003055 const struct cred *cred = current_cred();
Stephen Smalleyccb54472017-05-12 12:41:24 -04003056 struct inode *inode = d_backing_inode(dentry);
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003057 unsigned int ia_valid = iattr->ia_valid;
Eric Paris95dbf732012-04-04 13:45:34 -04003058 __u32 av = FILE__WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003060 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
3061 if (ia_valid & ATTR_FORCE) {
3062 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
3063 ATTR_FORCE);
3064 if (!ia_valid)
3065 return 0;
3066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003068 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3069 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
Eric Paris2875fa02011-04-28 16:04:24 -04003070 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003072 if (selinux_policycap_openperm() &&
Stephen Smalleyccb54472017-05-12 12:41:24 -04003073 inode->i_sb->s_magic != SOCKFS_MAGIC &&
3074 (ia_valid & ATTR_SIZE) &&
3075 !(ia_valid & ATTR_FILE))
Eric Paris95dbf732012-04-04 13:45:34 -04003076 av |= FILE__OPEN;
3077
3078 return dentry_has_perm(cred, dentry, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079}
3080
Al Viro3f7036a2015-03-08 19:28:30 -04003081static int selinux_inode_getattr(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082{
Al Viro3f7036a2015-03-08 19:28:30 -04003083 return path_has_perm(current_cred(), path, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084}
3085
Stephen Smalleydb590002017-04-20 11:31:30 -04003086static bool has_cap_mac_admin(bool audit)
3087{
3088 const struct cred *cred = current_cred();
Micah Mortonc1a85a02019-01-07 16:10:53 -08003089 unsigned int opts = audit ? CAP_OPT_NONE : CAP_OPT_NOAUDIT;
Stephen Smalleydb590002017-04-20 11:31:30 -04003090
Micah Mortonc1a85a02019-01-07 16:10:53 -08003091 if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, opts))
Stephen Smalleydb590002017-04-20 11:31:30 -04003092 return false;
Micah Mortonc1a85a02019-01-07 16:10:53 -08003093 if (cred_has_capability(cred, CAP_MAC_ADMIN, opts, true))
Stephen Smalleydb590002017-04-20 11:31:30 -04003094 return false;
3095 return true;
3096}
3097
David Howells8f0cfa52008-04-29 00:59:41 -07003098static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
3099 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100{
David Howellsc6f493d2015-03-17 22:26:22 +00003101 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003102 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 struct superblock_security_struct *sbsec;
Thomas Liu2bf49692009-07-14 12:14:09 -04003104 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11003105 u32 newsid, sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 int rc = 0;
3107
Eric W. Biederman6b240302017-10-02 09:38:20 -05003108 if (strcmp(name, XATTR_NAME_SELINUX)) {
3109 rc = cap_inode_setxattr(dentry, name, value, size, flags);
3110 if (rc)
3111 return rc;
3112
3113 /* Not an attribute we recognize, so just check the
3114 ordinary setattr permission. */
3115 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
3118 sbsec = inode->i_sb->s_security;
Eric Paris12f348b2012-10-09 10:56:25 -04003119 if (!(sbsec->flags & SBLABEL_MNT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 return -EOPNOTSUPP;
3121
Serge E. Hallyn2e149672011-03-23 16:43:26 -07003122 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 return -EPERM;
3124
Eric Paris50c205f2012-04-04 15:01:43 -04003125 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04003126 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127
Paul Moore20cdef82016-04-04 14:14:42 -04003128 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003129 rc = avc_has_perm(&selinux_state,
3130 sid, isec->sid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 FILE__RELABELFROM, &ad);
3132 if (rc)
3133 return rc;
3134
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003135 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3136 GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003137 if (rc == -EINVAL) {
Stephen Smalleydb590002017-04-20 11:31:30 -04003138 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04003139 struct audit_buffer *ab;
3140 size_t audit_size;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003141
3142 /* We strip a nul only if it is at the end, otherwise the
3143 * context contains a nul and we should audit that */
Al Viroe3fea3f2012-06-09 08:15:16 +01003144 if (value) {
Colin Ian Kingadd24372017-10-14 13:46:55 +01003145 const char *str = value;
3146
Al Viroe3fea3f2012-06-09 08:15:16 +01003147 if (str[size - 1] == '\0')
3148 audit_size = size - 1;
3149 else
3150 audit_size = size;
3151 } else {
Al Viroe3fea3f2012-06-09 08:15:16 +01003152 audit_size = 0;
3153 }
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04003154 ab = audit_log_start(audit_context(),
3155 GFP_ATOMIC, AUDIT_SELINUX_ERR);
Eric Parisd6ea83e2012-04-04 13:45:49 -04003156 audit_log_format(ab, "op=setxattr invalid_context=");
3157 audit_log_n_untrustedstring(ab, value, audit_size);
3158 audit_log_end(ab);
3159
Stephen Smalley12b29f32008-05-07 13:03:20 -04003160 return rc;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003161 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003162 rc = security_context_to_sid_force(&selinux_state, value,
3163 size, &newsid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 if (rc)
3166 return rc;
3167
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003168 rc = avc_has_perm(&selinux_state,
3169 sid, newsid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 FILE__RELABELTO, &ad);
3171 if (rc)
3172 return rc;
3173
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003174 rc = security_validate_transition(&selinux_state, isec->sid, newsid,
3175 sid, isec->sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 if (rc)
3177 return rc;
3178
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003179 return avc_has_perm(&selinux_state,
3180 newsid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 sbsec->sid,
3182 SECCLASS_FILESYSTEM,
3183 FILESYSTEM__ASSOCIATE,
3184 &ad);
3185}
3186
David Howells8f0cfa52008-04-29 00:59:41 -07003187static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
Eric Parisf5269712008-05-14 11:27:45 -04003188 const void *value, size_t size,
David Howells8f0cfa52008-04-29 00:59:41 -07003189 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190{
David Howellsc6f493d2015-03-17 22:26:22 +00003191 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003192 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 u32 newsid;
3194 int rc;
3195
3196 if (strcmp(name, XATTR_NAME_SELINUX)) {
3197 /* Not an attribute we recognize, so nothing to do. */
3198 return;
3199 }
3200
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003201 rc = security_context_to_sid_force(&selinux_state, value, size,
3202 &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +02003204 pr_err("SELinux: unable to map context to SID"
Stephen Smalley12b29f32008-05-07 13:03:20 -04003205 "for (%s, %lu), rc=%d\n",
3206 inode->i_sb->s_id, inode->i_ino, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 return;
3208 }
3209
Paul Moore20cdef82016-04-04 14:14:42 -04003210 isec = backing_inode_security(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003211 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003212 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003214 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003215 spin_unlock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003216
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 return;
3218}
3219
David Howells8f0cfa52008-04-29 00:59:41 -07003220static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221{
David Howells88e67f32008-11-14 10:39:21 +11003222 const struct cred *cred = current_cred();
3223
Eric Paris2875fa02011-04-28 16:04:24 -04003224 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225}
3226
Eric Paris828dfe12008-04-17 13:17:49 -04003227static int selinux_inode_listxattr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228{
David Howells88e67f32008-11-14 10:39:21 +11003229 const struct cred *cred = current_cred();
3230
Eric Paris2875fa02011-04-28 16:04:24 -04003231 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232}
3233
David Howells8f0cfa52008-04-29 00:59:41 -07003234static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235{
Eric W. Biederman6b240302017-10-02 09:38:20 -05003236 if (strcmp(name, XATTR_NAME_SELINUX)) {
3237 int rc = cap_inode_removexattr(dentry, name);
3238 if (rc)
3239 return rc;
3240
3241 /* Not an attribute we recognize, so just check the
3242 ordinary setattr permission. */
3243 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245
3246 /* No one is allowed to remove a SELinux security label.
3247 You can change the label, but all data must be labeled. */
3248 return -EACCES;
3249}
3250
James Morrisd381d8a2005-10-30 14:59:22 -08003251/*
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003252 * Copy the inode security context value to the user.
James Morrisd381d8a2005-10-30 14:59:22 -08003253 *
3254 * Permission check is handled by selinux_inode_getxattr hook.
3255 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05003256static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257{
David P. Quigley42492592008-02-04 22:29:39 -08003258 u32 size;
3259 int error;
3260 char *context = NULL;
Paul Moore20cdef82016-04-04 14:14:42 -04003261 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00003263 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3264 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003266 /*
3267 * If the caller has CAP_MAC_ADMIN, then get the raw context
3268 * value even if it is not defined by current policy; otherwise,
3269 * use the in-core value under current policy.
3270 * Use the non-auditing forms of the permission checks since
3271 * getxattr may be called by unprivileged processes commonly
3272 * and lack of permission just means that we fall back to the
3273 * in-core context value, not a denial.
3274 */
Paul Moore20cdef82016-04-04 14:14:42 -04003275 isec = inode_security(inode);
Stephen Smalleydb590002017-04-20 11:31:30 -04003276 if (has_cap_mac_admin(false))
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003277 error = security_sid_to_context_force(&selinux_state,
3278 isec->sid, &context,
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003279 &size);
3280 else
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003281 error = security_sid_to_context(&selinux_state, isec->sid,
3282 &context, &size);
David P. Quigley42492592008-02-04 22:29:39 -08003283 if (error)
3284 return error;
3285 error = size;
3286 if (alloc) {
3287 *buffer = context;
3288 goto out_nofree;
3289 }
3290 kfree(context);
3291out_nofree:
3292 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293}
3294
3295static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Eric Paris828dfe12008-04-17 13:17:49 -04003296 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297{
Paul Moore2c971652016-04-19 16:36:28 -04003298 struct inode_security_struct *isec = inode_security_novalidate(inode);
Ondrej Mosnacek53e0c2a2018-12-21 21:18:53 +01003299 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 u32 newsid;
3301 int rc;
3302
3303 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3304 return -EOPNOTSUPP;
3305
Ondrej Mosnacek53e0c2a2018-12-21 21:18:53 +01003306 if (!(sbsec->flags & SBLABEL_MNT))
3307 return -EOPNOTSUPP;
3308
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 if (!value || !size)
3310 return -EACCES;
3311
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003312 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3313 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 if (rc)
3315 return rc;
3316
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003317 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003318 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003320 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003321 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 return 0;
3323}
3324
3325static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3326{
3327 const int len = sizeof(XATTR_NAME_SELINUX);
3328 if (buffer && len <= buffer_size)
3329 memcpy(buffer, XATTR_NAME_SELINUX, len);
3330 return len;
3331}
3332
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05003333static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003334{
Andreas Gruenbachere817c2f2016-02-18 12:04:08 +01003335 struct inode_security_struct *isec = inode_security_novalidate(inode);
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003336 *secid = isec->sid;
3337}
3338
Vivek Goyal56909eb2016-07-13 10:44:48 -04003339static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
3340{
3341 u32 sid;
3342 struct task_security_struct *tsec;
3343 struct cred *new_creds = *new;
3344
3345 if (new_creds == NULL) {
3346 new_creds = prepare_creds();
3347 if (!new_creds)
3348 return -ENOMEM;
3349 }
3350
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07003351 tsec = selinux_cred(new_creds);
Vivek Goyal56909eb2016-07-13 10:44:48 -04003352 /* Get label from overlay inode and set it in create_sid */
3353 selinux_inode_getsecid(d_inode(src), &sid);
3354 tsec->create_sid = sid;
3355 *new = new_creds;
3356 return 0;
3357}
3358
Vivek Goyal19472b62016-07-13 10:44:50 -04003359static int selinux_inode_copy_up_xattr(const char *name)
3360{
3361 /* The copy_up hook above sets the initial context on an inode, but we
3362 * don't then want to overwrite it by blindly copying all the lower
3363 * xattrs up. Instead, we have to filter out SELinux-related xattrs.
3364 */
3365 if (strcmp(name, XATTR_NAME_SELINUX) == 0)
3366 return 1; /* Discard */
3367 /*
3368 * Any other attribute apart from SELINUX is not claimed, supported
3369 * by selinux.
3370 */
3371 return -EOPNOTSUPP;
3372}
3373
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374/* file security operations */
3375
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003376static int selinux_revalidate_file_permission(struct file *file, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377{
David Howells88e67f32008-11-14 10:39:21 +11003378 const struct cred *cred = current_cred();
Al Viro496ad9a2013-01-23 17:07:38 -05003379 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3382 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3383 mask |= MAY_APPEND;
3384
Paul Moore389fb8002009-03-27 17:10:34 -04003385 return file_has_perm(cred, file,
3386 file_mask_to_av(inode->i_mode, mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387}
3388
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003389static int selinux_file_permission(struct file *file, int mask)
3390{
Al Viro496ad9a2013-01-23 17:07:38 -05003391 struct inode *inode = file_inode(file);
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003392 struct file_security_struct *fsec = selinux_file(file);
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003393 struct inode_security_struct *isec;
Stephen Smalley20dda182009-06-22 14:54:53 -04003394 u32 sid = current_sid();
3395
Paul Moore389fb8002009-03-27 17:10:34 -04003396 if (!mask)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003397 /* No permission to check. Existence test. */
3398 return 0;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003399
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003400 isec = inode_security(inode);
Stephen Smalley20dda182009-06-22 14:54:53 -04003401 if (sid == fsec->sid && fsec->isid == isec->sid &&
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003402 fsec->pseqno == avc_policy_seqno(&selinux_state))
Eric Paris83d49852012-04-04 13:45:40 -04003403 /* No change since file_open check. */
Stephen Smalley20dda182009-06-22 14:54:53 -04003404 return 0;
3405
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003406 return selinux_revalidate_file_permission(file, mask);
3407}
3408
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409static int selinux_file_alloc_security(struct file *file)
3410{
3411 return file_alloc_security(file);
3412}
3413
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003414/*
3415 * Check whether a task has the ioctl permission and cmd
3416 * operation to an inode.
3417 */
Geliang Tang1d2a1682015-10-21 17:44:27 -04003418static int ioctl_has_perm(const struct cred *cred, struct file *file,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003419 u32 requested, u16 cmd)
3420{
3421 struct common_audit_data ad;
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003422 struct file_security_struct *fsec = selinux_file(file);
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003423 struct inode *inode = file_inode(file);
Paul Moore20cdef82016-04-04 14:14:42 -04003424 struct inode_security_struct *isec;
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003425 struct lsm_ioctlop_audit ioctl;
3426 u32 ssid = cred_sid(cred);
3427 int rc;
3428 u8 driver = cmd >> 8;
3429 u8 xperm = cmd & 0xff;
3430
3431 ad.type = LSM_AUDIT_DATA_IOCTL_OP;
3432 ad.u.op = &ioctl;
3433 ad.u.op->cmd = cmd;
3434 ad.u.op->path = file->f_path;
3435
3436 if (ssid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003437 rc = avc_has_perm(&selinux_state,
3438 ssid, fsec->sid,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003439 SECCLASS_FD,
3440 FD__USE,
3441 &ad);
3442 if (rc)
3443 goto out;
3444 }
3445
3446 if (unlikely(IS_PRIVATE(inode)))
3447 return 0;
3448
Paul Moore20cdef82016-04-04 14:14:42 -04003449 isec = inode_security(inode);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003450 rc = avc_has_extended_perms(&selinux_state,
3451 ssid, isec->sid, isec->sclass,
3452 requested, driver, xperm, &ad);
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003453out:
3454 return rc;
3455}
3456
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3458 unsigned long arg)
3459{
David Howells88e67f32008-11-14 10:39:21 +11003460 const struct cred *cred = current_cred();
Eric Paris0b24dcb2011-02-25 15:39:20 -05003461 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462
Eric Paris0b24dcb2011-02-25 15:39:20 -05003463 switch (cmd) {
3464 case FIONREAD:
3465 /* fall through */
3466 case FIBMAP:
3467 /* fall through */
3468 case FIGETBSZ:
3469 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003470 case FS_IOC_GETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003471 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003472 case FS_IOC_GETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003473 error = file_has_perm(cred, file, FILE__GETATTR);
3474 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475
Al Viro2f99c362012-03-23 16:04:05 -04003476 case FS_IOC_SETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003477 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003478 case FS_IOC_SETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003479 error = file_has_perm(cred, file, FILE__SETATTR);
3480 break;
3481
3482 /* sys_ioctl() checks */
3483 case FIONBIO:
3484 /* fall through */
3485 case FIOASYNC:
3486 error = file_has_perm(cred, file, 0);
3487 break;
3488
3489 case KDSKBENT:
3490 case KDSKBSENT:
Eric Paris6a9de492012-01-03 12:25:14 -05003491 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
Micah Mortonc1a85a02019-01-07 16:10:53 -08003492 CAP_OPT_NONE, true);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003493 break;
3494
3495 /* default case assumes that the command will go
3496 * to the file's ioctl() function.
3497 */
3498 default:
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003499 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003500 }
3501 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502}
3503
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003504static int default_noexec;
3505
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3507{
David Howells88e67f32008-11-14 10:39:21 +11003508 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003509 u32 sid = cred_sid(cred);
David Howellsd84f4f92008-11-14 10:39:23 +11003510 int rc = 0;
David Howells88e67f32008-11-14 10:39:21 +11003511
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003512 if (default_noexec &&
Stephen Smalley892e8ca2015-07-10 09:40:59 -04003513 (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
3514 (!shared && (prot & PROT_WRITE)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 /*
3516 * We are making executable an anonymous mapping or a
3517 * private file mapping that will also be writable.
3518 * This has an additional check.
3519 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003520 rc = avc_has_perm(&selinux_state,
3521 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003522 PROCESS__EXECMEM, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 if (rc)
David Howellsd84f4f92008-11-14 10:39:23 +11003524 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526
3527 if (file) {
3528 /* read access is always possible with a mapping */
3529 u32 av = FILE__READ;
3530
3531 /* write access only matters if the mapping is shared */
3532 if (shared && (prot & PROT_WRITE))
3533 av |= FILE__WRITE;
3534
3535 if (prot & PROT_EXEC)
3536 av |= FILE__EXECUTE;
3537
David Howells88e67f32008-11-14 10:39:21 +11003538 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 }
David Howellsd84f4f92008-11-14 10:39:23 +11003540
3541error:
3542 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543}
3544
Al Viroe5467852012-05-30 13:30:51 -04003545static int selinux_mmap_addr(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07003547 int rc = 0;
Paul Moore98883bf2014-03-19 16:46:11 -04003548
3549 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
3550 u32 sid = current_sid();
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003551 rc = avc_has_perm(&selinux_state,
3552 sid, sid, SECCLASS_MEMPROTECT,
Paul Moore98883bf2014-03-19 16:46:11 -04003553 MEMPROTECT__MMAP_ZERO, NULL);
3554 }
3555
3556 return rc;
Al Viroe5467852012-05-30 13:30:51 -04003557}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558
Al Viroe5467852012-05-30 13:30:51 -04003559static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3560 unsigned long prot, unsigned long flags)
3561{
Stephen Smalley3ba4bf52017-05-05 09:14:48 -04003562 struct common_audit_data ad;
3563 int rc;
3564
3565 if (file) {
3566 ad.type = LSM_AUDIT_DATA_FILE;
3567 ad.u.file = file;
3568 rc = inode_has_perm(current_cred(), file_inode(file),
3569 FILE__MAP, &ad);
3570 if (rc)
3571 return rc;
3572 }
3573
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003574 if (selinux_state.checkreqprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 prot = reqprot;
3576
3577 return file_map_prot_check(file, prot,
3578 (flags & MAP_TYPE) == MAP_SHARED);
3579}
3580
3581static int selinux_file_mprotect(struct vm_area_struct *vma,
3582 unsigned long reqprot,
3583 unsigned long prot)
3584{
David Howells88e67f32008-11-14 10:39:21 +11003585 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003586 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003588 if (selinux_state.checkreqprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 prot = reqprot;
3590
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003591 if (default_noexec &&
3592 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
James Morrisd541bbe2009-01-29 12:19:51 +11003593 int rc = 0;
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003594 if (vma->vm_start >= vma->vm_mm->start_brk &&
3595 vma->vm_end <= vma->vm_mm->brk) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003596 rc = avc_has_perm(&selinux_state,
3597 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003598 PROCESS__EXECHEAP, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003599 } else if (!vma->vm_file &&
Stephen Smalleyc2316db2016-04-08 13:55:03 -04003600 ((vma->vm_start <= vma->vm_mm->start_stack &&
3601 vma->vm_end >= vma->vm_mm->start_stack) ||
Andy Lutomirskid17af502016-09-30 10:58:58 -07003602 vma_is_stack_for_current(vma))) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003603 rc = avc_has_perm(&selinux_state,
3604 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003605 PROCESS__EXECSTACK, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003606 } else if (vma->vm_file && vma->anon_vma) {
3607 /*
3608 * We are making executable a file mapping that has
3609 * had some COW done. Since pages might have been
3610 * written, check ability to execute the possibly
3611 * modified content. This typically should only
3612 * occur for text relocations.
3613 */
David Howellsd84f4f92008-11-14 10:39:23 +11003614 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003615 }
Lorenzo Hernandez García-Hierro6b992192005-06-25 14:54:34 -07003616 if (rc)
3617 return rc;
3618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619
3620 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3621}
3622
3623static int selinux_file_lock(struct file *file, unsigned int cmd)
3624{
David Howells88e67f32008-11-14 10:39:21 +11003625 const struct cred *cred = current_cred();
3626
3627 return file_has_perm(cred, file, FILE__LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628}
3629
3630static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3631 unsigned long arg)
3632{
David Howells88e67f32008-11-14 10:39:21 +11003633 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 int err = 0;
3635
3636 switch (cmd) {
Eric Paris828dfe12008-04-17 13:17:49 -04003637 case F_SETFL:
Eric Paris828dfe12008-04-17 13:17:49 -04003638 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
David Howells88e67f32008-11-14 10:39:21 +11003639 err = file_has_perm(cred, file, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003641 }
3642 /* fall through */
3643 case F_SETOWN:
3644 case F_SETSIG:
3645 case F_GETFL:
3646 case F_GETOWN:
3647 case F_GETSIG:
Cyrill Gorcunov1d151c32012-07-30 14:43:00 -07003648 case F_GETOWNER_UIDS:
Eric Paris828dfe12008-04-17 13:17:49 -04003649 /* Just check FD__USE permission */
David Howells88e67f32008-11-14 10:39:21 +11003650 err = file_has_perm(cred, file, 0);
Eric Paris828dfe12008-04-17 13:17:49 -04003651 break;
3652 case F_GETLK:
3653 case F_SETLK:
3654 case F_SETLKW:
Jeff Layton0d3f7a22014-04-22 08:23:58 -04003655 case F_OFD_GETLK:
3656 case F_OFD_SETLK:
3657 case F_OFD_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658#if BITS_PER_LONG == 32
Eric Paris828dfe12008-04-17 13:17:49 -04003659 case F_GETLK64:
3660 case F_SETLK64:
3661 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662#endif
David Howells88e67f32008-11-14 10:39:21 +11003663 err = file_has_perm(cred, file, FILE__LOCK);
Eric Paris828dfe12008-04-17 13:17:49 -04003664 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 }
3666
3667 return err;
3668}
3669
Jeff Laytone0b93ed2014-08-22 11:27:32 -04003670static void selinux_file_set_fowner(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 struct file_security_struct *fsec;
3673
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003674 fsec = selinux_file(file);
David Howells275bb412008-11-14 10:39:19 +11003675 fsec->fown_sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676}
3677
3678static int selinux_file_send_sigiotask(struct task_struct *tsk,
3679 struct fown_struct *fown, int signum)
3680{
Eric Paris828dfe12008-04-17 13:17:49 -04003681 struct file *file;
Stephen Smalley65c90bc2009-05-04 15:43:18 -04003682 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 struct file_security_struct *fsec;
3685
3686 /* struct fown_struct is never outside the context of a struct file */
Eric Paris828dfe12008-04-17 13:17:49 -04003687 file = container_of(fown, struct file, f_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003689 fsec = selinux_file(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690
3691 if (!signum)
3692 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3693 else
3694 perm = signal_to_av(signum);
3695
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003696 return avc_has_perm(&selinux_state,
3697 fsec->fown_sid, sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 SECCLASS_PROCESS, perm, NULL);
3699}
3700
3701static int selinux_file_receive(struct file *file)
3702{
David Howells88e67f32008-11-14 10:39:21 +11003703 const struct cred *cred = current_cred();
3704
3705 return file_has_perm(cred, file, file_to_av(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706}
3707
Al Viro94817692018-07-10 14:13:18 -04003708static int selinux_file_open(struct file *file)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003709{
3710 struct file_security_struct *fsec;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003711 struct inode_security_struct *isec;
David Howellsd84f4f92008-11-14 10:39:23 +11003712
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003713 fsec = selinux_file(file);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003714 isec = inode_security(file_inode(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003715 /*
3716 * Save inode label and policy sequence number
3717 * at open-time so that selinux_file_permission
3718 * can determine whether revalidation is necessary.
3719 * Task label is already saved in the file security
3720 * struct as its SID.
3721 */
3722 fsec->isid = isec->sid;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003723 fsec->pseqno = avc_policy_seqno(&selinux_state);
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003724 /*
3725 * Since the inode label or policy seqno may have changed
3726 * between the selinux_inode_permission check and the saving
3727 * of state above, recheck that access is still permitted.
3728 * Otherwise, access might never be revalidated against the
3729 * new inode label or new policy.
3730 * This check is not redundant - do not remove.
3731 */
Al Viro94817692018-07-10 14:13:18 -04003732 return file_path_has_perm(file->f_cred, file, open_file_to_av(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003733}
3734
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735/* task security operations */
3736
Tetsuo Handaa79be232017-03-28 23:08:45 +09003737static int selinux_task_alloc(struct task_struct *task,
3738 unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003740 u32 sid = current_sid();
3741
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003742 return avc_has_perm(&selinux_state,
3743 sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744}
3745
David Howellsf1752ee2008-11-14 10:39:17 +11003746/*
David Howellsd84f4f92008-11-14 10:39:23 +11003747 * prepare a new set of credentials for modification
3748 */
3749static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3750 gfp_t gfp)
3751{
Casey Schauflerbbd36622018-11-12 09:30:56 -08003752 const struct task_security_struct *old_tsec = selinux_cred(old);
3753 struct task_security_struct *tsec = selinux_cred(new);
David Howellsd84f4f92008-11-14 10:39:23 +11003754
Casey Schauflerbbd36622018-11-12 09:30:56 -08003755 *tsec = *old_tsec;
David Howellsd84f4f92008-11-14 10:39:23 +11003756 return 0;
3757}
3758
3759/*
David Howellsee18d642009-09-02 09:14:21 +01003760 * transfer the SELinux data to a blank set of creds
3761 */
3762static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3763{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07003764 const struct task_security_struct *old_tsec = selinux_cred(old);
3765 struct task_security_struct *tsec = selinux_cred(new);
David Howellsee18d642009-09-02 09:14:21 +01003766
3767 *tsec = *old_tsec;
3768}
3769
Matthew Garrett3ec30112018-01-08 13:36:19 -08003770static void selinux_cred_getsecid(const struct cred *c, u32 *secid)
3771{
3772 *secid = cred_sid(c);
3773}
3774
David Howellsee18d642009-09-02 09:14:21 +01003775/*
David Howells3a3b7ce2008-11-14 10:39:28 +11003776 * set the security data for a kernel service
3777 * - all the creation contexts are set to unlabelled
3778 */
3779static int selinux_kernel_act_as(struct cred *new, u32 secid)
3780{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07003781 struct task_security_struct *tsec = selinux_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11003782 u32 sid = current_sid();
3783 int ret;
3784
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003785 ret = avc_has_perm(&selinux_state,
3786 sid, secid,
David Howells3a3b7ce2008-11-14 10:39:28 +11003787 SECCLASS_KERNEL_SERVICE,
3788 KERNEL_SERVICE__USE_AS_OVERRIDE,
3789 NULL);
3790 if (ret == 0) {
3791 tsec->sid = secid;
3792 tsec->create_sid = 0;
3793 tsec->keycreate_sid = 0;
3794 tsec->sockcreate_sid = 0;
3795 }
3796 return ret;
3797}
3798
3799/*
3800 * set the file creation context in a security record to the same as the
3801 * objective context of the specified inode
3802 */
3803static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3804{
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003805 struct inode_security_struct *isec = inode_security(inode);
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07003806 struct task_security_struct *tsec = selinux_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11003807 u32 sid = current_sid();
3808 int ret;
3809
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003810 ret = avc_has_perm(&selinux_state,
3811 sid, isec->sid,
David Howells3a3b7ce2008-11-14 10:39:28 +11003812 SECCLASS_KERNEL_SERVICE,
3813 KERNEL_SERVICE__CREATE_FILES_AS,
3814 NULL);
3815
3816 if (ret == 0)
3817 tsec->create_sid = isec->sid;
David Howellsef574712010-02-26 01:56:16 +00003818 return ret;
David Howells3a3b7ce2008-11-14 10:39:28 +11003819}
3820
Eric Parisdd8dbf22009-11-03 16:35:32 +11003821static int selinux_kernel_module_request(char *kmod_name)
Eric Paris25354c42009-08-13 09:45:03 -04003822{
Eric Parisdd8dbf22009-11-03 16:35:32 +11003823 struct common_audit_data ad;
3824
Eric Paris50c205f2012-04-04 15:01:43 -04003825 ad.type = LSM_AUDIT_DATA_KMOD;
Eric Parisdd8dbf22009-11-03 16:35:32 +11003826 ad.u.kmod_name = kmod_name;
3827
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003828 return avc_has_perm(&selinux_state,
3829 current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM,
Eric Parisdd8dbf22009-11-03 16:35:32 +11003830 SYSTEM__MODULE_REQUEST, &ad);
Eric Paris25354c42009-08-13 09:45:03 -04003831}
3832
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003833static int selinux_kernel_module_from_file(struct file *file)
3834{
3835 struct common_audit_data ad;
3836 struct inode_security_struct *isec;
3837 struct file_security_struct *fsec;
3838 u32 sid = current_sid();
3839 int rc;
3840
3841 /* init_module */
3842 if (file == NULL)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003843 return avc_has_perm(&selinux_state,
3844 sid, sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003845 SYSTEM__MODULE_LOAD, NULL);
3846
3847 /* finit_module */
Paul Moore20cdef82016-04-04 14:14:42 -04003848
Vivek Goyal43af5de2016-09-09 11:37:49 -04003849 ad.type = LSM_AUDIT_DATA_FILE;
3850 ad.u.file = file;
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003851
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003852 fsec = selinux_file(file);
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003853 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003854 rc = avc_has_perm(&selinux_state,
3855 sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003856 if (rc)
3857 return rc;
3858 }
3859
Paul Moore20cdef82016-04-04 14:14:42 -04003860 isec = inode_security(file_inode(file));
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003861 return avc_has_perm(&selinux_state,
3862 sid, isec->sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003863 SYSTEM__MODULE_LOAD, &ad);
3864}
3865
3866static int selinux_kernel_read_file(struct file *file,
3867 enum kernel_read_file_id id)
3868{
3869 int rc = 0;
3870
3871 switch (id) {
3872 case READING_MODULE:
3873 rc = selinux_kernel_module_from_file(file);
3874 break;
3875 default:
3876 break;
3877 }
3878
3879 return rc;
3880}
3881
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04003882static int selinux_kernel_load_data(enum kernel_load_data_id id)
3883{
3884 int rc = 0;
3885
3886 switch (id) {
3887 case LOADING_MODULE:
3888 rc = selinux_kernel_module_from_file(NULL);
3889 default:
3890 break;
3891 }
3892
3893 return rc;
3894}
3895
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3897{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003898 return avc_has_perm(&selinux_state,
3899 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003900 PROCESS__SETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901}
3902
3903static int selinux_task_getpgid(struct task_struct *p)
3904{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003905 return avc_has_perm(&selinux_state,
3906 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003907 PROCESS__GETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908}
3909
3910static int selinux_task_getsid(struct task_struct *p)
3911{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003912 return avc_has_perm(&selinux_state,
3913 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003914 PROCESS__GETSESSION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915}
3916
David Quigleyf9008e4c2006-06-30 01:55:46 -07003917static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3918{
David Howells275bb412008-11-14 10:39:19 +11003919 *secid = task_sid(p);
David Quigleyf9008e4c2006-06-30 01:55:46 -07003920}
3921
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922static int selinux_task_setnice(struct task_struct *p, int nice)
3923{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003924 return avc_has_perm(&selinux_state,
3925 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003926 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927}
3928
James Morris03e68062006-06-23 02:03:58 -07003929static int selinux_task_setioprio(struct task_struct *p, int ioprio)
3930{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003931 return avc_has_perm(&selinux_state,
3932 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003933 PROCESS__SETSCHED, NULL);
James Morris03e68062006-06-23 02:03:58 -07003934}
3935
David Quigleya1836a42006-06-30 01:55:49 -07003936static int selinux_task_getioprio(struct task_struct *p)
3937{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003938 return avc_has_perm(&selinux_state,
3939 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003940 PROCESS__GETSCHED, NULL);
David Quigleya1836a42006-06-30 01:55:49 -07003941}
3942
Corentin LABBE42985552017-10-04 20:32:18 +02003943static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred,
3944 unsigned int flags)
Stephen Smalley791ec492017-02-17 07:57:00 -05003945{
3946 u32 av = 0;
3947
Stephen Smalley84e68852017-02-28 09:35:08 -05003948 if (!flags)
3949 return 0;
Stephen Smalley791ec492017-02-17 07:57:00 -05003950 if (flags & LSM_PRLIMIT_WRITE)
3951 av |= PROCESS__SETRLIMIT;
3952 if (flags & LSM_PRLIMIT_READ)
3953 av |= PROCESS__GETRLIMIT;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003954 return avc_has_perm(&selinux_state,
3955 cred_sid(cred), cred_sid(tcred),
Stephen Smalley791ec492017-02-17 07:57:00 -05003956 SECCLASS_PROCESS, av, NULL);
3957}
3958
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003959static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
3960 struct rlimit *new_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961{
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003962 struct rlimit *old_rlim = p->signal->rlim + resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963
3964 /* Control the ability to change the hard limit (whether
3965 lowering or raising it), so that the hard limit can
3966 later be used as a safe reset point for the soft limit
David Howellsd84f4f92008-11-14 10:39:23 +11003967 upon context transitions. See selinux_bprm_committing_creds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 if (old_rlim->rlim_max != new_rlim->rlim_max)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003969 return avc_has_perm(&selinux_state,
3970 current_sid(), task_sid(p),
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003971 SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972
3973 return 0;
3974}
3975
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003976static int selinux_task_setscheduler(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003978 return avc_has_perm(&selinux_state,
3979 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003980 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981}
3982
3983static int selinux_task_getscheduler(struct task_struct *p)
3984{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003985 return avc_has_perm(&selinux_state,
3986 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003987 PROCESS__GETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988}
3989
David Quigley35601542006-06-23 02:04:01 -07003990static int selinux_task_movememory(struct task_struct *p)
3991{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003992 return avc_has_perm(&selinux_state,
3993 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003994 PROCESS__SETSCHED, NULL);
David Quigley35601542006-06-23 02:04:01 -07003995}
3996
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02003997static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04003998 int sig, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999{
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004000 u32 secid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 if (!sig)
4004 perm = PROCESS__SIGNULL; /* null signal; existence test */
4005 else
4006 perm = signal_to_av(sig);
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004007 if (!cred)
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004008 secid = current_sid();
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004009 else
4010 secid = cred_sid(cred);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004011 return avc_has_perm(&selinux_state,
4012 secid, task_sid(p), SECCLASS_PROCESS, perm, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013}
4014
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015static void selinux_task_to_inode(struct task_struct *p,
4016 struct inode *inode)
4017{
Casey Schaufler80788c22018-09-21 17:19:11 -07004018 struct inode_security_struct *isec = selinux_inode(inode);
David Howells275bb412008-11-14 10:39:19 +11004019 u32 sid = task_sid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004021 spin_lock(&isec->lock);
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01004022 isec->sclass = inode_mode_to_security_class(inode->i_mode);
David Howells275bb412008-11-14 10:39:19 +11004023 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004024 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004025 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026}
4027
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004029static int selinux_parse_skb_ipv4(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004030 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031{
4032 int offset, ihlen, ret = -EINVAL;
4033 struct iphdr _iph, *ih;
4034
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004035 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
4037 if (ih == NULL)
4038 goto out;
4039
4040 ihlen = ih->ihl * 4;
4041 if (ihlen < sizeof(_iph))
4042 goto out;
4043
Eric Paris48c62af2012-04-02 13:15:44 -04004044 ad->u.net->v4info.saddr = ih->saddr;
4045 ad->u.net->v4info.daddr = ih->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 ret = 0;
4047
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004048 if (proto)
4049 *proto = ih->protocol;
4050
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 switch (ih->protocol) {
Eric Paris828dfe12008-04-17 13:17:49 -04004052 case IPPROTO_TCP: {
4053 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054
Eric Paris828dfe12008-04-17 13:17:49 -04004055 if (ntohs(ih->frag_off) & IP_OFFSET)
4056 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057
4058 offset += ihlen;
4059 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4060 if (th == NULL)
4061 break;
4062
Eric Paris48c62af2012-04-02 13:15:44 -04004063 ad->u.net->sport = th->source;
4064 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067
Eric Paris828dfe12008-04-17 13:17:49 -04004068 case IPPROTO_UDP: {
4069 struct udphdr _udph, *uh;
4070
4071 if (ntohs(ih->frag_off) & IP_OFFSET)
4072 break;
4073
4074 offset += ihlen;
4075 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4076 if (uh == NULL)
4077 break;
4078
Eric Paris48c62af2012-04-02 13:15:44 -04004079 ad->u.net->sport = uh->source;
4080 ad->u.net->dport = uh->dest;
Eric Paris828dfe12008-04-17 13:17:49 -04004081 break;
4082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083
James Morris2ee92d42006-11-13 16:09:01 -08004084 case IPPROTO_DCCP: {
4085 struct dccp_hdr _dccph, *dh;
4086
4087 if (ntohs(ih->frag_off) & IP_OFFSET)
4088 break;
4089
4090 offset += ihlen;
4091 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4092 if (dh == NULL)
4093 break;
4094
Eric Paris48c62af2012-04-02 13:15:44 -04004095 ad->u.net->sport = dh->dccph_sport;
4096 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004097 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004098 }
James Morris2ee92d42006-11-13 16:09:01 -08004099
Richard Hainesd4529302018-02-13 20:57:18 +00004100#if IS_ENABLED(CONFIG_IP_SCTP)
4101 case IPPROTO_SCTP: {
4102 struct sctphdr _sctph, *sh;
4103
4104 if (ntohs(ih->frag_off) & IP_OFFSET)
4105 break;
4106
4107 offset += ihlen;
4108 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4109 if (sh == NULL)
4110 break;
4111
4112 ad->u.net->sport = sh->source;
4113 ad->u.net->dport = sh->dest;
4114 break;
4115 }
4116#endif
Eric Paris828dfe12008-04-17 13:17:49 -04004117 default:
4118 break;
4119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120out:
4121 return ret;
4122}
4123
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004124#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125
4126/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004127static int selinux_parse_skb_ipv6(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004128 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129{
4130 u8 nexthdr;
4131 int ret = -EINVAL, offset;
4132 struct ipv6hdr _ipv6h, *ip6;
Jesse Gross75f28112011-11-30 17:05:51 -08004133 __be16 frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004135 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
4137 if (ip6 == NULL)
4138 goto out;
4139
Eric Paris48c62af2012-04-02 13:15:44 -04004140 ad->u.net->v6info.saddr = ip6->saddr;
4141 ad->u.net->v6info.daddr = ip6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 ret = 0;
4143
4144 nexthdr = ip6->nexthdr;
4145 offset += sizeof(_ipv6h);
Jesse Gross75f28112011-11-30 17:05:51 -08004146 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 if (offset < 0)
4148 goto out;
4149
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004150 if (proto)
4151 *proto = nexthdr;
4152
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 switch (nexthdr) {
4154 case IPPROTO_TCP: {
Eric Paris828dfe12008-04-17 13:17:49 -04004155 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156
4157 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4158 if (th == NULL)
4159 break;
4160
Eric Paris48c62af2012-04-02 13:15:44 -04004161 ad->u.net->sport = th->source;
4162 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 break;
4164 }
4165
4166 case IPPROTO_UDP: {
4167 struct udphdr _udph, *uh;
4168
4169 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4170 if (uh == NULL)
4171 break;
4172
Eric Paris48c62af2012-04-02 13:15:44 -04004173 ad->u.net->sport = uh->source;
4174 ad->u.net->dport = uh->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 break;
4176 }
4177
James Morris2ee92d42006-11-13 16:09:01 -08004178 case IPPROTO_DCCP: {
4179 struct dccp_hdr _dccph, *dh;
4180
4181 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4182 if (dh == NULL)
4183 break;
4184
Eric Paris48c62af2012-04-02 13:15:44 -04004185 ad->u.net->sport = dh->dccph_sport;
4186 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004187 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004188 }
James Morris2ee92d42006-11-13 16:09:01 -08004189
Richard Hainesd4529302018-02-13 20:57:18 +00004190#if IS_ENABLED(CONFIG_IP_SCTP)
4191 case IPPROTO_SCTP: {
4192 struct sctphdr _sctph, *sh;
4193
4194 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4195 if (sh == NULL)
4196 break;
4197
4198 ad->u.net->sport = sh->source;
4199 ad->u.net->dport = sh->dest;
4200 break;
4201 }
4202#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 /* includes fragments */
4204 default:
4205 break;
4206 }
4207out:
4208 return ret;
4209}
4210
4211#endif /* IPV6 */
4212
Thomas Liu2bf49692009-07-14 12:14:09 -04004213static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
David Howellscf9481e2008-07-27 21:31:07 +10004214 char **_addrp, int src, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215{
David Howellscf9481e2008-07-27 21:31:07 +10004216 char *addrp;
4217 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218
Eric Paris48c62af2012-04-02 13:15:44 -04004219 switch (ad->u.net->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 case PF_INET:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004221 ret = selinux_parse_skb_ipv4(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004222 if (ret)
4223 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004224 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
4225 &ad->u.net->v4info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004226 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004228#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 case PF_INET6:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004230 ret = selinux_parse_skb_ipv6(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004231 if (ret)
4232 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004233 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
4234 &ad->u.net->v6info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004235 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236#endif /* IPV6 */
4237 default:
David Howellscf9481e2008-07-27 21:31:07 +10004238 addrp = NULL;
4239 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 }
4241
David Howellscf9481e2008-07-27 21:31:07 +10004242parse_error:
peter enderborgc103a912018-06-12 10:09:03 +02004243 pr_warn(
David Howellscf9481e2008-07-27 21:31:07 +10004244 "SELinux: failure in selinux_parse_skb(),"
4245 " unable to parse packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 return ret;
David Howellscf9481e2008-07-27 21:31:07 +10004247
4248okay:
4249 if (_addrp)
4250 *_addrp = addrp;
4251 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252}
4253
Paul Moore4f6a9932007-03-01 14:35:22 -05004254/**
Paul Moore220deb92008-01-29 08:38:23 -05004255 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
Paul Moore4f6a9932007-03-01 14:35:22 -05004256 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05004257 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -05004258 * @sid: the packet's peer label SID
Paul Moore4f6a9932007-03-01 14:35:22 -05004259 *
4260 * Description:
Paul Moore220deb92008-01-29 08:38:23 -05004261 * Check the various different forms of network peer labeling and determine
4262 * the peer label/SID for the packet; most of the magic actually occurs in
4263 * the security server function security_net_peersid_cmp(). The function
4264 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4265 * or -EACCES if @sid is invalid due to inconsistencies with the different
4266 * peer labels.
Paul Moore4f6a9932007-03-01 14:35:22 -05004267 *
4268 */
Paul Moore220deb92008-01-29 08:38:23 -05004269static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
Paul Moore4f6a9932007-03-01 14:35:22 -05004270{
Paul Moore71f1cb02008-01-29 08:51:16 -05004271 int err;
Paul Moore4f6a9932007-03-01 14:35:22 -05004272 u32 xfrm_sid;
4273 u32 nlbl_sid;
Paul Moore220deb92008-01-29 08:38:23 -05004274 u32 nlbl_type;
Paul Moore4f6a9932007-03-01 14:35:22 -05004275
Paul Moore817eff72013-12-10 14:57:54 -05004276 err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
Paul Moorebed4d7e2013-07-23 17:38:40 -04004277 if (unlikely(err))
4278 return -EACCES;
4279 err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
4280 if (unlikely(err))
4281 return -EACCES;
Paul Moore220deb92008-01-29 08:38:23 -05004282
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004283 err = security_net_peersid_resolve(&selinux_state, nlbl_sid,
4284 nlbl_type, xfrm_sid, sid);
Paul Moore71f1cb02008-01-29 08:51:16 -05004285 if (unlikely(err)) {
peter enderborgc103a912018-06-12 10:09:03 +02004286 pr_warn(
Paul Moore71f1cb02008-01-29 08:51:16 -05004287 "SELinux: failure in selinux_skb_peerlbl_sid(),"
4288 " unable to determine packet's peer label\n");
Paul Moore220deb92008-01-29 08:38:23 -05004289 return -EACCES;
Paul Moore71f1cb02008-01-29 08:51:16 -05004290 }
Paul Moore220deb92008-01-29 08:38:23 -05004291
4292 return 0;
Paul Moore4f6a9932007-03-01 14:35:22 -05004293}
4294
Paul Moore446b8022013-12-04 16:10:51 -05004295/**
4296 * selinux_conn_sid - Determine the child socket label for a connection
4297 * @sk_sid: the parent socket's SID
4298 * @skb_sid: the packet's SID
4299 * @conn_sid: the resulting connection SID
4300 *
4301 * If @skb_sid is valid then the user:role:type information from @sk_sid is
4302 * combined with the MLS information from @skb_sid in order to create
4303 * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy
4304 * of @sk_sid. Returns zero on success, negative values on failure.
4305 *
4306 */
4307static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
4308{
4309 int err = 0;
4310
4311 if (skb_sid != SECSID_NULL)
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004312 err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid,
4313 conn_sid);
Paul Moore446b8022013-12-04 16:10:51 -05004314 else
4315 *conn_sid = sk_sid;
4316
4317 return err;
4318}
4319
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320/* socket security operations */
Paul Moored4f2d972010-04-22 14:46:18 -04004321
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004322static int socket_sockcreate_sid(const struct task_security_struct *tsec,
4323 u16 secclass, u32 *socksid)
Paul Moored4f2d972010-04-22 14:46:18 -04004324{
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004325 if (tsec->sockcreate_sid > SECSID_NULL) {
4326 *socksid = tsec->sockcreate_sid;
4327 return 0;
4328 }
4329
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004330 return security_transition_sid(&selinux_state, tsec->sid, tsec->sid,
4331 secclass, NULL, socksid);
Paul Moored4f2d972010-04-22 14:46:18 -04004332}
4333
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004334static int sock_has_perm(struct sock *sk, u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335{
Paul Moore253bfae2010-04-22 14:46:19 -04004336 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004337 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004338 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339
Paul Moore253bfae2010-04-22 14:46:19 -04004340 if (sksec->sid == SECINITSID_KERNEL)
4341 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342
Eric Paris50c205f2012-04-04 15:01:43 -04004343 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004344 ad.u.net = &net;
4345 ad.u.net->sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004347 return avc_has_perm(&selinux_state,
4348 current_sid(), sksec->sid, sksec->sclass, perms,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004349 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350}
4351
4352static int selinux_socket_create(int family, int type,
4353 int protocol, int kern)
4354{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07004355 const struct task_security_struct *tsec = selinux_cred(current_cred());
Paul Moored4f2d972010-04-22 14:46:18 -04004356 u32 newsid;
David Howells275bb412008-11-14 10:39:19 +11004357 u16 secclass;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004358 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359
4360 if (kern)
Paul Moored4f2d972010-04-22 14:46:18 -04004361 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362
David Howells275bb412008-11-14 10:39:19 +11004363 secclass = socket_type_to_security_class(family, type, protocol);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004364 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
4365 if (rc)
4366 return rc;
4367
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004368 return avc_has_perm(&selinux_state,
4369 tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370}
4371
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004372static int selinux_socket_post_create(struct socket *sock, int family,
4373 int type, int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07004375 const struct task_security_struct *tsec = selinux_cred(current_cred());
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004376 struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004377 struct sk_security_struct *sksec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004378 u16 sclass = socket_type_to_security_class(family, type, protocol);
4379 u32 sid = SECINITSID_KERNEL;
David Howells275bb412008-11-14 10:39:19 +11004380 int err = 0;
4381
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004382 if (!kern) {
4383 err = socket_sockcreate_sid(tsec, sclass, &sid);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004384 if (err)
4385 return err;
4386 }
David Howells275bb412008-11-14 10:39:19 +11004387
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004388 isec->sclass = sclass;
4389 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004390 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004392 if (sock->sk) {
4393 sksec = sock->sk->sk_security;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004394 sksec->sclass = sclass;
4395 sksec->sid = sid;
Richard Hainesd4529302018-02-13 20:57:18 +00004396 /* Allows detection of the first association on this socket */
4397 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4398 sksec->sctp_assoc_state = SCTP_ASSOC_UNSET;
4399
Paul Moore389fb8002009-03-27 17:10:34 -04004400 err = selinux_netlbl_socket_post_create(sock->sk, family);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004401 }
4402
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004403 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404}
4405
David Herrmann0b811db2018-05-04 16:28:21 +02004406static int selinux_socket_socketpair(struct socket *socka,
4407 struct socket *sockb)
4408{
4409 struct sk_security_struct *sksec_a = socka->sk->sk_security;
4410 struct sk_security_struct *sksec_b = sockb->sk->sk_security;
4411
4412 sksec_a->peer_sid = sksec_b->sid;
4413 sksec_b->peer_sid = sksec_a->sid;
4414
4415 return 0;
4416}
4417
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418/* Range of port numbers used to automatically bind.
4419 Need to determine whether we should perform a name_bind
4420 permission check between the socket and the port number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421
4422static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
4423{
Paul Moore253bfae2010-04-22 14:46:19 -04004424 struct sock *sk = sock->sk;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004425 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 u16 family;
4427 int err;
4428
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004429 err = sock_has_perm(sk, SOCKET__BIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 if (err)
4431 goto out;
4432
Richard Hainesd4529302018-02-13 20:57:18 +00004433 /* If PF_INET or PF_INET6, check name_bind permission for the port. */
Paul Moore253bfae2010-04-22 14:46:19 -04004434 family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 if (family == PF_INET || family == PF_INET6) {
4436 char *addrp;
Thomas Liu2bf49692009-07-14 12:14:09 -04004437 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004438 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 struct sockaddr_in *addr4 = NULL;
4440 struct sockaddr_in6 *addr6 = NULL;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004441 u16 family_sa = address->sa_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 unsigned short snum;
James Morrise399f982008-06-12 01:39:58 +10004443 u32 sid, node_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444
Richard Hainesd4529302018-02-13 20:57:18 +00004445 /*
4446 * sctp_bindx(3) calls via selinux_sctp_bind_connect()
4447 * that validates multiple binding addresses. Because of this
4448 * need to check address->sa_family as it is possible to have
4449 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4450 */
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004451 switch (family_sa) {
4452 case AF_UNSPEC:
Richard Haines68741a8a2018-03-02 19:54:34 +00004453 case AF_INET:
4454 if (addrlen < sizeof(struct sockaddr_in))
4455 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 addr4 = (struct sockaddr_in *)address;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004457 if (family_sa == AF_UNSPEC) {
4458 /* see __inet_bind(), we only want to allow
4459 * AF_UNSPEC if the address is INADDR_ANY
4460 */
4461 if (addr4->sin_addr.s_addr != htonl(INADDR_ANY))
4462 goto err_af;
4463 family_sa = AF_INET;
4464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 snum = ntohs(addr4->sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 addrp = (char *)&addr4->sin_addr.s_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004467 break;
4468 case AF_INET6:
4469 if (addrlen < SIN6_LEN_RFC2133)
4470 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 addr6 = (struct sockaddr_in6 *)address;
4472 snum = ntohs(addr6->sin6_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 addrp = (char *)&addr6->sin6_addr.s6_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004474 break;
4475 default:
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004476 goto err_af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 }
4478
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004479 ad.type = LSM_AUDIT_DATA_NET;
4480 ad.u.net = &net;
4481 ad.u.net->sport = htons(snum);
4482 ad.u.net->family = family_sa;
4483
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004484 if (snum) {
4485 int low, high;
4486
Eric W. Biederman0bbf87d2013-09-28 14:10:59 -07004487 inet_get_local_port_range(sock_net(sk), &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004488
Krister Johansen4548b682017-01-20 17:49:11 -08004489 if (snum < max(inet_prot_sock(sock_net(sk)), low) ||
4490 snum > high) {
Paul Moore3e1121722008-04-10 10:48:14 -04004491 err = sel_netport_sid(sk->sk_protocol,
4492 snum, &sid);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004493 if (err)
4494 goto out;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004495 err = avc_has_perm(&selinux_state,
4496 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004497 sksec->sclass,
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004498 SOCKET__NAME_BIND, &ad);
4499 if (err)
4500 goto out;
4501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 }
Eric Paris828dfe12008-04-17 13:17:49 -04004503
Paul Moore253bfae2010-04-22 14:46:19 -04004504 switch (sksec->sclass) {
James Morris13402582005-09-30 14:24:34 -04004505 case SECCLASS_TCP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 node_perm = TCP_SOCKET__NODE_BIND;
4507 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004508
James Morris13402582005-09-30 14:24:34 -04004509 case SECCLASS_UDP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 node_perm = UDP_SOCKET__NODE_BIND;
4511 break;
James Morris2ee92d42006-11-13 16:09:01 -08004512
4513 case SECCLASS_DCCP_SOCKET:
4514 node_perm = DCCP_SOCKET__NODE_BIND;
4515 break;
4516
Richard Hainesd4529302018-02-13 20:57:18 +00004517 case SECCLASS_SCTP_SOCKET:
4518 node_perm = SCTP_SOCKET__NODE_BIND;
4519 break;
4520
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 default:
4522 node_perm = RAWIP_SOCKET__NODE_BIND;
4523 break;
4524 }
Eric Paris828dfe12008-04-17 13:17:49 -04004525
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004526 err = sel_netnode_sid(addrp, family_sa, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 if (err)
4528 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04004529
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004530 if (family_sa == AF_INET)
Eric Paris48c62af2012-04-02 13:15:44 -04004531 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 else
Eric Paris48c62af2012-04-02 13:15:44 -04004533 ad.u.net->v6info.saddr = addr6->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004535 err = avc_has_perm(&selinux_state,
4536 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004537 sksec->sclass, node_perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 if (err)
4539 goto out;
4540 }
4541out:
4542 return err;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004543err_af:
4544 /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
4545 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4546 return -EINVAL;
4547 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548}
4549
Richard Hainesd4529302018-02-13 20:57:18 +00004550/* This supports connect(2) and SCTP connect services such as sctp_connectx(3)
Kees Cookd61330c2019-02-17 14:08:36 -08004551 * and sctp_sendmsg(3) as described in Documentation/security/SCTP.rst
Richard Hainesd4529302018-02-13 20:57:18 +00004552 */
4553static int selinux_socket_connect_helper(struct socket *sock,
4554 struct sockaddr *address, int addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555{
Paul Moore014ab192008-10-10 10:16:33 -04004556 struct sock *sk = sock->sk;
Paul Moore253bfae2010-04-22 14:46:19 -04004557 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 int err;
4559
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004560 err = sock_has_perm(sk, SOCKET__CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 if (err)
4562 return err;
4563
4564 /*
Richard Hainesd4529302018-02-13 20:57:18 +00004565 * If a TCP, DCCP or SCTP socket, check name_connect permission
4566 * for the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 */
Paul Moore253bfae2010-04-22 14:46:19 -04004568 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00004569 sksec->sclass == SECCLASS_DCCP_SOCKET ||
4570 sksec->sclass == SECCLASS_SCTP_SOCKET) {
Thomas Liu2bf49692009-07-14 12:14:09 -04004571 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004572 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 struct sockaddr_in *addr4 = NULL;
4574 struct sockaddr_in6 *addr6 = NULL;
4575 unsigned short snum;
James Morris2ee92d42006-11-13 16:09:01 -08004576 u32 sid, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577
Richard Hainesd4529302018-02-13 20:57:18 +00004578 /* sctp_connectx(3) calls via selinux_sctp_bind_connect()
4579 * that validates multiple connect addresses. Because of this
4580 * need to check address->sa_family as it is possible to have
4581 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4582 */
Richard Haines68741a8a2018-03-02 19:54:34 +00004583 switch (address->sa_family) {
4584 case AF_INET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 addr4 = (struct sockaddr_in *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004586 if (addrlen < sizeof(struct sockaddr_in))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 return -EINVAL;
4588 snum = ntohs(addr4->sin_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004589 break;
4590 case AF_INET6:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 addr6 = (struct sockaddr_in6 *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004592 if (addrlen < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 return -EINVAL;
4594 snum = ntohs(addr6->sin6_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004595 break;
4596 default:
4597 /* Note that SCTP services expect -EINVAL, whereas
4598 * others expect -EAFNOSUPPORT.
4599 */
4600 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4601 return -EINVAL;
4602 else
4603 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 }
4605
Paul Moore3e1121722008-04-10 10:48:14 -04004606 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004608 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609
Richard Hainesd4529302018-02-13 20:57:18 +00004610 switch (sksec->sclass) {
4611 case SECCLASS_TCP_SOCKET:
4612 perm = TCP_SOCKET__NAME_CONNECT;
4613 break;
4614 case SECCLASS_DCCP_SOCKET:
4615 perm = DCCP_SOCKET__NAME_CONNECT;
4616 break;
4617 case SECCLASS_SCTP_SOCKET:
4618 perm = SCTP_SOCKET__NAME_CONNECT;
4619 break;
4620 }
James Morris2ee92d42006-11-13 16:09:01 -08004621
Eric Paris50c205f2012-04-04 15:01:43 -04004622 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004623 ad.u.net = &net;
4624 ad.u.net->dport = htons(snum);
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004625 ad.u.net->family = address->sa_family;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004626 err = avc_has_perm(&selinux_state,
4627 sksec->sid, sid, sksec->sclass, perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004629 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 }
4631
Richard Hainesd4529302018-02-13 20:57:18 +00004632 return 0;
4633}
Paul Moore014ab192008-10-10 10:16:33 -04004634
Richard Hainesd4529302018-02-13 20:57:18 +00004635/* Supports connect(2), see comments in selinux_socket_connect_helper() */
4636static int selinux_socket_connect(struct socket *sock,
4637 struct sockaddr *address, int addrlen)
4638{
4639 int err;
4640 struct sock *sk = sock->sk;
4641
4642 err = selinux_socket_connect_helper(sock, address, addrlen);
4643 if (err)
4644 return err;
4645
4646 return selinux_netlbl_socket_connect(sk, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647}
4648
4649static int selinux_socket_listen(struct socket *sock, int backlog)
4650{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004651 return sock_has_perm(sock->sk, SOCKET__LISTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652}
4653
4654static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4655{
4656 int err;
4657 struct inode_security_struct *isec;
4658 struct inode_security_struct *newisec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004659 u16 sclass;
4660 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004662 err = sock_has_perm(sock->sk, SOCKET__ACCEPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 if (err)
4664 return err;
4665
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004666 isec = inode_security_novalidate(SOCK_INODE(sock));
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004667 spin_lock(&isec->lock);
4668 sclass = isec->sclass;
4669 sid = isec->sid;
4670 spin_unlock(&isec->lock);
4671
4672 newisec = inode_security_novalidate(SOCK_INODE(newsock));
4673 newisec->sclass = sclass;
4674 newisec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004675 newisec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676
4677 return 0;
4678}
4679
4680static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
Eric Paris828dfe12008-04-17 13:17:49 -04004681 int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004683 return sock_has_perm(sock->sk, SOCKET__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684}
4685
4686static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4687 int size, int flags)
4688{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004689 return sock_has_perm(sock->sk, SOCKET__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690}
4691
4692static int selinux_socket_getsockname(struct socket *sock)
4693{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004694 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695}
4696
4697static int selinux_socket_getpeername(struct socket *sock)
4698{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004699 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700}
4701
Eric Paris828dfe12008-04-17 13:17:49 -04004702static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703{
Paul Mooref8687af2006-10-30 15:22:15 -08004704 int err;
4705
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004706 err = sock_has_perm(sock->sk, SOCKET__SETOPT);
Paul Mooref8687af2006-10-30 15:22:15 -08004707 if (err)
4708 return err;
4709
4710 return selinux_netlbl_socket_setsockopt(sock, level, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711}
4712
4713static int selinux_socket_getsockopt(struct socket *sock, int level,
4714 int optname)
4715{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004716 return sock_has_perm(sock->sk, SOCKET__GETOPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717}
4718
4719static int selinux_socket_shutdown(struct socket *sock, int how)
4720{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004721 return sock_has_perm(sock->sk, SOCKET__SHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722}
4723
David S. Miller3610cda2011-01-05 15:38:53 -08004724static int selinux_socket_unix_stream_connect(struct sock *sock,
4725 struct sock *other,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 struct sock *newsk)
4727{
David S. Miller3610cda2011-01-05 15:38:53 -08004728 struct sk_security_struct *sksec_sock = sock->sk_security;
4729 struct sk_security_struct *sksec_other = other->sk_security;
Paul Moore4d1e2452010-04-22 14:46:18 -04004730 struct sk_security_struct *sksec_new = newsk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004731 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004732 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 int err;
4734
Eric Paris50c205f2012-04-04 15:01:43 -04004735 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004736 ad.u.net = &net;
4737 ad.u.net->sk = other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004739 err = avc_has_perm(&selinux_state,
4740 sksec_sock->sid, sksec_other->sid,
Paul Moore4d1e2452010-04-22 14:46:18 -04004741 sksec_other->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4743 if (err)
4744 return err;
4745
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 /* server child socket */
Paul Moore4d1e2452010-04-22 14:46:18 -04004747 sksec_new->peer_sid = sksec_sock->sid;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004748 err = security_sid_mls_copy(&selinux_state, sksec_other->sid,
4749 sksec_sock->sid, &sksec_new->sid);
Paul Moore4d1e2452010-04-22 14:46:18 -04004750 if (err)
4751 return err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004752
Paul Moore4d1e2452010-04-22 14:46:18 -04004753 /* connecting socket */
4754 sksec_sock->peer_sid = sksec_new->sid;
4755
4756 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757}
4758
4759static int selinux_socket_unix_may_send(struct socket *sock,
4760 struct socket *other)
4761{
Paul Moore253bfae2010-04-22 14:46:19 -04004762 struct sk_security_struct *ssec = sock->sk->sk_security;
4763 struct sk_security_struct *osec = other->sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004764 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004765 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766
Eric Paris50c205f2012-04-04 15:01:43 -04004767 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004768 ad.u.net = &net;
4769 ad.u.net->sk = other->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004771 return avc_has_perm(&selinux_state,
4772 ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
Paul Moore253bfae2010-04-22 14:46:19 -04004773 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774}
4775
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004776static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
4777 char *addrp, u16 family, u32 peer_sid,
Thomas Liu2bf49692009-07-14 12:14:09 -04004778 struct common_audit_data *ad)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004779{
4780 int err;
4781 u32 if_sid;
4782 u32 node_sid;
4783
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004784 err = sel_netif_sid(ns, ifindex, &if_sid);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004785 if (err)
4786 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004787 err = avc_has_perm(&selinux_state,
4788 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004789 SECCLASS_NETIF, NETIF__INGRESS, ad);
4790 if (err)
4791 return err;
4792
4793 err = sel_netnode_sid(addrp, family, &node_sid);
4794 if (err)
4795 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004796 return avc_has_perm(&selinux_state,
4797 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004798 SECCLASS_NODE, NODE__RECVFROM, ad);
4799}
4800
Paul Moore220deb92008-01-29 08:38:23 -05004801static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
Paul Moored8395c82008-10-10 10:16:30 -04004802 u16 family)
Paul Moore220deb92008-01-29 08:38:23 -05004803{
Paul Moore277d3422008-12-31 12:54:11 -05004804 int err = 0;
Paul Moore220deb92008-01-29 08:38:23 -05004805 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004806 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004807 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004808 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004809 char *addrp;
4810
Eric Paris50c205f2012-04-04 15:01:43 -04004811 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004812 ad.u.net = &net;
4813 ad.u.net->netif = skb->skb_iif;
4814 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004815 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4816 if (err)
4817 return err;
Paul Moore220deb92008-01-29 08:38:23 -05004818
Paul Moore58bfbb52009-03-27 17:10:41 -04004819 if (selinux_secmark_enabled()) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004820 err = avc_has_perm(&selinux_state,
4821 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Moored8395c82008-10-10 10:16:30 -04004822 PACKET__RECV, &ad);
Paul Moore58bfbb52009-03-27 17:10:41 -04004823 if (err)
4824 return err;
4825 }
Paul Moore220deb92008-01-29 08:38:23 -05004826
Steffen Klassertb9679a72011-02-23 12:55:21 +01004827 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4828 if (err)
4829 return err;
4830 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004831
James Morris4e5ab4c2006-06-09 00:33:33 -07004832 return err;
4833}
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004834
James Morris4e5ab4c2006-06-09 00:33:33 -07004835static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4836{
Paul Moore220deb92008-01-29 08:38:23 -05004837 int err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004838 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004839 u16 family = sk->sk_family;
4840 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004841 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004842 struct lsm_network_audit net = {0,};
Paul Moore220deb92008-01-29 08:38:23 -05004843 char *addrp;
Paul Moored8395c82008-10-10 10:16:30 -04004844 u8 secmark_active;
4845 u8 peerlbl_active;
James Morris4e5ab4c2006-06-09 00:33:33 -07004846
James Morris4e5ab4c2006-06-09 00:33:33 -07004847 if (family != PF_INET && family != PF_INET6)
Paul Moore220deb92008-01-29 08:38:23 -05004848 return 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07004849
4850 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
Al Viro87fcd702006-12-04 22:00:55 +00004851 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
James Morris4e5ab4c2006-06-09 00:33:33 -07004852 family = PF_INET;
4853
Paul Moored8395c82008-10-10 10:16:30 -04004854 /* If any sort of compatibility mode is enabled then handoff processing
4855 * to the selinux_sock_rcv_skb_compat() function to deal with the
4856 * special handling. We do this in an attempt to keep this function
4857 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004858 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04004859 return selinux_sock_rcv_skb_compat(sk, skb, family);
4860
4861 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04004862 peerlbl_active = selinux_peerlbl_enabled();
Paul Moored8395c82008-10-10 10:16:30 -04004863 if (!secmark_active && !peerlbl_active)
4864 return 0;
4865
Eric Paris50c205f2012-04-04 15:01:43 -04004866 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004867 ad.u.net = &net;
4868 ad.u.net->netif = skb->skb_iif;
4869 ad.u.net->family = family;
Paul Moore224dfbd2008-01-29 08:38:13 -05004870 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
James Morris4e5ab4c2006-06-09 00:33:33 -07004871 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05004872 return err;
James Morris4e5ab4c2006-06-09 00:33:33 -07004873
Paul Moored8395c82008-10-10 10:16:30 -04004874 if (peerlbl_active) {
Paul Moored621d352008-01-29 08:43:36 -05004875 u32 peer_sid;
4876
4877 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4878 if (err)
4879 return err;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004880 err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
4881 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004882 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04004883 selinux_netlbl_err(skb, family, err, 0);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004884 return err;
Paul Mooredfaebe92008-10-10 10:16:31 -04004885 }
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004886 err = avc_has_perm(&selinux_state,
4887 sk_sid, peer_sid, SECCLASS_PEER,
Paul Moored621d352008-01-29 08:43:36 -05004888 PEER__RECV, &ad);
Chad Hanson46d01d62013-12-23 17:45:01 -05004889 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04004890 selinux_netlbl_err(skb, family, err, 0);
Chad Hanson46d01d62013-12-23 17:45:01 -05004891 return err;
4892 }
Paul Moored621d352008-01-29 08:43:36 -05004893 }
4894
Paul Moored8395c82008-10-10 10:16:30 -04004895 if (secmark_active) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004896 err = avc_has_perm(&selinux_state,
4897 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004898 PACKET__RECV, &ad);
4899 if (err)
4900 return err;
4901 }
4902
Paul Moored621d352008-01-29 08:43:36 -05004903 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904}
4905
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004906static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
4907 int __user *optlen, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908{
4909 int err = 0;
4910 char *scontext;
4911 u32 scontext_len;
Paul Moore253bfae2010-04-22 14:46:19 -04004912 struct sk_security_struct *sksec = sock->sk->sk_security;
Paul Moore3de4bab2006-11-17 17:38:54 -05004913 u32 peer_sid = SECSID_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914
Paul Moore253bfae2010-04-22 14:46:19 -04004915 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00004916 sksec->sclass == SECCLASS_TCP_SOCKET ||
4917 sksec->sclass == SECCLASS_SCTP_SOCKET)
Eric Parisdd3e7832010-04-07 15:08:46 -04004918 peer_sid = sksec->peer_sid;
Paul Moore253bfae2010-04-22 14:46:19 -04004919 if (peer_sid == SECSID_NULL)
4920 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004922 err = security_sid_to_context(&selinux_state, peer_sid, &scontext,
4923 &scontext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 if (err)
Paul Moore253bfae2010-04-22 14:46:19 -04004925 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926
4927 if (scontext_len > len) {
4928 err = -ERANGE;
4929 goto out_len;
4930 }
4931
4932 if (copy_to_user(optval, scontext, scontext_len))
4933 err = -EFAULT;
4934
4935out_len:
4936 if (put_user(scontext_len, optlen))
4937 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 kfree(scontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 return err;
4940}
4941
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004942static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004943{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004944 u32 peer_secid = SECSID_NULL;
Paul Moore75e22912008-01-29 08:38:04 -05004945 u16 family;
Paul Moore899134f2016-03-28 15:19:10 -04004946 struct inode_security_struct *isec;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07004947
Paul Mooreaa862902008-10-10 10:16:29 -04004948 if (skb && skb->protocol == htons(ETH_P_IP))
4949 family = PF_INET;
4950 else if (skb && skb->protocol == htons(ETH_P_IPV6))
4951 family = PF_INET6;
4952 else if (sock)
Paul Moore75e22912008-01-29 08:38:04 -05004953 family = sock->sk->sk_family;
Paul Moore75e22912008-01-29 08:38:04 -05004954 else
4955 goto out;
4956
Paul Moore899134f2016-03-28 15:19:10 -04004957 if (sock && family == PF_UNIX) {
4958 isec = inode_security_novalidate(SOCK_INODE(sock));
4959 peer_secid = isec->sid;
4960 } else if (skb)
Paul Moore220deb92008-01-29 08:38:23 -05004961 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004962
Paul Moore75e22912008-01-29 08:38:04 -05004963out:
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004964 *secid = peer_secid;
Paul Moore75e22912008-01-29 08:38:04 -05004965 if (peer_secid == SECSID_NULL)
4966 return -EINVAL;
4967 return 0;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004968}
4969
Al Viro7d877f32005-10-21 03:20:43 -04004970static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971{
Paul Moore84914b72010-04-22 14:46:18 -04004972 struct sk_security_struct *sksec;
4973
4974 sksec = kzalloc(sizeof(*sksec), priority);
4975 if (!sksec)
4976 return -ENOMEM;
4977
4978 sksec->peer_sid = SECINITSID_UNLABELED;
4979 sksec->sid = SECINITSID_UNLABELED;
Stephen Smalley5dee25d2015-07-10 17:19:57 -04004980 sksec->sclass = SECCLASS_SOCKET;
Paul Moore84914b72010-04-22 14:46:18 -04004981 selinux_netlbl_sk_security_reset(sksec);
4982 sk->sk_security = sksec;
4983
4984 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985}
4986
4987static void selinux_sk_free_security(struct sock *sk)
4988{
Paul Moore84914b72010-04-22 14:46:18 -04004989 struct sk_security_struct *sksec = sk->sk_security;
4990
4991 sk->sk_security = NULL;
4992 selinux_netlbl_sk_security_free(sksec);
4993 kfree(sksec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994}
4995
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004996static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
4997{
Eric Parisdd3e7832010-04-07 15:08:46 -04004998 struct sk_security_struct *sksec = sk->sk_security;
4999 struct sk_security_struct *newsksec = newsk->sk_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005000
Eric Parisdd3e7832010-04-07 15:08:46 -04005001 newsksec->sid = sksec->sid;
5002 newsksec->peer_sid = sksec->peer_sid;
5003 newsksec->sclass = sksec->sclass;
Paul Moore99f59ed2006-08-29 17:53:48 -07005004
Eric Parisdd3e7832010-04-07 15:08:46 -04005005 selinux_netlbl_sk_security_reset(newsksec);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005006}
5007
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005008static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005009{
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005010 if (!sk)
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005011 *secid = SECINITSID_ANY_SOCKET;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005012 else {
5013 struct sk_security_struct *sksec = sk->sk_security;
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005014
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005015 *secid = sksec->sid;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005016 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005017}
5018
Eric Paris828dfe12008-04-17 13:17:49 -04005019static void selinux_sock_graft(struct sock *sk, struct socket *parent)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005020{
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05005021 struct inode_security_struct *isec =
5022 inode_security_novalidate(SOCK_INODE(parent));
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005023 struct sk_security_struct *sksec = sk->sk_security;
5024
Paul Moore2873ead2014-07-28 10:42:48 -04005025 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
5026 sk->sk_family == PF_UNIX)
David Woodhouse2148ccc2006-09-29 15:50:25 -07005027 isec->sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05005028 sksec->sclass = isec->sclass;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005029}
5030
Richard Hainesd4529302018-02-13 20:57:18 +00005031/* Called whenever SCTP receives an INIT chunk. This happens when an incoming
5032 * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association
5033 * already present).
5034 */
5035static int selinux_sctp_assoc_request(struct sctp_endpoint *ep,
5036 struct sk_buff *skb)
5037{
5038 struct sk_security_struct *sksec = ep->base.sk->sk_security;
5039 struct common_audit_data ad;
5040 struct lsm_network_audit net = {0,};
5041 u8 peerlbl_active;
5042 u32 peer_sid = SECINITSID_UNLABELED;
5043 u32 conn_sid;
5044 int err = 0;
5045
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005046 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005047 return 0;
5048
5049 peerlbl_active = selinux_peerlbl_enabled();
5050
5051 if (peerlbl_active) {
5052 /* This will return peer_sid = SECSID_NULL if there are
5053 * no peer labels, see security_net_peersid_resolve().
5054 */
5055 err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family,
5056 &peer_sid);
5057 if (err)
5058 return err;
5059
5060 if (peer_sid == SECSID_NULL)
5061 peer_sid = SECINITSID_UNLABELED;
5062 }
5063
5064 if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) {
5065 sksec->sctp_assoc_state = SCTP_ASSOC_SET;
5066
5067 /* Here as first association on socket. As the peer SID
5068 * was allowed by peer recv (and the netif/node checks),
5069 * then it is approved by policy and used as the primary
5070 * peer SID for getpeercon(3).
5071 */
5072 sksec->peer_sid = peer_sid;
5073 } else if (sksec->peer_sid != peer_sid) {
5074 /* Other association peer SIDs are checked to enforce
5075 * consistency among the peer SIDs.
5076 */
5077 ad.type = LSM_AUDIT_DATA_NET;
5078 ad.u.net = &net;
5079 ad.u.net->sk = ep->base.sk;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005080 err = avc_has_perm(&selinux_state,
5081 sksec->peer_sid, peer_sid, sksec->sclass,
Richard Hainesd4529302018-02-13 20:57:18 +00005082 SCTP_SOCKET__ASSOCIATION, &ad);
5083 if (err)
5084 return err;
5085 }
5086
5087 /* Compute the MLS component for the connection and store
5088 * the information in ep. This will be used by SCTP TCP type
5089 * sockets and peeled off connections as they cause a new
5090 * socket to be generated. selinux_sctp_sk_clone() will then
5091 * plug this into the new socket.
5092 */
5093 err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid);
5094 if (err)
5095 return err;
5096
5097 ep->secid = conn_sid;
5098 ep->peer_secid = peer_sid;
5099
5100 /* Set any NetLabel labels including CIPSO/CALIPSO options. */
5101 return selinux_netlbl_sctp_assoc_request(ep, skb);
5102}
5103
5104/* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting
5105 * based on their @optname.
5106 */
5107static int selinux_sctp_bind_connect(struct sock *sk, int optname,
5108 struct sockaddr *address,
5109 int addrlen)
5110{
5111 int len, err = 0, walk_size = 0;
5112 void *addr_buf;
5113 struct sockaddr *addr;
5114 struct socket *sock;
5115
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005116 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005117 return 0;
5118
5119 /* Process one or more addresses that may be IPv4 or IPv6 */
5120 sock = sk->sk_socket;
5121 addr_buf = address;
5122
5123 while (walk_size < addrlen) {
Ondrej Mosnacekc1383252018-11-13 16:16:08 +01005124 if (walk_size + sizeof(sa_family_t) > addrlen)
5125 return -EINVAL;
5126
Richard Hainesd4529302018-02-13 20:57:18 +00005127 addr = addr_buf;
5128 switch (addr->sa_family) {
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005129 case AF_UNSPEC:
Richard Hainesd4529302018-02-13 20:57:18 +00005130 case AF_INET:
5131 len = sizeof(struct sockaddr_in);
5132 break;
5133 case AF_INET6:
5134 len = sizeof(struct sockaddr_in6);
5135 break;
5136 default:
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005137 return -EINVAL;
Richard Hainesd4529302018-02-13 20:57:18 +00005138 }
5139
Xin Long292c9972019-03-09 00:07:34 +08005140 if (walk_size + len > addrlen)
5141 return -EINVAL;
5142
Richard Hainesd4529302018-02-13 20:57:18 +00005143 err = -EINVAL;
5144 switch (optname) {
5145 /* Bind checks */
5146 case SCTP_PRIMARY_ADDR:
5147 case SCTP_SET_PEER_PRIMARY_ADDR:
5148 case SCTP_SOCKOPT_BINDX_ADD:
5149 err = selinux_socket_bind(sock, addr, len);
5150 break;
5151 /* Connect checks */
5152 case SCTP_SOCKOPT_CONNECTX:
5153 case SCTP_PARAM_SET_PRIMARY:
5154 case SCTP_PARAM_ADD_IP:
5155 case SCTP_SENDMSG_CONNECT:
5156 err = selinux_socket_connect_helper(sock, addr, len);
5157 if (err)
5158 return err;
5159
5160 /* As selinux_sctp_bind_connect() is called by the
5161 * SCTP protocol layer, the socket is already locked,
5162 * therefore selinux_netlbl_socket_connect_locked() is
5163 * is called here. The situations handled are:
5164 * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2),
5165 * whenever a new IP address is added or when a new
5166 * primary address is selected.
5167 * Note that an SCTP connect(2) call happens before
5168 * the SCTP protocol layer and is handled via
5169 * selinux_socket_connect().
5170 */
5171 err = selinux_netlbl_socket_connect_locked(sk, addr);
5172 break;
5173 }
5174
5175 if (err)
5176 return err;
5177
5178 addr_buf += len;
5179 walk_size += len;
5180 }
5181
5182 return 0;
5183}
5184
5185/* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */
5186static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk,
5187 struct sock *newsk)
5188{
5189 struct sk_security_struct *sksec = sk->sk_security;
5190 struct sk_security_struct *newsksec = newsk->sk_security;
5191
5192 /* If policy does not support SECCLASS_SCTP_SOCKET then call
5193 * the non-sctp clone version.
5194 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005195 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005196 return selinux_sk_clone_security(sk, newsk);
5197
5198 newsksec->sid = ep->secid;
5199 newsksec->peer_sid = ep->peer_secid;
5200 newsksec->sclass = sksec->sclass;
5201 selinux_netlbl_sctp_sk_clone(sk, newsk);
5202}
5203
Adrian Bunk9a673e52006-08-15 00:03:53 -07005204static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
5205 struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005206{
5207 struct sk_security_struct *sksec = sk->sk_security;
5208 int err;
Paul Moore0b1f24e2013-12-03 11:39:13 -05005209 u16 family = req->rsk_ops->family;
Paul Moore446b8022013-12-04 16:10:51 -05005210 u32 connsid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005211 u32 peersid;
5212
Paul Mooreaa862902008-10-10 10:16:29 -04005213 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
Paul Moore220deb92008-01-29 08:38:23 -05005214 if (err)
5215 return err;
Paul Moore446b8022013-12-04 16:10:51 -05005216 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
5217 if (err)
5218 return err;
5219 req->secid = connsid;
5220 req->peer_secid = peersid;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07005221
Paul Moore389fb8002009-03-27 17:10:34 -04005222 return selinux_netlbl_inet_conn_request(req, family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005223}
5224
Adrian Bunk9a673e52006-08-15 00:03:53 -07005225static void selinux_inet_csk_clone(struct sock *newsk,
5226 const struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005227{
5228 struct sk_security_struct *newsksec = newsk->sk_security;
5229
5230 newsksec->sid = req->secid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005231 newsksec->peer_sid = req->peer_secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005232 /* NOTE: Ideally, we should also get the isec->sid for the
5233 new socket in sync, but we don't have the isec available yet.
5234 So we will wait until sock_graft to do it, by which
5235 time it will have been created and available. */
Paul Moore99f59ed2006-08-29 17:53:48 -07005236
Paul Moore9f2ad662006-11-17 17:38:53 -05005237 /* We don't need to take any sort of lock here as we are the only
5238 * thread with access to newsksec */
Paul Moore389fb8002009-03-27 17:10:34 -04005239 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005240}
5241
Paul Moore014ab192008-10-10 10:16:33 -04005242static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005243{
Paul Mooreaa862902008-10-10 10:16:29 -04005244 u16 family = sk->sk_family;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005245 struct sk_security_struct *sksec = sk->sk_security;
5246
Paul Mooreaa862902008-10-10 10:16:29 -04005247 /* handle mapped IPv4 packets arriving via IPv6 sockets */
5248 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5249 family = PF_INET;
5250
5251 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005252}
5253
Eric Paris2606fd12010-10-13 16:24:41 -04005254static int selinux_secmark_relabel_packet(u32 sid)
5255{
5256 const struct task_security_struct *__tsec;
5257 u32 tsid;
5258
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07005259 __tsec = selinux_cred(current_cred());
Eric Paris2606fd12010-10-13 16:24:41 -04005260 tsid = __tsec->sid;
5261
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005262 return avc_has_perm(&selinux_state,
5263 tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO,
5264 NULL);
Eric Paris2606fd12010-10-13 16:24:41 -04005265}
5266
5267static void selinux_secmark_refcount_inc(void)
5268{
5269 atomic_inc(&selinux_secmark_refcount);
5270}
5271
5272static void selinux_secmark_refcount_dec(void)
5273{
5274 atomic_dec(&selinux_secmark_refcount);
5275}
5276
Adrian Bunk9a673e52006-08-15 00:03:53 -07005277static void selinux_req_classify_flow(const struct request_sock *req,
5278 struct flowi *fl)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005279{
David S. Miller1d28f422011-03-12 00:29:39 -05005280 fl->flowi_secid = req->secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005281}
5282
Paul Moore5dbbaf22013-01-14 07:12:19 +00005283static int selinux_tun_dev_alloc_security(void **security)
5284{
5285 struct tun_security_struct *tunsec;
5286
5287 tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
5288 if (!tunsec)
5289 return -ENOMEM;
5290 tunsec->sid = current_sid();
5291
5292 *security = tunsec;
5293 return 0;
5294}
5295
5296static void selinux_tun_dev_free_security(void *security)
5297{
5298 kfree(security);
5299}
5300
Paul Mooreed6d76e2009-08-28 18:12:49 -04005301static int selinux_tun_dev_create(void)
5302{
5303 u32 sid = current_sid();
5304
5305 /* we aren't taking into account the "sockcreate" SID since the socket
5306 * that is being created here is not a socket in the traditional sense,
5307 * instead it is a private sock, accessible only to the kernel, and
5308 * representing a wide range of network traffic spanning multiple
5309 * connections unlike traditional sockets - check the TUN driver to
5310 * get a better understanding of why this socket is special */
5311
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005312 return avc_has_perm(&selinux_state,
5313 sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005314 NULL);
5315}
5316
Paul Moore5dbbaf22013-01-14 07:12:19 +00005317static int selinux_tun_dev_attach_queue(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005318{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005319 struct tun_security_struct *tunsec = security;
5320
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005321 return avc_has_perm(&selinux_state,
5322 current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Moore5dbbaf22013-01-14 07:12:19 +00005323 TUN_SOCKET__ATTACH_QUEUE, NULL);
5324}
5325
5326static int selinux_tun_dev_attach(struct sock *sk, void *security)
5327{
5328 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005329 struct sk_security_struct *sksec = sk->sk_security;
5330
5331 /* we don't currently perform any NetLabel based labeling here and it
5332 * isn't clear that we would want to do so anyway; while we could apply
5333 * labeling without the support of the TUN user the resulting labeled
5334 * traffic from the other end of the connection would almost certainly
5335 * cause confusion to the TUN user that had no idea network labeling
5336 * protocols were being used */
5337
Paul Moore5dbbaf22013-01-14 07:12:19 +00005338 sksec->sid = tunsec->sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005339 sksec->sclass = SECCLASS_TUN_SOCKET;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005340
5341 return 0;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005342}
5343
Paul Moore5dbbaf22013-01-14 07:12:19 +00005344static int selinux_tun_dev_open(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005345{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005346 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005347 u32 sid = current_sid();
5348 int err;
5349
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005350 err = avc_has_perm(&selinux_state,
5351 sid, tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005352 TUN_SOCKET__RELABELFROM, NULL);
5353 if (err)
5354 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005355 err = avc_has_perm(&selinux_state,
5356 sid, sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005357 TUN_SOCKET__RELABELTO, NULL);
5358 if (err)
5359 return err;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005360 tunsec->sid = sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005361
5362 return 0;
5363}
5364
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
5366{
5367 int err = 0;
5368 u32 perm;
5369 struct nlmsghdr *nlh;
Paul Moore253bfae2010-04-22 14:46:19 -04005370 struct sk_security_struct *sksec = sk->sk_security;
Eric Paris828dfe12008-04-17 13:17:49 -04005371
Hong zhi guo77954982013-03-27 06:49:35 +00005372 if (skb->len < NLMSG_HDRLEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 err = -EINVAL;
5374 goto out;
5375 }
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07005376 nlh = nlmsg_hdr(skb);
Eric Paris828dfe12008-04-17 13:17:49 -04005377
Paul Moore253bfae2010-04-22 14:46:19 -04005378 err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 if (err) {
5380 if (err == -EINVAL) {
Vladis Dronov76319942015-12-24 11:09:41 -05005381 pr_warn_ratelimited("SELinux: unrecognized netlink"
5382 " message: protocol=%hu nlmsg_type=%hu sclass=%s"
5383 " pig=%d comm=%s\n",
Marek Milkoviccded3ff2015-06-04 16:22:16 -04005384 sk->sk_protocol, nlh->nlmsg_type,
Vladis Dronov76319942015-12-24 11:09:41 -05005385 secclass_map[sksec->sclass - 1].name,
5386 task_pid_nr(current), current->comm);
Paul Mooree5a5ca92018-03-01 17:38:30 -05005387 if (!enforcing_enabled(&selinux_state) ||
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005388 security_get_allow_unknown(&selinux_state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 err = 0;
5390 }
5391
5392 /* Ignore */
5393 if (err == -ENOENT)
5394 err = 0;
5395 goto out;
5396 }
5397
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005398 err = sock_has_perm(sk, perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399out:
5400 return err;
5401}
5402
5403#ifdef CONFIG_NETFILTER
5404
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005405static unsigned int selinux_ip_forward(struct sk_buff *skb,
5406 const struct net_device *indev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005407 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408{
Paul Mooredfaebe92008-10-10 10:16:31 -04005409 int err;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005410 char *addrp;
5411 u32 peer_sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04005412 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005413 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005414 u8 secmark_active;
Paul Moore948bf852008-10-10 10:16:32 -04005415 u8 netlbl_active;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005416 u8 peerlbl_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005417
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005418 if (!selinux_policycap_netpeer())
Paul Mooreeffad8d2008-01-29 08:49:27 -05005419 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005420
Paul Mooreeffad8d2008-01-29 08:49:27 -05005421 secmark_active = selinux_secmark_enabled();
Paul Moore948bf852008-10-10 10:16:32 -04005422 netlbl_active = netlbl_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005423 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005424 if (!secmark_active && !peerlbl_active)
5425 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005426
Paul Moored8395c82008-10-10 10:16:30 -04005427 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
5428 return NF_DROP;
5429
Eric Paris50c205f2012-04-04 15:01:43 -04005430 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005431 ad.u.net = &net;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005432 ad.u.net->netif = indev->ifindex;
Eric Paris48c62af2012-04-02 13:15:44 -04005433 ad.u.net->family = family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005434 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
5435 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436
Paul Mooredfaebe92008-10-10 10:16:31 -04005437 if (peerlbl_active) {
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005438 err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex,
5439 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04005440 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04005441 selinux_netlbl_err(skb, family, err, 1);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005442 return NF_DROP;
Paul Mooredfaebe92008-10-10 10:16:31 -04005443 }
5444 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05005445
5446 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005447 if (avc_has_perm(&selinux_state,
5448 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005449 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
5450 return NF_DROP;
5451
Paul Moore948bf852008-10-10 10:16:32 -04005452 if (netlbl_active)
5453 /* we do this in the FORWARD path and not the POST_ROUTING
5454 * path because we want to make sure we apply the necessary
5455 * labeling before IPsec is applied so we can leverage AH
5456 * protection */
5457 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
5458 return NF_DROP;
5459
Paul Mooreeffad8d2008-01-29 08:49:27 -05005460 return NF_ACCEPT;
5461}
5462
Eric W. Biederman06198b32015-09-18 14:33:06 -05005463static unsigned int selinux_ipv4_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005464 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005465 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005466{
David S. Miller238e54c2015-04-03 20:32:56 -04005467 return selinux_ip_forward(skb, state->in, PF_INET);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005468}
5469
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005470#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005471static unsigned int selinux_ipv6_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005472 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005473 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005474{
David S. Miller238e54c2015-04-03 20:32:56 -04005475 return selinux_ip_forward(skb, state->in, PF_INET6);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005476}
5477#endif /* IPV6 */
5478
Paul Moore948bf852008-10-10 10:16:32 -04005479static unsigned int selinux_ip_output(struct sk_buff *skb,
5480 u16 family)
5481{
Paul Moore47180062013-12-04 16:10:45 -05005482 struct sock *sk;
Paul Moore948bf852008-10-10 10:16:32 -04005483 u32 sid;
5484
5485 if (!netlbl_enabled())
5486 return NF_ACCEPT;
5487
5488 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
5489 * because we want to make sure we apply the necessary labeling
5490 * before IPsec is applied so we can leverage AH protection */
Paul Moore47180062013-12-04 16:10:45 -05005491 sk = skb->sk;
5492 if (sk) {
5493 struct sk_security_struct *sksec;
5494
Eric Dumazete446f9d2015-10-08 05:01:55 -07005495 if (sk_listener(sk))
Paul Moore47180062013-12-04 16:10:45 -05005496 /* if the socket is the listening state then this
5497 * packet is a SYN-ACK packet which means it needs to
5498 * be labeled based on the connection/request_sock and
5499 * not the parent socket. unfortunately, we can't
5500 * lookup the request_sock yet as it isn't queued on
5501 * the parent socket until after the SYN-ACK is sent.
5502 * the "solution" is to simply pass the packet as-is
5503 * as any IP option based labeling should be copied
5504 * from the initial connection request (in the IP
5505 * layer). it is far from ideal, but until we get a
5506 * security label in the packet itself this is the
5507 * best we can do. */
5508 return NF_ACCEPT;
5509
5510 /* standard practice, label using the parent socket */
5511 sksec = sk->sk_security;
Paul Moore948bf852008-10-10 10:16:32 -04005512 sid = sksec->sid;
5513 } else
5514 sid = SECINITSID_KERNEL;
5515 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
5516 return NF_DROP;
5517
5518 return NF_ACCEPT;
5519}
5520
Eric W. Biederman06198b32015-09-18 14:33:06 -05005521static unsigned int selinux_ipv4_output(void *priv,
Paul Moore948bf852008-10-10 10:16:32 -04005522 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005523 const struct nf_hook_state *state)
Paul Moore948bf852008-10-10 10:16:32 -04005524{
5525 return selinux_ip_output(skb, PF_INET);
5526}
5527
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005528#if IS_ENABLED(CONFIG_IPV6)
Huw Davies2917f572016-06-27 15:06:15 -04005529static unsigned int selinux_ipv6_output(void *priv,
5530 struct sk_buff *skb,
5531 const struct nf_hook_state *state)
5532{
5533 return selinux_ip_output(skb, PF_INET6);
5534}
5535#endif /* IPV6 */
5536
Paul Mooreeffad8d2008-01-29 08:49:27 -05005537static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
5538 int ifindex,
Paul Moored8395c82008-10-10 10:16:30 -04005539 u16 family)
James Morris4e5ab4c2006-06-09 00:33:33 -07005540{
Eric Dumazet54abc682015-11-08 10:54:07 -08005541 struct sock *sk = skb_to_full_sk(skb);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005542 struct sk_security_struct *sksec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005543 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005544 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04005545 char *addrp;
5546 u8 proto;
James Morris4e5ab4c2006-06-09 00:33:33 -07005547
Paul Mooreeffad8d2008-01-29 08:49:27 -05005548 if (sk == NULL)
5549 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005550 sksec = sk->sk_security;
James Morris4e5ab4c2006-06-09 00:33:33 -07005551
Eric Paris50c205f2012-04-04 15:01:43 -04005552 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005553 ad.u.net = &net;
5554 ad.u.net->netif = ifindex;
5555 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005556 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
5557 return NF_DROP;
5558
Paul Moore58bfbb52009-03-27 17:10:41 -04005559 if (selinux_secmark_enabled())
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005560 if (avc_has_perm(&selinux_state,
5561 sksec->sid, skb->secmark,
Paul Moored8395c82008-10-10 10:16:30 -04005562 SECCLASS_PACKET, PACKET__SEND, &ad))
Eric Paris2fe66ec2010-11-23 06:28:08 +00005563 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005564
Steffen Klassertb9679a72011-02-23 12:55:21 +01005565 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
5566 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005567
Paul Mooreeffad8d2008-01-29 08:49:27 -05005568 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569}
5570
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005571static unsigned int selinux_ip_postroute(struct sk_buff *skb,
5572 const struct net_device *outdev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005573 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574{
Paul Mooreeffad8d2008-01-29 08:49:27 -05005575 u32 secmark_perm;
5576 u32 peer_sid;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005577 int ifindex = outdev->ifindex;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005578 struct sock *sk;
Thomas Liu2bf49692009-07-14 12:14:09 -04005579 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005580 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005581 char *addrp;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005582 u8 secmark_active;
5583 u8 peerlbl_active;
5584
Paul Mooreeffad8d2008-01-29 08:49:27 -05005585 /* If any sort of compatibility mode is enabled then handoff processing
5586 * to the selinux_ip_postroute_compat() function to deal with the
5587 * special handling. We do this in an attempt to keep this function
5588 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005589 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04005590 return selinux_ip_postroute_compat(skb, ifindex, family);
Paul Moorec0828e52013-12-10 14:58:01 -05005591
Paul Mooreeffad8d2008-01-29 08:49:27 -05005592 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005593 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005594 if (!secmark_active && !peerlbl_active)
5595 return NF_ACCEPT;
5596
Eric Dumazet54abc682015-11-08 10:54:07 -08005597 sk = skb_to_full_sk(skb);
Paul Moorec0828e52013-12-10 14:58:01 -05005598
Paul Mooreeffad8d2008-01-29 08:49:27 -05005599#ifdef CONFIG_XFRM
5600 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
5601 * packet transformation so allow the packet to pass without any checks
5602 * since we'll have another chance to perform access control checks
5603 * when the packet is on it's final way out.
5604 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
Paul Moorec0828e52013-12-10 14:58:01 -05005605 * is NULL, in this case go ahead and apply access control.
5606 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
5607 * TCP listening state we cannot wait until the XFRM processing
5608 * is done as we will miss out on the SA label if we do;
5609 * unfortunately, this means more work, but it is only once per
5610 * connection. */
5611 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
Eric Dumazete446f9d2015-10-08 05:01:55 -07005612 !(sk && sk_listener(sk)))
Paul Mooreeffad8d2008-01-29 08:49:27 -05005613 return NF_ACCEPT;
5614#endif
Paul Mooreeffad8d2008-01-29 08:49:27 -05005615
Paul Moored8395c82008-10-10 10:16:30 -04005616 if (sk == NULL) {
Paul Moore446b8022013-12-04 16:10:51 -05005617 /* Without an associated socket the packet is either coming
5618 * from the kernel or it is being forwarded; check the packet
5619 * to determine which and if the packet is being forwarded
5620 * query the packet directly to determine the security label. */
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005621 if (skb->skb_iif) {
5622 secmark_perm = PACKET__FORWARD_OUT;
Paul Moored8395c82008-10-10 10:16:30 -04005623 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005624 return NF_DROP;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005625 } else {
5626 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005627 peer_sid = SECINITSID_KERNEL;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005628 }
Eric Dumazete446f9d2015-10-08 05:01:55 -07005629 } else if (sk_listener(sk)) {
Paul Moore446b8022013-12-04 16:10:51 -05005630 /* Locally generated packet but the associated socket is in the
5631 * listening state which means this is a SYN-ACK packet. In
5632 * this particular case the correct security label is assigned
5633 * to the connection/request_sock but unfortunately we can't
5634 * query the request_sock as it isn't queued on the parent
5635 * socket until after the SYN-ACK packet is sent; the only
5636 * viable choice is to regenerate the label like we do in
5637 * selinux_inet_conn_request(). See also selinux_ip_output()
5638 * for similar problems. */
5639 u32 skb_sid;
Eric Dumazete446f9d2015-10-08 05:01:55 -07005640 struct sk_security_struct *sksec;
5641
Eric Dumazete446f9d2015-10-08 05:01:55 -07005642 sksec = sk->sk_security;
Paul Moore446b8022013-12-04 16:10:51 -05005643 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
5644 return NF_DROP;
Paul Moorec0828e52013-12-10 14:58:01 -05005645 /* At this point, if the returned skb peerlbl is SECSID_NULL
5646 * and the packet has been through at least one XFRM
5647 * transformation then we must be dealing with the "final"
5648 * form of labeled IPsec packet; since we've already applied
5649 * all of our access controls on this packet we can safely
5650 * pass the packet. */
5651 if (skb_sid == SECSID_NULL) {
5652 switch (family) {
5653 case PF_INET:
5654 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
5655 return NF_ACCEPT;
5656 break;
5657 case PF_INET6:
5658 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
5659 return NF_ACCEPT;
Paul Moorea7a91a12014-09-03 10:51:59 -04005660 break;
Paul Moorec0828e52013-12-10 14:58:01 -05005661 default:
5662 return NF_DROP_ERR(-ECONNREFUSED);
5663 }
5664 }
Paul Moore446b8022013-12-04 16:10:51 -05005665 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
5666 return NF_DROP;
5667 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005668 } else {
Paul Moore446b8022013-12-04 16:10:51 -05005669 /* Locally generated packet, fetch the security label from the
5670 * associated socket. */
Paul Mooreeffad8d2008-01-29 08:49:27 -05005671 struct sk_security_struct *sksec = sk->sk_security;
5672 peer_sid = sksec->sid;
5673 secmark_perm = PACKET__SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005674 }
5675
Eric Paris50c205f2012-04-04 15:01:43 -04005676 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005677 ad.u.net = &net;
5678 ad.u.net->netif = ifindex;
5679 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005680 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
Eric Paris04f6d702010-11-23 06:28:02 +00005681 return NF_DROP;
Paul Moored8395c82008-10-10 10:16:30 -04005682
Paul Mooreeffad8d2008-01-29 08:49:27 -05005683 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005684 if (avc_has_perm(&selinux_state,
5685 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005686 SECCLASS_PACKET, secmark_perm, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005687 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005688
5689 if (peerlbl_active) {
5690 u32 if_sid;
5691 u32 node_sid;
5692
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005693 if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005694 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005695 if (avc_has_perm(&selinux_state,
5696 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005697 SECCLASS_NETIF, NETIF__EGRESS, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005698 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005699
5700 if (sel_netnode_sid(addrp, family, &node_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005701 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005702 if (avc_has_perm(&selinux_state,
5703 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005704 SECCLASS_NODE, NODE__SENDTO, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005705 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005706 }
5707
5708 return NF_ACCEPT;
5709}
5710
Eric W. Biederman06198b32015-09-18 14:33:06 -05005711static unsigned int selinux_ipv4_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005712 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005713 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005714{
David S. Miller238e54c2015-04-03 20:32:56 -04005715 return selinux_ip_postroute(skb, state->out, PF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716}
5717
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005718#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005719static unsigned int selinux_ipv6_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005720 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005721 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722{
David S. Miller238e54c2015-04-03 20:32:56 -04005723 return selinux_ip_postroute(skb, state->out, PF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725#endif /* IPV6 */
5726
5727#endif /* CONFIG_NETFILTER */
5728
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
5730{
Stephen Smalley941fc5b2009-10-01 14:48:23 -04005731 return selinux_nlmsg_perm(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732}
5733
Casey Schauflerecd5f822018-11-20 11:55:02 -08005734static void ipc_init_security(struct ipc_security_struct *isec, u16 sclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 isec->sclass = sclass;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005737 isec->sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738}
5739
5740static int msg_msg_alloc_security(struct msg_msg *msg)
5741{
5742 struct msg_security_struct *msec;
5743
Casey Schauflerecd5f822018-11-20 11:55:02 -08005744 msec = selinux_msg_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 msec->sid = SECINITSID_UNLABELED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746
5747 return 0;
5748}
5749
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
Stephen Smalley6af963f2005-05-01 08:58:39 -07005751 u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005754 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005755 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756
Casey Schaufler7c653822018-09-21 17:19:45 -07005757 isec = selinux_ipc(ipc_perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758
Eric Paris50c205f2012-04-04 15:01:43 -04005759 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 ad.u.ipc_id = ipc_perms->key;
5761
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005762 return avc_has_perm(&selinux_state,
5763 sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764}
5765
5766static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
5767{
5768 return msg_msg_alloc_security(msg);
5769}
5770
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771/* message queue security operations */
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005772static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005775 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005776 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 int rc;
5778
Casey Schauflerecd5f822018-11-20 11:55:02 -08005779 isec = selinux_ipc(msq);
5780 ipc_init_security(isec, SECCLASS_MSGQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781
Eric Paris50c205f2012-04-04 15:01:43 -04005782 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005783 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005785 rc = avc_has_perm(&selinux_state,
5786 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 MSGQ__CREATE, &ad);
Casey Schauflerecd5f822018-11-20 11:55:02 -08005788 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789}
5790
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005791static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005794 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005795 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796
Casey Schaufler7c653822018-09-21 17:19:45 -07005797 isec = selinux_ipc(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798
Eric Paris50c205f2012-04-04 15:01:43 -04005799 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005800 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005802 return avc_has_perm(&selinux_state,
5803 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 MSGQ__ASSOCIATE, &ad);
5805}
5806
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005807static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808{
5809 int err;
5810 int perms;
5811
Eric Paris828dfe12008-04-17 13:17:49 -04005812 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813 case IPC_INFO:
5814 case MSG_INFO:
5815 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005816 return avc_has_perm(&selinux_state,
5817 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005818 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819 case IPC_STAT:
5820 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07005821 case MSG_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
5823 break;
5824 case IPC_SET:
5825 perms = MSGQ__SETATTR;
5826 break;
5827 case IPC_RMID:
5828 perms = MSGQ__DESTROY;
5829 break;
5830 default:
5831 return 0;
5832 }
5833
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005834 err = ipc_has_perm(msq, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835 return err;
5836}
5837
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005838static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 struct ipc_security_struct *isec;
5841 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005842 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005843 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844 int rc;
5845
Casey Schaufler7c653822018-09-21 17:19:45 -07005846 isec = selinux_ipc(msq);
5847 msec = selinux_msg_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848
5849 /*
5850 * First time through, need to assign label to the message
5851 */
5852 if (msec->sid == SECINITSID_UNLABELED) {
5853 /*
5854 * Compute new sid based on current process and
5855 * message queue this message will be stored in
5856 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005857 rc = security_transition_sid(&selinux_state, sid, isec->sid,
5858 SECCLASS_MSG, NULL, &msec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859 if (rc)
5860 return rc;
5861 }
5862
Eric Paris50c205f2012-04-04 15:01:43 -04005863 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005864 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865
5866 /* Can this process write to the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005867 rc = avc_has_perm(&selinux_state,
5868 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869 MSGQ__WRITE, &ad);
5870 if (!rc)
5871 /* Can this process send the message */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005872 rc = avc_has_perm(&selinux_state,
5873 sid, msec->sid, SECCLASS_MSG,
David Howells275bb412008-11-14 10:39:19 +11005874 MSG__SEND, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875 if (!rc)
5876 /* Can the message be put in the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005877 rc = avc_has_perm(&selinux_state,
5878 msec->sid, isec->sid, SECCLASS_MSGQ,
David Howells275bb412008-11-14 10:39:19 +11005879 MSGQ__ENQUEUE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880
5881 return rc;
5882}
5883
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005884static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885 struct task_struct *target,
5886 long type, int mode)
5887{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 struct ipc_security_struct *isec;
5889 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005890 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005891 u32 sid = task_sid(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 int rc;
5893
Casey Schaufler7c653822018-09-21 17:19:45 -07005894 isec = selinux_ipc(msq);
5895 msec = selinux_msg_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896
Eric Paris50c205f2012-04-04 15:01:43 -04005897 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005898 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005900 rc = avc_has_perm(&selinux_state,
5901 sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902 SECCLASS_MSGQ, MSGQ__READ, &ad);
5903 if (!rc)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005904 rc = avc_has_perm(&selinux_state,
5905 sid, msec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 SECCLASS_MSG, MSG__RECEIVE, &ad);
5907 return rc;
5908}
5909
5910/* Shared Memory security operations */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005911static int selinux_shm_alloc_security(struct kern_ipc_perm *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005914 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005915 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916 int rc;
5917
Casey Schauflerecd5f822018-11-20 11:55:02 -08005918 isec = selinux_ipc(shp);
5919 ipc_init_security(isec, SECCLASS_SHM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920
Eric Paris50c205f2012-04-04 15:01:43 -04005921 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005922 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005924 rc = avc_has_perm(&selinux_state,
5925 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926 SHM__CREATE, &ad);
Casey Schauflerecd5f822018-11-20 11:55:02 -08005927 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928}
5929
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005930static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005933 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005934 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935
Casey Schaufler7c653822018-09-21 17:19:45 -07005936 isec = selinux_ipc(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937
Eric Paris50c205f2012-04-04 15:01:43 -04005938 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005939 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005941 return avc_has_perm(&selinux_state,
5942 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943 SHM__ASSOCIATE, &ad);
5944}
5945
5946/* Note, at this point, shp is locked down */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005947static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948{
5949 int perms;
5950 int err;
5951
Eric Paris828dfe12008-04-17 13:17:49 -04005952 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953 case IPC_INFO:
5954 case SHM_INFO:
5955 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005956 return avc_has_perm(&selinux_state,
5957 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005958 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959 case IPC_STAT:
5960 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07005961 case SHM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962 perms = SHM__GETATTR | SHM__ASSOCIATE;
5963 break;
5964 case IPC_SET:
5965 perms = SHM__SETATTR;
5966 break;
5967 case SHM_LOCK:
5968 case SHM_UNLOCK:
5969 perms = SHM__LOCK;
5970 break;
5971 case IPC_RMID:
5972 perms = SHM__DESTROY;
5973 break;
5974 default:
5975 return 0;
5976 }
5977
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005978 err = ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979 return err;
5980}
5981
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005982static int selinux_shm_shmat(struct kern_ipc_perm *shp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983 char __user *shmaddr, int shmflg)
5984{
5985 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986
5987 if (shmflg & SHM_RDONLY)
5988 perms = SHM__READ;
5989 else
5990 perms = SHM__READ | SHM__WRITE;
5991
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005992 return ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993}
5994
5995/* Semaphore security operations */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05005996static int selinux_sem_alloc_security(struct kern_ipc_perm *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005999 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006000 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001 int rc;
6002
Casey Schauflerecd5f822018-11-20 11:55:02 -08006003 isec = selinux_ipc(sma);
6004 ipc_init_security(isec, SECCLASS_SEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005
Eric Paris50c205f2012-04-04 15:01:43 -04006006 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006007 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006009 rc = avc_has_perm(&selinux_state,
6010 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011 SEM__CREATE, &ad);
Casey Schauflerecd5f822018-11-20 11:55:02 -08006012 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013}
6014
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006015static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006018 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006019 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020
Casey Schaufler7c653822018-09-21 17:19:45 -07006021 isec = selinux_ipc(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022
Eric Paris50c205f2012-04-04 15:01:43 -04006023 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006024 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006026 return avc_has_perm(&selinux_state,
6027 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 SEM__ASSOCIATE, &ad);
6029}
6030
6031/* Note, at this point, sma is locked down */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006032static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033{
6034 int err;
6035 u32 perms;
6036
Eric Paris828dfe12008-04-17 13:17:49 -04006037 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038 case IPC_INFO:
6039 case SEM_INFO:
6040 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006041 return avc_has_perm(&selinux_state,
6042 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006043 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 case GETPID:
6045 case GETNCNT:
6046 case GETZCNT:
6047 perms = SEM__GETATTR;
6048 break;
6049 case GETVAL:
6050 case GETALL:
6051 perms = SEM__READ;
6052 break;
6053 case SETVAL:
6054 case SETALL:
6055 perms = SEM__WRITE;
6056 break;
6057 case IPC_RMID:
6058 perms = SEM__DESTROY;
6059 break;
6060 case IPC_SET:
6061 perms = SEM__SETATTR;
6062 break;
6063 case IPC_STAT:
6064 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07006065 case SEM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066 perms = SEM__GETATTR | SEM__ASSOCIATE;
6067 break;
6068 default:
6069 return 0;
6070 }
6071
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006072 err = ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073 return err;
6074}
6075
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006076static int selinux_sem_semop(struct kern_ipc_perm *sma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077 struct sembuf *sops, unsigned nsops, int alter)
6078{
6079 u32 perms;
6080
6081 if (alter)
6082 perms = SEM__READ | SEM__WRITE;
6083 else
6084 perms = SEM__READ;
6085
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006086 return ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087}
6088
6089static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
6090{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091 u32 av = 0;
6092
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093 av = 0;
6094 if (flag & S_IRUGO)
6095 av |= IPC__UNIX_READ;
6096 if (flag & S_IWUGO)
6097 av |= IPC__UNIX_WRITE;
6098
6099 if (av == 0)
6100 return 0;
6101
Stephen Smalley6af963f2005-05-01 08:58:39 -07006102 return ipc_has_perm(ipcp, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103}
6104
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02006105static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
6106{
Casey Schaufler7c653822018-09-21 17:19:45 -07006107 struct ipc_security_struct *isec = selinux_ipc(ipcp);
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02006108 *secid = isec->sid;
6109}
6110
Eric Paris828dfe12008-04-17 13:17:49 -04006111static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112{
6113 if (inode)
6114 inode_doinit_with_dentry(inode, dentry);
6115}
6116
6117static int selinux_getprocattr(struct task_struct *p,
Al Viro04ff9702007-03-12 16:17:58 +00006118 char *name, char **value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119{
David Howells275bb412008-11-14 10:39:19 +11006120 const struct task_security_struct *__tsec;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00006121 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122 int error;
Al Viro04ff9702007-03-12 16:17:58 +00006123 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124
David Howells275bb412008-11-14 10:39:19 +11006125 rcu_read_lock();
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07006126 __tsec = selinux_cred(__task_cred(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006128 if (current != p) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006129 error = avc_has_perm(&selinux_state,
6130 current_sid(), __tsec->sid,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006131 SECCLASS_PROCESS, PROCESS__GETATTR, NULL);
6132 if (error)
6133 goto bad;
6134 }
6135
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136 if (!strcmp(name, "current"))
David Howells275bb412008-11-14 10:39:19 +11006137 sid = __tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138 else if (!strcmp(name, "prev"))
David Howells275bb412008-11-14 10:39:19 +11006139 sid = __tsec->osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140 else if (!strcmp(name, "exec"))
David Howells275bb412008-11-14 10:39:19 +11006141 sid = __tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142 else if (!strcmp(name, "fscreate"))
David Howells275bb412008-11-14 10:39:19 +11006143 sid = __tsec->create_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006144 else if (!strcmp(name, "keycreate"))
David Howells275bb412008-11-14 10:39:19 +11006145 sid = __tsec->keycreate_sid;
Eric Paris42c3e032006-06-26 00:26:03 -07006146 else if (!strcmp(name, "sockcreate"))
David Howells275bb412008-11-14 10:39:19 +11006147 sid = __tsec->sockcreate_sid;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006148 else {
6149 error = -EINVAL;
6150 goto bad;
6151 }
David Howells275bb412008-11-14 10:39:19 +11006152 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006153
6154 if (!sid)
6155 return 0;
6156
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006157 error = security_sid_to_context(&selinux_state, sid, value, &len);
Al Viro04ff9702007-03-12 16:17:58 +00006158 if (error)
6159 return error;
6160 return len;
David Howells275bb412008-11-14 10:39:19 +11006161
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006162bad:
David Howells275bb412008-11-14 10:39:19 +11006163 rcu_read_unlock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006164 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165}
6166
Stephen Smalleyb21507e2017-01-09 10:07:31 -05006167static int selinux_setprocattr(const char *name, void *value, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168{
6169 struct task_security_struct *tsec;
David Howellsd84f4f92008-11-14 10:39:23 +11006170 struct cred *new;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006171 u32 mysid = current_sid(), sid = 0, ptsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172 int error;
6173 char *str = value;
6174
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175 /*
6176 * Basic control over ability to set these attributes at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177 */
6178 if (!strcmp(name, "exec"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006179 error = avc_has_perm(&selinux_state,
6180 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006181 PROCESS__SETEXEC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182 else if (!strcmp(name, "fscreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006183 error = avc_has_perm(&selinux_state,
6184 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006185 PROCESS__SETFSCREATE, NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07006186 else if (!strcmp(name, "keycreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006187 error = avc_has_perm(&selinux_state,
6188 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006189 PROCESS__SETKEYCREATE, NULL);
Eric Paris42c3e032006-06-26 00:26:03 -07006190 else if (!strcmp(name, "sockcreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006191 error = avc_has_perm(&selinux_state,
6192 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006193 PROCESS__SETSOCKCREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194 else if (!strcmp(name, "current"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006195 error = avc_has_perm(&selinux_state,
6196 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006197 PROCESS__SETCURRENT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198 else
6199 error = -EINVAL;
6200 if (error)
6201 return error;
6202
6203 /* Obtain a SID for the context, if one was specified. */
Stephen Smalleya050a572017-01-31 11:54:04 -05006204 if (size && str[0] && str[0] != '\n') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205 if (str[size-1] == '\n') {
6206 str[size-1] = 0;
6207 size--;
6208 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006209 error = security_context_to_sid(&selinux_state, value, size,
6210 &sid, GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006211 if (error == -EINVAL && !strcmp(name, "fscreate")) {
Stephen Smalleydb590002017-04-20 11:31:30 -04006212 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04006213 struct audit_buffer *ab;
6214 size_t audit_size;
6215
6216 /* We strip a nul only if it is at the end, otherwise the
6217 * context contains a nul and we should audit that */
6218 if (str[size - 1] == '\0')
6219 audit_size = size - 1;
6220 else
6221 audit_size = size;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04006222 ab = audit_log_start(audit_context(),
6223 GFP_ATOMIC,
6224 AUDIT_SELINUX_ERR);
Eric Parisd6ea83e2012-04-04 13:45:49 -04006225 audit_log_format(ab, "op=fscreate invalid_context=");
6226 audit_log_n_untrustedstring(ab, value, audit_size);
6227 audit_log_end(ab);
6228
Stephen Smalley12b29f32008-05-07 13:03:20 -04006229 return error;
Eric Parisd6ea83e2012-04-04 13:45:49 -04006230 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006231 error = security_context_to_sid_force(
6232 &selinux_state,
6233 value, size, &sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235 if (error)
6236 return error;
6237 }
6238
David Howellsd84f4f92008-11-14 10:39:23 +11006239 new = prepare_creds();
6240 if (!new)
6241 return -ENOMEM;
6242
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243 /* Permission checking based on the specified context is
6244 performed during the actual operation (execve,
6245 open/mkdir/...), when we know the full context of the
David Howellsd84f4f92008-11-14 10:39:23 +11006246 operation. See selinux_bprm_set_creds for the execve
Linus Torvalds1da177e2005-04-16 15:20:36 -07006247 checks and may_create for the file creation checks. The
6248 operation will then fail if the context is not permitted. */
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07006249 tsec = selinux_cred(new);
David Howellsd84f4f92008-11-14 10:39:23 +11006250 if (!strcmp(name, "exec")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251 tsec->exec_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006252 } else if (!strcmp(name, "fscreate")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253 tsec->create_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006254 } else if (!strcmp(name, "keycreate")) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006255 error = avc_has_perm(&selinux_state,
6256 mysid, sid, SECCLASS_KEY, KEY__CREATE,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006257 NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07006258 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11006259 goto abort_change;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006260 tsec->keycreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006261 } else if (!strcmp(name, "sockcreate")) {
Eric Paris42c3e032006-06-26 00:26:03 -07006262 tsec->sockcreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006263 } else if (!strcmp(name, "current")) {
6264 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265 if (sid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +11006266 goto abort_change;
KaiGai Koheid9250de2008-08-28 16:35:57 +09006267
David Howellsd84f4f92008-11-14 10:39:23 +11006268 /* Only allow single threaded processes to change context */
6269 error = -EPERM;
Oleg Nesterov5bb459b2009-07-10 03:48:23 +02006270 if (!current_is_single_threaded()) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006271 error = security_bounded_transition(&selinux_state,
6272 tsec->sid, sid);
David Howellsd84f4f92008-11-14 10:39:23 +11006273 if (error)
6274 goto abort_change;
Eric Paris828dfe12008-04-17 13:17:49 -04006275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276
6277 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006278 error = avc_has_perm(&selinux_state,
6279 tsec->sid, sid, SECCLASS_PROCESS,
Eric Paris828dfe12008-04-17 13:17:49 -04006280 PROCESS__DYNTRANSITION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11006282 goto abort_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283
6284 /* Check for ptracing, and update the task SID if ok.
6285 Otherwise, leave SID unchanged and fail. */
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006286 ptsid = ptrace_parent_sid();
Paul Moore0c6181c2016-03-30 21:41:21 -04006287 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006288 error = avc_has_perm(&selinux_state,
6289 ptsid, sid, SECCLASS_PROCESS,
David Howellsd84f4f92008-11-14 10:39:23 +11006290 PROCESS__PTRACE, NULL);
6291 if (error)
6292 goto abort_change;
6293 }
6294
6295 tsec->sid = sid;
6296 } else {
6297 error = -EINVAL;
6298 goto abort_change;
6299 }
6300
6301 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302 return size;
David Howellsd84f4f92008-11-14 10:39:23 +11006303
6304abort_change:
6305 abort_creds(new);
6306 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307}
6308
David Quigley746df9b2013-05-22 12:50:35 -04006309static int selinux_ismaclabel(const char *name)
6310{
6311 return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
6312}
6313
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006314static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
6315{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006316 return security_sid_to_context(&selinux_state, secid,
6317 secdata, seclen);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006318}
6319
David Howells7bf570d2008-04-29 20:52:51 +01006320static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
David Howells63cb3442008-01-15 23:47:35 +00006321{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006322 return security_context_to_sid(&selinux_state, secdata, seclen,
6323 secid, GFP_KERNEL);
David Howells63cb3442008-01-15 23:47:35 +00006324}
6325
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006326static void selinux_release_secctx(char *secdata, u32 seclen)
6327{
Paul Moore088999e2007-08-01 11:12:58 -04006328 kfree(secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006329}
6330
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006331static void selinux_inode_invalidate_secctx(struct inode *inode)
6332{
Casey Schaufler80788c22018-09-21 17:19:11 -07006333 struct inode_security_struct *isec = selinux_inode(inode);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006334
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006335 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006336 isec->initialized = LABEL_INVALID;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006337 spin_unlock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006338}
6339
David P. Quigley1ee65e32009-09-03 14:25:57 -04006340/*
6341 * called with inode->i_mutex locked
6342 */
6343static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
6344{
Ondrej Mosnacek53e0c2a2018-12-21 21:18:53 +01006345 int rc = selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX,
6346 ctx, ctxlen, 0);
6347 /* Do not return error when suppressing label (SBLABEL_MNT not set). */
6348 return rc == -EOPNOTSUPP ? 0 : rc;
David P. Quigley1ee65e32009-09-03 14:25:57 -04006349}
6350
6351/*
6352 * called with inode->i_mutex locked
6353 */
6354static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
6355{
6356 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
6357}
6358
6359static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
6360{
6361 int len = 0;
6362 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
6363 ctx, true);
6364 if (len < 0)
6365 return len;
6366 *ctxlen = len;
6367 return 0;
6368}
Michael LeMayd7200242006-06-22 14:47:17 -07006369#ifdef CONFIG_KEYS
6370
David Howellsd84f4f92008-11-14 10:39:23 +11006371static int selinux_key_alloc(struct key *k, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -07006372 unsigned long flags)
Michael LeMayd7200242006-06-22 14:47:17 -07006373{
David Howellsd84f4f92008-11-14 10:39:23 +11006374 const struct task_security_struct *tsec;
Michael LeMayd7200242006-06-22 14:47:17 -07006375 struct key_security_struct *ksec;
6376
6377 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
6378 if (!ksec)
6379 return -ENOMEM;
6380
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07006381 tsec = selinux_cred(cred);
David Howellsd84f4f92008-11-14 10:39:23 +11006382 if (tsec->keycreate_sid)
6383 ksec->sid = tsec->keycreate_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006384 else
David Howellsd84f4f92008-11-14 10:39:23 +11006385 ksec->sid = tsec->sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006386
David Howells275bb412008-11-14 10:39:19 +11006387 k->security = ksec;
Michael LeMayd7200242006-06-22 14:47:17 -07006388 return 0;
6389}
6390
6391static void selinux_key_free(struct key *k)
6392{
6393 struct key_security_struct *ksec = k->security;
6394
6395 k->security = NULL;
6396 kfree(ksec);
6397}
6398
6399static int selinux_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11006400 const struct cred *cred,
David Howellsf5895942014-03-14 17:44:49 +00006401 unsigned perm)
Michael LeMayd7200242006-06-22 14:47:17 -07006402{
6403 struct key *key;
Michael LeMayd7200242006-06-22 14:47:17 -07006404 struct key_security_struct *ksec;
David Howells275bb412008-11-14 10:39:19 +11006405 u32 sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006406
6407 /* if no specific permissions are requested, we skip the
6408 permission check. No serious, additional covert channels
6409 appear to be created. */
6410 if (perm == 0)
6411 return 0;
6412
David Howellsd84f4f92008-11-14 10:39:23 +11006413 sid = cred_sid(cred);
David Howells275bb412008-11-14 10:39:19 +11006414
6415 key = key_ref_to_ptr(key_ref);
6416 ksec = key->security;
6417
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006418 return avc_has_perm(&selinux_state,
6419 sid, ksec->sid, SECCLASS_KEY, perm, NULL);
Michael LeMayd7200242006-06-22 14:47:17 -07006420}
6421
David Howells70a5bb72008-04-29 01:01:26 -07006422static int selinux_key_getsecurity(struct key *key, char **_buffer)
6423{
6424 struct key_security_struct *ksec = key->security;
6425 char *context = NULL;
6426 unsigned len;
6427 int rc;
6428
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006429 rc = security_sid_to_context(&selinux_state, ksec->sid,
6430 &context, &len);
David Howells70a5bb72008-04-29 01:01:26 -07006431 if (!rc)
6432 rc = len;
6433 *_buffer = context;
6434 return rc;
6435}
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006436#endif
David Howells70a5bb72008-04-29 01:01:26 -07006437
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006438#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006439static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val)
6440{
6441 struct common_audit_data ad;
6442 int err;
6443 u32 sid = 0;
6444 struct ib_security_struct *sec = ib_sec;
6445 struct lsm_ibpkey_audit ibpkey;
6446
Daniel Jurgens409dcf32017-05-19 15:48:59 +03006447 err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid);
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006448 if (err)
6449 return err;
6450
6451 ad.type = LSM_AUDIT_DATA_IBPKEY;
6452 ibpkey.subnet_prefix = subnet_prefix;
6453 ibpkey.pkey = pkey_val;
6454 ad.u.ibpkey = &ibpkey;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006455 return avc_has_perm(&selinux_state,
6456 sec->sid, sid,
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006457 SECCLASS_INFINIBAND_PKEY,
6458 INFINIBAND_PKEY__ACCESS, &ad);
6459}
6460
Daniel Jurgensab861df2017-05-19 15:48:58 +03006461static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,
6462 u8 port_num)
6463{
6464 struct common_audit_data ad;
6465 int err;
6466 u32 sid = 0;
6467 struct ib_security_struct *sec = ib_sec;
6468 struct lsm_ibendport_audit ibendport;
6469
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006470 err = security_ib_endport_sid(&selinux_state, dev_name, port_num,
6471 &sid);
Daniel Jurgensab861df2017-05-19 15:48:58 +03006472
6473 if (err)
6474 return err;
6475
6476 ad.type = LSM_AUDIT_DATA_IBENDPORT;
6477 strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name));
6478 ibendport.port = port_num;
6479 ad.u.ibendport = &ibendport;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006480 return avc_has_perm(&selinux_state,
6481 sec->sid, sid,
Daniel Jurgensab861df2017-05-19 15:48:58 +03006482 SECCLASS_INFINIBAND_ENDPORT,
6483 INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad);
6484}
6485
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006486static int selinux_ib_alloc_security(void **ib_sec)
6487{
6488 struct ib_security_struct *sec;
6489
6490 sec = kzalloc(sizeof(*sec), GFP_KERNEL);
6491 if (!sec)
6492 return -ENOMEM;
6493 sec->sid = current_sid();
6494
6495 *ib_sec = sec;
6496 return 0;
6497}
6498
6499static void selinux_ib_free_security(void *ib_sec)
6500{
6501 kfree(ib_sec);
6502}
Michael LeMayd7200242006-06-22 14:47:17 -07006503#endif
6504
Chenbo Fengec27c352017-10-18 13:00:25 -07006505#ifdef CONFIG_BPF_SYSCALL
6506static int selinux_bpf(int cmd, union bpf_attr *attr,
6507 unsigned int size)
6508{
6509 u32 sid = current_sid();
6510 int ret;
6511
6512 switch (cmd) {
6513 case BPF_MAP_CREATE:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006514 ret = avc_has_perm(&selinux_state,
6515 sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
Chenbo Fengec27c352017-10-18 13:00:25 -07006516 NULL);
6517 break;
6518 case BPF_PROG_LOAD:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006519 ret = avc_has_perm(&selinux_state,
6520 sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
Chenbo Fengec27c352017-10-18 13:00:25 -07006521 NULL);
6522 break;
6523 default:
6524 ret = 0;
6525 break;
6526 }
6527
6528 return ret;
6529}
6530
6531static u32 bpf_map_fmode_to_av(fmode_t fmode)
6532{
6533 u32 av = 0;
6534
6535 if (fmode & FMODE_READ)
6536 av |= BPF__MAP_READ;
6537 if (fmode & FMODE_WRITE)
6538 av |= BPF__MAP_WRITE;
6539 return av;
6540}
6541
Chenbo Fengf66e4482017-10-18 13:00:26 -07006542/* This function will check the file pass through unix socket or binder to see
6543 * if it is a bpf related object. And apply correspinding checks on the bpf
6544 * object based on the type. The bpf maps and programs, not like other files and
6545 * socket, are using a shared anonymous inode inside the kernel as their inode.
6546 * So checking that inode cannot identify if the process have privilege to
6547 * access the bpf object and that's why we have to add this additional check in
6548 * selinux_file_receive and selinux_binder_transfer_files.
6549 */
6550static int bpf_fd_pass(struct file *file, u32 sid)
6551{
6552 struct bpf_security_struct *bpfsec;
6553 struct bpf_prog *prog;
6554 struct bpf_map *map;
6555 int ret;
6556
6557 if (file->f_op == &bpf_map_fops) {
6558 map = file->private_data;
6559 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006560 ret = avc_has_perm(&selinux_state,
6561 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006562 bpf_map_fmode_to_av(file->f_mode), NULL);
6563 if (ret)
6564 return ret;
6565 } else if (file->f_op == &bpf_prog_fops) {
6566 prog = file->private_data;
6567 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006568 ret = avc_has_perm(&selinux_state,
6569 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006570 BPF__PROG_RUN, NULL);
6571 if (ret)
6572 return ret;
6573 }
6574 return 0;
6575}
6576
Chenbo Fengec27c352017-10-18 13:00:25 -07006577static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
6578{
6579 u32 sid = current_sid();
6580 struct bpf_security_struct *bpfsec;
6581
6582 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006583 return avc_has_perm(&selinux_state,
6584 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006585 bpf_map_fmode_to_av(fmode), NULL);
6586}
6587
6588static int selinux_bpf_prog(struct bpf_prog *prog)
6589{
6590 u32 sid = current_sid();
6591 struct bpf_security_struct *bpfsec;
6592
6593 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006594 return avc_has_perm(&selinux_state,
6595 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006596 BPF__PROG_RUN, NULL);
6597}
6598
6599static int selinux_bpf_map_alloc(struct bpf_map *map)
6600{
6601 struct bpf_security_struct *bpfsec;
6602
6603 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6604 if (!bpfsec)
6605 return -ENOMEM;
6606
6607 bpfsec->sid = current_sid();
6608 map->security = bpfsec;
6609
6610 return 0;
6611}
6612
6613static void selinux_bpf_map_free(struct bpf_map *map)
6614{
6615 struct bpf_security_struct *bpfsec = map->security;
6616
6617 map->security = NULL;
6618 kfree(bpfsec);
6619}
6620
6621static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux)
6622{
6623 struct bpf_security_struct *bpfsec;
6624
6625 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6626 if (!bpfsec)
6627 return -ENOMEM;
6628
6629 bpfsec->sid = current_sid();
6630 aux->security = bpfsec;
6631
6632 return 0;
6633}
6634
6635static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
6636{
6637 struct bpf_security_struct *bpfsec = aux->security;
6638
6639 aux->security = NULL;
6640 kfree(bpfsec);
6641}
6642#endif
6643
Casey Schauflerbbd36622018-11-12 09:30:56 -08006644struct lsm_blob_sizes selinux_blob_sizes __lsm_ro_after_init = {
6645 .lbs_cred = sizeof(struct task_security_struct),
Casey Schaufler33bf60c2018-11-12 12:02:49 -08006646 .lbs_file = sizeof(struct file_security_struct),
Casey Schauflerafb1cbe32018-09-21 17:19:29 -07006647 .lbs_inode = sizeof(struct inode_security_struct),
Casey Schauflerecd5f822018-11-20 11:55:02 -08006648 .lbs_ipc = sizeof(struct ipc_security_struct),
6649 .lbs_msg_msg = sizeof(struct msg_security_struct),
Casey Schauflerbbd36622018-11-12 09:30:56 -08006650};
6651
James Morrisca97d932017-02-15 00:18:51 +11006652static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07006653 LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
6654 LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
6655 LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
6656 LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02006657
Casey Schauflere20b0432015-05-02 15:11:36 -07006658 LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
6659 LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
6660 LSM_HOOK_INIT(capget, selinux_capget),
6661 LSM_HOOK_INIT(capset, selinux_capset),
6662 LSM_HOOK_INIT(capable, selinux_capable),
6663 LSM_HOOK_INIT(quotactl, selinux_quotactl),
6664 LSM_HOOK_INIT(quota_on, selinux_quota_on),
6665 LSM_HOOK_INIT(syslog, selinux_syslog),
6666 LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
Stephen Smalley79af7302015-01-21 10:54:10 -05006667
Casey Schauflere20b0432015-05-02 15:11:36 -07006668 LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006669
Casey Schauflere20b0432015-05-02 15:11:36 -07006670 LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds),
6671 LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
6672 LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006673
Al Viro0b520752018-12-23 16:02:47 -05006674 LSM_HOOK_INIT(fs_context_dup, selinux_fs_context_dup),
David Howells442155c2018-11-01 23:07:24 +00006675 LSM_HOOK_INIT(fs_context_parse_param, selinux_fs_context_parse_param),
6676
Casey Schauflere20b0432015-05-02 15:11:36 -07006677 LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
6678 LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security),
Al Viro5b400232018-12-12 20:13:29 -05006679 LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts),
Al Viro204cc0c2018-12-13 13:41:47 -05006680 LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07006681 LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
6682 LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
6683 LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
6684 LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
6685 LSM_HOOK_INIT(sb_mount, selinux_mount),
6686 LSM_HOOK_INIT(sb_umount, selinux_umount),
6687 LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
6688 LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
Al Viro757cbe52018-12-14 23:42:21 -05006689 LSM_HOOK_INIT(sb_add_mnt_opt, selinux_add_mnt_opt),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690
Casey Schauflere20b0432015-05-02 15:11:36 -07006691 LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
Vivek Goyala518b0a2016-07-13 10:44:53 -04006692 LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
Eric Parise0007522008-03-05 10:31:54 -05006693
Casey Schauflere20b0432015-05-02 15:11:36 -07006694 LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
6695 LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
6696 LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
6697 LSM_HOOK_INIT(inode_create, selinux_inode_create),
6698 LSM_HOOK_INIT(inode_link, selinux_inode_link),
6699 LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
6700 LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
6701 LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
6702 LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
6703 LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
6704 LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
6705 LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
6706 LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
6707 LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
6708 LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
6709 LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
6710 LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
6711 LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
6712 LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
6713 LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
6714 LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
6715 LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
6716 LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
6717 LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
6718 LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
Vivek Goyal56909eb2016-07-13 10:44:48 -04006719 LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
Vivek Goyal19472b62016-07-13 10:44:50 -04006720 LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721
Casey Schauflere20b0432015-05-02 15:11:36 -07006722 LSM_HOOK_INIT(file_permission, selinux_file_permission),
6723 LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07006724 LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
6725 LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
6726 LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
6727 LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
6728 LSM_HOOK_INIT(file_lock, selinux_file_lock),
6729 LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
6730 LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
6731 LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
6732 LSM_HOOK_INIT(file_receive, selinux_file_receive),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006733
Casey Schauflere20b0432015-05-02 15:11:36 -07006734 LSM_HOOK_INIT(file_open, selinux_file_open),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006735
Tetsuo Handaa79be232017-03-28 23:08:45 +09006736 LSM_HOOK_INIT(task_alloc, selinux_task_alloc),
Casey Schauflere20b0432015-05-02 15:11:36 -07006737 LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
6738 LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08006739 LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07006740 LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
6741 LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
6742 LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04006743 LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data),
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07006744 LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
Casey Schauflere20b0432015-05-02 15:11:36 -07006745 LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
6746 LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
6747 LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
6748 LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid),
6749 LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
6750 LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
6751 LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
Stephen Smalley791ec492017-02-17 07:57:00 -05006752 LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit),
Casey Schauflere20b0432015-05-02 15:11:36 -07006753 LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
6754 LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
6755 LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
6756 LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
6757 LSM_HOOK_INIT(task_kill, selinux_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07006758 LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09006759
Casey Schauflere20b0432015-05-02 15:11:36 -07006760 LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
6761 LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762
Casey Schauflere20b0432015-05-02 15:11:36 -07006763 LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006764
Casey Schauflere20b0432015-05-02 15:11:36 -07006765 LSM_HOOK_INIT(msg_queue_alloc_security,
6766 selinux_msg_queue_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07006767 LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
6768 LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
6769 LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
6770 LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006771
Casey Schauflere20b0432015-05-02 15:11:36 -07006772 LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07006773 LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
6774 LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
6775 LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776
Casey Schauflere20b0432015-05-02 15:11:36 -07006777 LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07006778 LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
6779 LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
6780 LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006781
Casey Schauflere20b0432015-05-02 15:11:36 -07006782 LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783
Casey Schauflere20b0432015-05-02 15:11:36 -07006784 LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
6785 LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786
Casey Schauflere20b0432015-05-02 15:11:36 -07006787 LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
6788 LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
6789 LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
6790 LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006791 LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
Casey Schauflere20b0432015-05-02 15:11:36 -07006792 LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
6793 LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
6794 LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795
Casey Schauflere20b0432015-05-02 15:11:36 -07006796 LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
6797 LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006798
Casey Schauflere20b0432015-05-02 15:11:36 -07006799 LSM_HOOK_INIT(socket_create, selinux_socket_create),
6800 LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
David Herrmann0b811db2018-05-04 16:28:21 +02006801 LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair),
Casey Schauflere20b0432015-05-02 15:11:36 -07006802 LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
6803 LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
6804 LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
6805 LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
6806 LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
6807 LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
6808 LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
6809 LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
6810 LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
6811 LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
6812 LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
6813 LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
6814 LSM_HOOK_INIT(socket_getpeersec_stream,
6815 selinux_socket_getpeersec_stream),
6816 LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
6817 LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
6818 LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
6819 LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
6820 LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
6821 LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
Richard Hainesd4529302018-02-13 20:57:18 +00006822 LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request),
6823 LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone),
6824 LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect),
Casey Schauflere20b0432015-05-02 15:11:36 -07006825 LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
6826 LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
6827 LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
6828 LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
6829 LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
6830 LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
6831 LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
6832 LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
6833 LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security),
6834 LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
6835 LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
6836 LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
6837 LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006838#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006839 LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access),
Daniel Jurgensab861df2017-05-19 15:48:58 +03006840 LSM_HOOK_INIT(ib_endport_manage_subnet,
6841 selinux_ib_endport_manage_subnet),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006842 LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security),
6843 LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security),
6844#endif
Trent Jaegerd28d1e02005-12-13 23:12:40 -08006845#ifdef CONFIG_SECURITY_NETWORK_XFRM
Casey Schauflere20b0432015-05-02 15:11:36 -07006846 LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
6847 LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
6848 LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
6849 LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
6850 LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
6851 LSM_HOOK_INIT(xfrm_state_alloc_acquire,
6852 selinux_xfrm_state_alloc_acquire),
6853 LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
6854 LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
6855 LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
6856 LSM_HOOK_INIT(xfrm_state_pol_flow_match,
6857 selinux_xfrm_state_pol_flow_match),
6858 LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006859#endif
Michael LeMayd7200242006-06-22 14:47:17 -07006860
6861#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07006862 LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
6863 LSM_HOOK_INIT(key_free, selinux_key_free),
6864 LSM_HOOK_INIT(key_permission, selinux_key_permission),
6865 LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
Michael LeMayd7200242006-06-22 14:47:17 -07006866#endif
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02006867
6868#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07006869 LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
6870 LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
6871 LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
6872 LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02006873#endif
Chenbo Fengec27c352017-10-18 13:00:25 -07006874
6875#ifdef CONFIG_BPF_SYSCALL
6876 LSM_HOOK_INIT(bpf, selinux_bpf),
6877 LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
6878 LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
6879 LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc),
6880 LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc),
6881 LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free),
6882 LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),
6883#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006884};
6885
6886static __init int selinux_init(void)
6887{
peter enderborgc103a912018-06-12 10:09:03 +02006888 pr_info("SELinux: Initializing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006889
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006890 memset(&selinux_state, 0, sizeof(selinux_state));
Paul Mooree5a5ca92018-03-01 17:38:30 -05006891 enforcing_set(&selinux_state, selinux_enforcing_boot);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006892 selinux_state.checkreqprot = selinux_checkreqprot_boot;
6893 selinux_ss_init(&selinux_state.ss);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006894 selinux_avc_init(&selinux_state.avc);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006895
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896 /* Set the security state for the initial task. */
David Howellsd84f4f92008-11-14 10:39:23 +11006897 cred_init_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006898
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04006899 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
6900
Linus Torvalds1da177e2005-04-16 15:20:36 -07006901 avc_init();
6902
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006903 avtab_cache_init();
6904
6905 ebitmap_cache_init();
6906
6907 hashtab_cache_init();
6908
Casey Schauflerd69dece52017-01-18 17:09:05 -08006909 security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006910
Paul Moore615e51f2014-06-26 14:33:56 -04006911 if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
6912 panic("SELinux: Unable to register AVC netcache callback\n");
6913
Daniel Jurgens8f408ab2017-05-19 15:48:53 +03006914 if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET))
6915 panic("SELinux: Unable to register AVC LSM notifier callback\n");
6916
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006917 if (selinux_enforcing_boot)
peter enderborgc103a912018-06-12 10:09:03 +02006918 pr_debug("SELinux: Starting in enforcing mode\n");
Eric Paris828dfe12008-04-17 13:17:49 -04006919 else
peter enderborgc103a912018-06-12 10:09:03 +02006920 pr_debug("SELinux: Starting in permissive mode\n");
Michael LeMayd7200242006-06-22 14:47:17 -07006921
David Howells442155c2018-11-01 23:07:24 +00006922 fs_validate_description(&selinux_fs_parameters);
6923
Linus Torvalds1da177e2005-04-16 15:20:36 -07006924 return 0;
6925}
6926
Al Viroe8c26252010-03-23 06:36:54 -04006927static void delayed_superblock_init(struct super_block *sb, void *unused)
6928{
Al Viro204cc0c2018-12-13 13:41:47 -05006929 selinux_set_mnt_opts(sb, NULL, 0, NULL);
Al Viroe8c26252010-03-23 06:36:54 -04006930}
6931
Linus Torvalds1da177e2005-04-16 15:20:36 -07006932void selinux_complete_init(void)
6933{
peter enderborgc103a912018-06-12 10:09:03 +02006934 pr_debug("SELinux: Completing initialization.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006935
6936 /* Set up any superblocks initialized prior to the policy load. */
peter enderborgc103a912018-06-12 10:09:03 +02006937 pr_debug("SELinux: Setting up existing superblocks.\n");
Al Viroe8c26252010-03-23 06:36:54 -04006938 iterate_supers(delayed_superblock_init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006939}
6940
6941/* SELinux requires early initialization in order to label
6942 all processes and objects when they are created. */
Kees Cook3d6e5f62018-10-10 17:18:23 -07006943DEFINE_LSM(selinux) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07006944 .name = "selinux",
Kees Cook14bd99c2018-09-19 19:57:06 -07006945 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
Kees Cookc5459b82018-09-13 22:28:48 -07006946 .enabled = &selinux_enabled,
Casey Schauflerbbd36622018-11-12 09:30:56 -08006947 .blobs = &selinux_blob_sizes,
Kees Cook3d6e5f62018-10-10 17:18:23 -07006948 .init = selinux_init,
6949};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006950
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08006951#if defined(CONFIG_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006952
Florian Westphal591bb2782017-07-26 11:40:52 +02006953static const struct nf_hook_ops selinux_nf_ops[] = {
Paul Mooreeffad8d2008-01-29 08:49:27 -05006954 {
6955 .hook = selinux_ipv4_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00006956 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05006957 .hooknum = NF_INET_POST_ROUTING,
6958 .priority = NF_IP_PRI_SELINUX_LAST,
6959 },
6960 {
6961 .hook = selinux_ipv4_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00006962 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05006963 .hooknum = NF_INET_FORWARD,
6964 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Moore948bf852008-10-10 10:16:32 -04006965 },
6966 {
6967 .hook = selinux_ipv4_output,
Alban Crequy2597a832012-05-14 03:56:39 +00006968 .pf = NFPROTO_IPV4,
Paul Moore948bf852008-10-10 10:16:32 -04006969 .hooknum = NF_INET_LOCAL_OUT,
6970 .priority = NF_IP_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02006971 },
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04006972#if IS_ENABLED(CONFIG_IPV6)
Paul Mooreeffad8d2008-01-29 08:49:27 -05006973 {
6974 .hook = selinux_ipv6_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00006975 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05006976 .hooknum = NF_INET_POST_ROUTING,
6977 .priority = NF_IP6_PRI_SELINUX_LAST,
6978 },
6979 {
6980 .hook = selinux_ipv6_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00006981 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05006982 .hooknum = NF_INET_FORWARD,
6983 .priority = NF_IP6_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02006984 },
Huw Davies2917f572016-06-27 15:06:15 -04006985 {
6986 .hook = selinux_ipv6_output,
6987 .pf = NFPROTO_IPV6,
6988 .hooknum = NF_INET_LOCAL_OUT,
6989 .priority = NF_IP6_PRI_SELINUX_FIRST,
6990 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07006991#endif /* IPV6 */
Jiri Pirko25db6be2014-09-03 17:42:13 +02006992};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006993
Florian Westphal8e71bf72017-04-21 11:49:09 +02006994static int __net_init selinux_nf_register(struct net *net)
6995{
6996 return nf_register_net_hooks(net, selinux_nf_ops,
6997 ARRAY_SIZE(selinux_nf_ops));
6998}
6999
7000static void __net_exit selinux_nf_unregister(struct net *net)
7001{
7002 nf_unregister_net_hooks(net, selinux_nf_ops,
7003 ARRAY_SIZE(selinux_nf_ops));
7004}
7005
7006static struct pernet_operations selinux_net_ops = {
7007 .init = selinux_nf_register,
7008 .exit = selinux_nf_unregister,
7009};
7010
Linus Torvalds1da177e2005-04-16 15:20:36 -07007011static int __init selinux_nf_ip_init(void)
7012{
Jiri Pirko25db6be2014-09-03 17:42:13 +02007013 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007014
7015 if (!selinux_enabled)
Jiri Pirko25db6be2014-09-03 17:42:13 +02007016 return 0;
Eric Parisfadcdb42007-02-22 18:11:31 -05007017
peter enderborgc103a912018-06-12 10:09:03 +02007018 pr_debug("SELinux: Registering netfilter hooks\n");
Eric Parisfadcdb42007-02-22 18:11:31 -05007019
Florian Westphal8e71bf72017-04-21 11:49:09 +02007020 err = register_pernet_subsys(&selinux_net_ops);
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07007021 if (err)
Florian Westphal8e71bf72017-04-21 11:49:09 +02007022 panic("SELinux: register_pernet_subsys: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007023
Jiri Pirko25db6be2014-09-03 17:42:13 +02007024 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007025}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026__initcall(selinux_nf_ip_init);
7027
7028#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7029static void selinux_nf_ip_exit(void)
7030{
peter enderborgc103a912018-06-12 10:09:03 +02007031 pr_debug("SELinux: Unregistering netfilter hooks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007032
Florian Westphal8e71bf72017-04-21 11:49:09 +02007033 unregister_pernet_subsys(&selinux_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007034}
7035#endif
7036
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007037#else /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007038
7039#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7040#define selinux_nf_ip_exit()
7041#endif
7042
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007043#endif /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007044
7045#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007046int selinux_disable(struct selinux_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007047{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007048 if (state->initialized) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049 /* Not permitted after initial policy load. */
7050 return -EINVAL;
7051 }
7052
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007053 if (state->disabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007054 /* Only do this once. */
7055 return -EINVAL;
7056 }
7057
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007058 state->disabled = 1;
7059
peter enderborgc103a912018-06-12 10:09:03 +02007060 pr_info("SELinux: Disabled at runtime.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007061
Stephen Smalley30d55282006-05-03 10:52:36 -04007062 selinux_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07007064 security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007065
Eric Parisaf8ff042009-09-20 21:23:01 -04007066 /* Try to destroy the avc node cache */
7067 avc_disable();
7068
Linus Torvalds1da177e2005-04-16 15:20:36 -07007069 /* Unregister netfilter hooks. */
7070 selinux_nf_ip_exit();
7071
7072 /* Unregister selinuxfs. */
7073 exit_sel_fs();
7074
7075 return 0;
7076}
7077#endif