Merge tag 'regulator-v5.2' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[linux-2.6-block.git] / security / selinux / hooks.c
CommitLineData
1da177e4
LT
1/*
2 * NSA Security-Enhanced Linux (SELinux) security module
3 *
4 * This file contains the SELinux hook function implementations.
5 *
7efbb60b 6 * Authors: Stephen Smalley, <sds@tycho.nsa.gov>
828dfe1d
EP
7 * Chris Vance, <cvance@nai.com>
8 * Wayne Salamon, <wsalamon@nai.com>
9 * James Morris <jmorris@redhat.com>
1da177e4
LT
10 *
11 * Copyright (C) 2001,2002 Networks Associates Technology, Inc.
2069f457
EP
12 * Copyright (C) 2003-2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
13 * Eric Paris <eparis@redhat.com>
1da177e4 14 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
828dfe1d 15 * <dgoeddel@trustedcs.com>
ed6d76e4 16 * Copyright (C) 2006, 2007, 2009 Hewlett-Packard Development Company, L.P.
82c21bfa 17 * Paul Moore <paul@paul-moore.com>
788e7dd4 18 * Copyright (C) 2007 Hitachi Software Engineering Co., Ltd.
828dfe1d 19 * Yuichi Nakamura <ynakam@hitachisoft.jp>
3a976fa6 20 * Copyright (C) 2016 Mellanox Technologies
1da177e4
LT
21 *
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,
828dfe1d 24 * as published by the Free Software Foundation.
1da177e4
LT
25 */
26
1da177e4 27#include <linux/init.h>
0b24dcb7 28#include <linux/kd.h>
1da177e4 29#include <linux/kernel.h>
0d094efe 30#include <linux/tracehook.h>
1da177e4 31#include <linux/errno.h>
3f07c014 32#include <linux/sched/signal.h>
29930025 33#include <linux/sched/task.h>
3c4ed7bd 34#include <linux/lsm_hooks.h>
1da177e4
LT
35#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>
0b24dcb7 42#include <linux/proc_fs.h>
1da177e4 43#include <linux/swap.h>
1da177e4
LT
44#include <linux/spinlock.h>
45#include <linux/syscalls.h>
2a7dba39 46#include <linux/dcache.h>
1da177e4 47#include <linux/file.h>
9f3acc31 48#include <linux/fdtable.h>
1da177e4
LT
49#include <linux/namei.h>
50#include <linux/mount.h>
442155c1
DH
51#include <linux/fs_context.h>
52#include <linux/fs_parser.h>
1da177e4
LT
53#include <linux/netfilter_ipv4.h>
54#include <linux/netfilter_ipv6.h>
55#include <linux/tty.h>
56#include <net/icmp.h>
227b60f5 57#include <net/ip.h> /* for local_port_range[] */
1da177e4 58#include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */
47180068 59#include <net/inet_connection_sock.h>
220deb96 60#include <net/net_namespace.h>
d621d35e 61#include <net/netlabel.h>
f5269710 62#include <linux/uaccess.h>
1da177e4 63#include <asm/ioctls.h>
60063497 64#include <linux/atomic.h>
1da177e4
LT
65#include <linux/bitops.h>
66#include <linux/interrupt.h>
67#include <linux/netdevice.h> /* for network interface checks */
77954983 68#include <net/netlink.h>
1da177e4
LT
69#include <linux/tcp.h>
70#include <linux/udp.h>
2ee92d46 71#include <linux/dccp.h>
d452930f
RH
72#include <linux/sctp.h>
73#include <net/sctp/structs.h>
1da177e4
LT
74#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>
1da177e4 82#include <linux/audit.h>
6931dfc9 83#include <linux/string.h>
23970741 84#include <linux/mutex.h>
f06febc9 85#include <linux/posix-timers.h>
00234592 86#include <linux/syslog.h>
3486740a 87#include <linux/user_namespace.h>
44fc7ea0 88#include <linux/export.h>
40401530
AV
89#include <linux/msg.h>
90#include <linux/shm.h>
ec27c356 91#include <linux/bpf.h>
e262e32d 92#include <uapi/linux/mount.h>
1da177e4
LT
93
94#include "avc.h"
95#include "objsec.h"
96#include "netif.h"
224dfbd8 97#include "netnode.h"
3e112172 98#include "netport.h"
409dcf31 99#include "ibpkey.h"
d28d1e08 100#include "xfrm.h"
c60475bf 101#include "netlabel.h"
9d57a7f9 102#include "audit.h"
7b98a585 103#include "avc_ss.h"
1da177e4 104
aa8e712c
SS
105struct selinux_state selinux_state;
106
d621d35e 107/* SECMARK reference count */
56a4ca99 108static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
d621d35e 109
1da177e4 110#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
aa8e712c 111static int selinux_enforcing_boot;
1da177e4
LT
112
113static int __init enforcing_setup(char *str)
114{
f5269710 115 unsigned long enforcing;
29707b20 116 if (!kstrtoul(str, 0, &enforcing))
aa8e712c 117 selinux_enforcing_boot = enforcing ? 1 : 0;
1da177e4
LT
118 return 1;
119}
120__setup("enforcing=", enforcing_setup);
aa8e712c
SS
121#else
122#define selinux_enforcing_boot 1
1da177e4
LT
123#endif
124
be6ec88f 125int selinux_enabled __lsm_ro_after_init = 1;
1da177e4 126#ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
1da177e4
LT
127static int __init selinux_enabled_setup(char *str)
128{
f5269710 129 unsigned long enabled;
29707b20 130 if (!kstrtoul(str, 0, &enabled))
f5269710 131 selinux_enabled = enabled ? 1 : 0;
1da177e4
LT
132 return 1;
133}
134__setup("selinux=", selinux_enabled_setup);
135#endif
136
aa8e712c
SS
137static 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
d621d35e
PM
150/**
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
2be4d74f
CP
157 * enabled, false (0) if SECMARK is disabled. If the always_check_network
158 * policy capability is enabled, SECMARK is always considered enabled.
d621d35e
PM
159 *
160 */
161static int selinux_secmark_enabled(void)
162{
aa8e712c
SS
163 return (selinux_policycap_alwaysnetwork() ||
164 atomic_read(&selinux_secmark_refcount));
2be4d74f
CP
165}
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{
aa8e712c
SS
179 return (selinux_policycap_alwaysnetwork() ||
180 netlbl_enabled() || selinux_xfrm_enabled());
d621d35e
PM
181}
182
615e51fd
PM
183static 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
8f408ab6
DJ
194static int selinux_lsm_notifier_avc_callback(u32 event)
195{
409dcf31
DJ
196 if (event == AVC_CALLBACK_RESET) {
197 sel_ib_pkey_flush();
8f408ab6 198 call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
409dcf31 199 }
8f408ab6
DJ
200
201 return 0;
202}
203
d84f4f99
DH
204/*
205 * initialise the security for the init task
206 */
207static void cred_init_security(void)
1da177e4 208{
3b11a1de 209 struct cred *cred = (struct cred *) current->real_cred;
1da177e4
LT
210 struct task_security_struct *tsec;
211
bbd3662a 212 tsec = selinux_cred(cred);
d84f4f99 213 tsec->osid = tsec->sid = SECINITSID_KERNEL;
1da177e4
LT
214}
215
88e67f3b
DH
216/*
217 * 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
0c6cfa62 223 tsec = selinux_cred(cred);
88e67f3b
DH
224 return tsec->sid;
225}
226
275bb41e 227/*
3b11a1de 228 * get the objective security ID of a task
275bb41e
DH
229 */
230static inline u32 task_sid(const struct task_struct *task)
231{
275bb41e
DH
232 u32 sid;
233
234 rcu_read_lock();
88e67f3b 235 sid = cred_sid(__task_cred(task));
275bb41e
DH
236 rcu_read_unlock();
237 return sid;
238}
239
88e67f3b
DH
240/* Allocate and free functions for each kind of security blob. */
241
1da177e4
LT
242static int inode_alloc_security(struct inode *inode)
243{
afb1cbe3 244 struct inode_security_struct *isec = selinux_inode(inode);
275bb41e 245 u32 sid = current_sid();
1da177e4 246
9287aed2 247 spin_lock_init(&isec->lock);
1da177e4 248 INIT_LIST_HEAD(&isec->list);
1da177e4
LT
249 isec->inode = inode;
250 isec->sid = SECINITSID_UNLABELED;
251 isec->sclass = SECCLASS_FILE;
275bb41e 252 isec->task_sid = sid;
42059112 253 isec->initialized = LABEL_INVALID;
1da177e4
LT
254
255 return 0;
256}
257
5d226df4
AG
258static 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
42059112 263 * allowed; when set to false, returns -ECHILD when the label is
e9193288 264 * invalid. The @dentry parameter should be set to a dentry of the inode.
5d226df4
AG
265 */
266static int __inode_security_revalidate(struct inode *inode,
e9193288 267 struct dentry *dentry,
5d226df4
AG
268 bool may_sleep)
269{
80788c22 270 struct inode_security_struct *isec = selinux_inode(inode);
5d226df4
AG
271
272 might_sleep_if(may_sleep);
273
aa8e712c
SS
274 if (selinux_state.initialized &&
275 isec->initialized != LABEL_INITIALIZED) {
5d226df4
AG
276 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 */
e9193288 284 inode_doinit_with_dentry(inode, dentry);
5d226df4
AG
285 }
286 return 0;
287}
288
5d226df4
AG
289static struct inode_security_struct *inode_security_novalidate(struct inode *inode)
290{
80788c22 291 return selinux_inode(inode);
5d226df4
AG
292}
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);
80788c22 301 return selinux_inode(inode);
5d226df4
AG
302}
303
83da53c5
AG
304/*
305 * Get the security label of an inode.
306 */
307static struct inode_security_struct *inode_security(struct inode *inode)
308{
5d226df4 309 __inode_security_revalidate(inode, NULL, true);
80788c22 310 return selinux_inode(inode);
83da53c5
AG
311}
312
2c97165b
PM
313static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry)
314{
315 struct inode *inode = d_backing_inode(dentry);
316
80788c22 317 return selinux_inode(inode);
2c97165b
PM
318}
319
83da53c5
AG
320/*
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
5d226df4 327 __inode_security_revalidate(inode, dentry, true);
80788c22 328 return selinux_inode(inode);
83da53c5
AG
329}
330
1da177e4
LT
331static void inode_free_security(struct inode *inode)
332{
80788c22 333 struct inode_security_struct *isec = selinux_inode(inode);
afb1cbe3 334 struct superblock_security_struct *sbsec;
1da177e4 335
afb1cbe3
CS
336 if (!isec)
337 return;
338 sbsec = inode->i_sb->s_security;
9629d04a
WL
339 /*
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);
1da177e4 351 list_del_init(&isec->list);
9629d04a
WL
352 spin_unlock(&sbsec->isec_lock);
353 }
1da177e4
LT
354}
355
356static int file_alloc_security(struct file *file)
357{
33bf60ca 358 struct file_security_struct *fsec = selinux_file(file);
275bb41e 359 u32 sid = current_sid();
1da177e4 360
275bb41e
DH
361 fsec->sid = sid;
362 fsec->fown_sid = sid;
1da177e4
LT
363
364 return 0;
365}
366
1da177e4
LT
367static int superblock_alloc_security(struct super_block *sb)
368{
369 struct superblock_security_struct *sbsec;
370
89d155ef 371 sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
1da177e4
LT
372 if (!sbsec)
373 return -ENOMEM;
374
bc7e982b 375 mutex_init(&sbsec->lock);
1da177e4
LT
376 INIT_LIST_HEAD(&sbsec->isec_head);
377 spin_lock_init(&sbsec->isec_lock);
1da177e4
LT
378 sbsec->sb = sb;
379 sbsec->sid = SECINITSID_UNLABELED;
380 sbsec->def_sid = SECINITSID_FILE;
c312feb2 381 sbsec->mntpoint_sid = SECINITSID_UNLABELED;
1da177e4
LT
382 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;
1da177e4
LT
390 sb->s_security = NULL;
391 kfree(sbsec);
392}
393
bd323655
AV
394struct selinux_mnt_opts {
395 const char *fscontext, *context, *rootcontext, *defcontext;
396};
397
204cc0cc
AV
398static void selinux_free_mnt_opts(void *mnt_opts)
399{
bd323655
AV
400 struct selinux_mnt_opts *opts = mnt_opts;
401 kfree(opts->fscontext);
402 kfree(opts->context);
403 kfree(opts->rootcontext);
404 kfree(opts->defcontext);
204cc0cc
AV
405 kfree(opts);
406}
407
1da177e4
LT
408static inline int inode_doinit(struct inode *inode)
409{
410 return inode_doinit_with_dentry(inode, NULL);
411}
412
413enum {
31e87930 414 Opt_error = -1,
442155c1
DH
415 Opt_context = 0,
416 Opt_defcontext = 1,
1da177e4 417 Opt_fscontext = 2,
442155c1
DH
418 Opt_rootcontext = 3,
419 Opt_seclabel = 4,
1da177e4
LT
420};
421
da3d76ab 422#define A(s, has_arg) {#s, sizeof(#s) - 1, Opt_##s, has_arg}
169d68ef
AV
423static struct {
424 const char *name;
425 int len;
426 int opt;
427 bool has_arg;
428} tokens[] = {
da3d76ab
AV
429 A(context, true),
430 A(fscontext, true),
431 A(defcontext, true),
432 A(rootcontext, true),
433 A(seclabel, false),
1da177e4 434};
169d68ef
AV
435#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}
1da177e4
LT
455
456#define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
457
c312feb2
EP
458static int may_context_mount_sb_relabel(u32 sid,
459 struct superblock_security_struct *sbsec,
275bb41e 460 const struct cred *cred)
c312feb2 461{
0c6cfa62 462 const struct task_security_struct *tsec = selinux_cred(cred);
c312feb2
EP
463 int rc;
464
6b6bc620
SS
465 rc = avc_has_perm(&selinux_state,
466 tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
c312feb2
EP
467 FILESYSTEM__RELABELFROM, NULL);
468 if (rc)
469 return rc;
470
6b6bc620
SS
471 rc = avc_has_perm(&selinux_state,
472 tsec->sid, sid, SECCLASS_FILESYSTEM,
c312feb2
EP
473 FILESYSTEM__RELABELTO, NULL);
474 return rc;
475}
476
0808925e
EP
477static int may_context_mount_inode_relabel(u32 sid,
478 struct superblock_security_struct *sbsec,
275bb41e 479 const struct cred *cred)
0808925e 480{
0c6cfa62 481 const struct task_security_struct *tsec = selinux_cred(cred);
0808925e 482 int rc;
6b6bc620
SS
483 rc = avc_has_perm(&selinux_state,
484 tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
0808925e
EP
485 FILESYSTEM__RELABELFROM, NULL);
486 if (rc)
487 return rc;
488
6b6bc620
SS
489 rc = avc_has_perm(&selinux_state,
490 sid, sbsec->sid, SECCLASS_FILESYSTEM,
0808925e
EP
491 FILESYSTEM__ASSOCIATE, NULL);
492 return rc;
493}
494
a83d6dda 495static int selinux_is_genfs_special_handling(struct super_block *sb)
b43e725d 496{
a83d6dda
OM
497 /* Special handling. Genfs but also in-core setxattr handler */
498 return !strcmp(sb->s_type->name, "sysfs") ||
d5f3a5f6
MS
499 !strcmp(sb->s_type->name, "pstore") ||
500 !strcmp(sb->s_type->name, "debugfs") ||
a2c7c6fb 501 !strcmp(sb->s_type->name, "tracefs") ||
2651225b 502 !strcmp(sb->s_type->name, "rootfs") ||
aa8e712c 503 (selinux_policycap_cgroupseclabel() &&
2651225b
SS
504 (!strcmp(sb->s_type->name, "cgroup") ||
505 !strcmp(sb->s_type->name, "cgroup2")));
b43e725d
EP
506}
507
a83d6dda
OM
508static 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
c9180a57 536static int sb_finish_set_opts(struct super_block *sb)
1da177e4 537{
1da177e4 538 struct superblock_security_struct *sbsec = sb->s_security;
c9180a57 539 struct dentry *root = sb->s_root;
c6f493d6 540 struct inode *root_inode = d_backing_inode(root);
c9180a57 541 int rc = 0;
1da177e4 542
c9180a57
EP
543 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. */
5d6c3191 549 if (!(root_inode->i_opflags & IOP_XATTR)) {
c103a91e 550 pr_warn("SELinux: (dev %s, type %s) has no "
29b1deb2 551 "xattr support\n", sb->s_id, sb->s_type->name);
c9180a57
EP
552 rc = -EOPNOTSUPP;
553 goto out;
554 }
5d6c3191
AG
555
556 rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0);
c9180a57
EP
557 if (rc < 0 && rc != -ENODATA) {
558 if (rc == -EOPNOTSUPP)
c103a91e 559 pr_warn("SELinux: (dev %s, type "
29b1deb2
LT
560 "%s) has no security xattr handler\n",
561 sb->s_id, sb->s_type->name);
c9180a57 562 else
c103a91e 563 pr_warn("SELinux: (dev %s, type "
29b1deb2
LT
564 "%s) getxattr errno %d\n", sb->s_id,
565 sb->s_type->name, -rc);
c9180a57
EP
566 goto out;
567 }
568 }
1da177e4 569
eadcabc6 570 sbsec->flags |= SE_SBINITIALIZED;
0b4d3452
SM
571
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 */
b43e725d 577 if (selinux_is_sblabel_mnt(sb))
12f348b9 578 sbsec->flags |= SBLABEL_MNT;
0b4d3452
SM
579 else
580 sbsec->flags &= ~SBLABEL_MNT;
ddd29ec6 581
c9180a57
EP
582 /* Initialize the root inode. */
583 rc = inode_doinit_with_dentry(root_inode, root);
1da177e4 584
c9180a57
EP
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);
8d64124a 590 while (!list_empty(&sbsec->isec_head)) {
c9180a57 591 struct inode_security_struct *isec =
8d64124a 592 list_first_entry(&sbsec->isec_head,
c9180a57
EP
593 struct inode_security_struct, list);
594 struct inode *inode = isec->inode;
923190d3 595 list_del_init(&isec->list);
c9180a57
EP
596 spin_unlock(&sbsec->isec_lock);
597 inode = igrab(inode);
598 if (inode) {
599 if (!IS_PRIVATE(inode))
600 inode_doinit(inode);
601 iput(inode);
602 }
603 spin_lock(&sbsec->isec_lock);
c9180a57
EP
604 }
605 spin_unlock(&sbsec->isec_lock);
606out:
607 return rc;
608}
1da177e4 609
c9180a57
EP
610static int bad_option(struct superblock_security_struct *sbsec, char flag,
611 u32 old_sid, u32 new_sid)
612{
0d90a7ec
DQ
613 char mnt_flags = sbsec->flags & SE_MNTMASK;
614
c9180a57 615 /* check if the old mount command had the same options */
0d90a7ec 616 if (sbsec->flags & SE_SBINITIALIZED)
c9180a57
EP
617 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 */
0d90a7ec
DQ
624 if (!(sbsec->flags & SE_SBINITIALIZED))
625 if (mnt_flags & flag)
c9180a57
EP
626 return 1;
627 return 0;
628}
e0007529 629
bd323655
AV
630static 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
c9180a57
EP
641/*
642 * Allow filesystems with binary mount data to explicitly set mount point
643 * labeling information.
644 */
e0007529 645static int selinux_set_mnt_opts(struct super_block *sb,
204cc0cc 646 void *mnt_opts,
649f6e77
DQ
647 unsigned long kern_flags,
648 unsigned long *set_kern_flags)
c9180a57 649{
275bb41e 650 const struct cred *cred = current_cred();
c9180a57 651 struct superblock_security_struct *sbsec = sb->s_security;
83da53c5 652 struct dentry *root = sbsec->sb->s_root;
bd323655 653 struct selinux_mnt_opts *opts = mnt_opts;
2c97165b 654 struct inode_security_struct *root_isec;
c9180a57
EP
655 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
656 u32 defcontext_sid = 0;
bd323655 657 int rc = 0;
c9180a57
EP
658
659 mutex_lock(&sbsec->lock);
660
aa8e712c 661 if (!selinux_state.initialized) {
bd323655 662 if (!opts) {
c9180a57
EP
663 /* Defer initialization until selinux_complete_init,
664 after the initial policy is loaded and the security
665 server is ready to handle calls. */
c9180a57
EP
666 goto out;
667 }
668 rc = -EINVAL;
c103a91e 669 pr_warn("SELinux: Unable to set superblock options "
744ba35e 670 "before the security server is initialized\n");
1da177e4 671 goto out;
c9180a57 672 }
649f6e77
DQ
673 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 }
1da177e4 679
e0007529
EP
680 /*
681 * 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 */
0d90a7ec 691 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
bd323655 692 && !opts)
f5269710 693 goto out;
e0007529 694
2c97165b
PM
695 root_isec = backing_inode_security_novalidate(root);
696
c9180a57
EP
697 /*
698 * 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 */
bd323655
AV
702 if (opts) {
703 if (opts->fscontext) {
704 rc = parse_sid(sb, opts->fscontext, &fscontext_sid);
705 if (rc)
706 goto out;
c9180a57
EP
707 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
708 fscontext_sid))
709 goto out_double_mount;
c9180a57 710 sbsec->flags |= FSCONTEXT_MNT;
bd323655
AV
711 }
712 if (opts->context) {
713 rc = parse_sid(sb, opts->context, &context_sid);
714 if (rc)
715 goto out;
c9180a57
EP
716 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
717 context_sid))
718 goto out_double_mount;
c9180a57 719 sbsec->flags |= CONTEXT_MNT;
bd323655
AV
720 }
721 if (opts->rootcontext) {
722 rc = parse_sid(sb, opts->rootcontext, &rootcontext_sid);
723 if (rc)
724 goto out;
c9180a57
EP
725 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
726 rootcontext_sid))
727 goto out_double_mount;
c9180a57 728 sbsec->flags |= ROOTCONTEXT_MNT;
bd323655
AV
729 }
730 if (opts->defcontext) {
731 rc = parse_sid(sb, opts->defcontext, &defcontext_sid);
732 if (rc)
733 goto out;
c9180a57
EP
734 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
735 defcontext_sid))
736 goto out_double_mount;
c9180a57 737 sbsec->flags |= DEFCONTEXT_MNT;
1da177e4 738 }
c9180a57
EP
739 }
740
0d90a7ec 741 if (sbsec->flags & SE_SBINITIALIZED) {
c9180a57 742 /* previously mounted with options, but not on this attempt? */
bd323655 743 if ((sbsec->flags & SE_MNTMASK) && !opts)
c9180a57
EP
744 goto out_double_mount;
745 rc = 0;
746 goto out;
747 }
748
089be43e 749 if (strcmp(sb->s_type->name, "proc") == 0)
134509d5
SS
750 sbsec->flags |= SE_SBPROC | SE_SBGENFS;
751
8e014720 752 if (!strcmp(sb->s_type->name, "debugfs") ||
6a391183 753 !strcmp(sb->s_type->name, "tracefs") ||
8e014720 754 !strcmp(sb->s_type->name, "sysfs") ||
901ef845
AM
755 !strcmp(sb->s_type->name, "pstore") ||
756 !strcmp(sb->s_type->name, "cgroup") ||
757 !strcmp(sb->s_type->name, "cgroup2"))
134509d5 758 sbsec->flags |= SE_SBGENFS;
c9180a57 759
eb9ae686
DQ
760 if (!sbsec->behavior) {
761 /*
762 * Determine the labeling behavior to use for this
763 * filesystem type.
764 */
aa8e712c 765 rc = security_fs_use(&selinux_state, sb);
eb9ae686 766 if (rc) {
c103a91e 767 pr_warn("%s: security_fs_use(%s) returned %d\n",
eb9ae686
DQ
768 __func__, sb->s_type->name, rc);
769 goto out;
770 }
c9180a57 771 }
aad82892
SF
772
773 /*
01593d32
SS
774 * 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.
aad82892 777 */
01593d32
SS
778 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")) {
aad82892
SF
782 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;
aa8e712c
SS
789 rc = security_transition_sid(&selinux_state,
790 current_sid(),
791 current_sid(),
aad82892
SF
792 SECCLASS_FILE, NULL,
793 &sbsec->mntpoint_sid);
794 if (rc)
795 goto out;
796 }
797 goto out_set_opts;
798 }
799
c9180a57
EP
800 /* sets the context of the superblock for the fs being mounted. */
801 if (fscontext_sid) {
275bb41e 802 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
1da177e4 803 if (rc)
c9180a57 804 goto out;
1da177e4 805
c9180a57 806 sbsec->sid = fscontext_sid;
c312feb2
EP
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 */
eb9ae686
DQ
814 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
c9180a57
EP
819 if (context_sid) {
820 if (!fscontext_sid) {
275bb41e
DH
821 rc = may_context_mount_sb_relabel(context_sid, sbsec,
822 cred);
b04ea3ce 823 if (rc)
c9180a57
EP
824 goto out;
825 sbsec->sid = context_sid;
b04ea3ce 826 } else {
275bb41e
DH
827 rc = may_context_mount_inode_relabel(context_sid, sbsec,
828 cred);
b04ea3ce 829 if (rc)
c9180a57 830 goto out;
b04ea3ce 831 }
c9180a57
EP
832 if (!rootcontext_sid)
833 rootcontext_sid = context_sid;
1da177e4 834
c9180a57 835 sbsec->mntpoint_sid = context_sid;
c312feb2 836 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
1da177e4
LT
837 }
838
c9180a57 839 if (rootcontext_sid) {
275bb41e
DH
840 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
841 cred);
0808925e 842 if (rc)
c9180a57 843 goto out;
0808925e 844
c9180a57 845 root_isec->sid = rootcontext_sid;
6f3be9f5 846 root_isec->initialized = LABEL_INITIALIZED;
0808925e
EP
847 }
848
c9180a57 849 if (defcontext_sid) {
eb9ae686
DQ
850 if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
851 sbsec->behavior != SECURITY_FS_USE_NATIVE) {
c9180a57 852 rc = -EINVAL;
c103a91e 853 pr_warn("SELinux: defcontext option is "
c9180a57
EP
854 "invalid for this filesystem type\n");
855 goto out;
1da177e4
LT
856 }
857
c9180a57
EP
858 if (defcontext_sid != sbsec->def_sid) {
859 rc = may_context_mount_inode_relabel(defcontext_sid,
275bb41e 860 sbsec, cred);
c9180a57
EP
861 if (rc)
862 goto out;
863 }
1da177e4 864
c9180a57 865 sbsec->def_sid = defcontext_sid;
1da177e4
LT
866 }
867
aad82892 868out_set_opts:
c9180a57 869 rc = sb_finish_set_opts(sb);
1da177e4 870out:
c9180a57 871 mutex_unlock(&sbsec->lock);
1da177e4 872 return rc;
c9180a57
EP
873out_double_mount:
874 rc = -EINVAL;
c103a91e 875 pr_warn("SELinux: mount invalid. Same superblock, different "
bd323655
AV
876 "security settings for (dev %s, type %s)\n", sb->s_id,
877 sb->s_type->name);
c9180a57 878 goto out;
1da177e4
LT
879}
880
094f7b69
JL
881static 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) {
83da53c5
AG
898 struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root);
899 struct inode_security_struct *newroot = backing_inode_security(newsb->s_root);
094f7b69
JL
900 if (oldroot->sid != newroot->sid)
901 goto mismatch;
902 }
903 return 0;
904mismatch:
c103a91e 905 pr_warn("SELinux: mount invalid. Same superblock, "
094f7b69
JL
906 "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,
0b4d3452
SM
912 struct super_block *newsb,
913 unsigned long kern_flags,
914 unsigned long *set_kern_flags)
1da177e4 915{
0b4d3452 916 int rc = 0;
c9180a57
EP
917 const struct superblock_security_struct *oldsbsec = oldsb->s_security;
918 struct superblock_security_struct *newsbsec = newsb->s_security;
1da177e4 919
c9180a57
EP
920 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
921 int set_context = (oldsbsec->flags & CONTEXT_MNT);
922 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
1da177e4 923
0f5e6420
EP
924 /*
925 * if the parent was able to be mounted it clearly had no special lsm
e8c26255 926 * mount options. thus we can safely deal with this superblock later
0f5e6420 927 */
aa8e712c 928 if (!selinux_state.initialized)
094f7b69 929 return 0;
c9180a57 930
0b4d3452
SM
931 /*
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
c9180a57 938 /* how can we clone if the old one wasn't set up?? */
0d90a7ec 939 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
c9180a57 940
094f7b69 941 /* if fs is reusing a sb, make sure that the contexts match */
3815a245
BF
942 if (newsbsec->flags & SE_SBINITIALIZED) {
943 if ((kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context)
944 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
094f7b69 945 return selinux_cmp_sb_context(oldsb, newsb);
3815a245 946 }
5a552617 947
c9180a57
EP
948 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
0b4d3452
SM
956 if (newsbsec->behavior == SECURITY_FS_USE_NATIVE &&
957 !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) {
aa8e712c 958 rc = security_fs_use(&selinux_state, newsb);
0b4d3452
SM
959 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
c9180a57
EP
968 if (set_context) {
969 u32 sid = oldsbsec->mntpoint_sid;
970
971 if (!set_fscontext)
972 newsbsec->sid = sid;
973 if (!set_rootcontext) {
83da53c5 974 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
c9180a57
EP
975 newisec->sid = sid;
976 }
977 newsbsec->mntpoint_sid = sid;
1da177e4 978 }
c9180a57 979 if (set_rootcontext) {
83da53c5
AG
980 const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root);
981 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
1da177e4 982
c9180a57 983 newisec->sid = oldisec->sid;
1da177e4
LT
984 }
985
c9180a57 986 sb_finish_set_opts(newsb);
0b4d3452 987out:
c9180a57 988 mutex_unlock(&newsbsec->lock);
0b4d3452 989 return rc;
c9180a57
EP
990}
991
ba641862 992static int selinux_add_opt(int token, const char *s, void **mnt_opts)
c9180a57 993{
ba641862 994 struct selinux_mnt_opts *opts = *mnt_opts;
1da177e4 995
da3d76ab 996 if (token == Opt_seclabel) /* eaten and completely ignored */
169d68ef 997 return 0;
e0007529 998
ba641862
AV
999 if (!opts) {
1000 opts = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL);
1001 if (!opts)
1002 return -ENOMEM;
1003 *mnt_opts = opts;
c9180a57 1004 }
ba641862
AV
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;
c9180a57 1028 }
e0007529 1029 return 0;
ba641862
AV
1030Einval:
1031 pr_warn(SEL_MOUNT_FAIL_MSG);
ba641862 1032 return -EINVAL;
1da177e4 1033}
e0007529 1034
757cbe59
AV
1035static int selinux_add_mnt_opt(const char *option, const char *val, int len,
1036 void **mnt_opts)
c9180a57 1037{
757cbe59
AV
1038 int token = Opt_error;
1039 int rc, i;
e0007529 1040
757cbe59
AV
1041 for (i = 0; i < ARRAY_SIZE(tokens); i++) {
1042 if (strcmp(option, tokens[i].name) == 0) {
1043 token = tokens[i].opt;
1044 break;
169d68ef 1045 }
757cbe59 1046 }
e0007529 1047
757cbe59
AV
1048 if (token == Opt_error)
1049 return -EINVAL;
e0007529 1050
757cbe59
AV
1051 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) {
169d68ef
AV
1057 selinux_free_mnt_opts(*mnt_opts);
1058 *mnt_opts = NULL;
1da177e4 1059 }
1da177e4 1060 }
e0007529
EP
1061 return rc;
1062}
1da177e4 1063
e3489f89 1064static int show_sid(struct seq_file *m, u32 sid)
2069f457 1065{
e3489f89
AV
1066 char *context = NULL;
1067 u32 len;
1068 int rc;
11689d47 1069
e3489f89
AV
1070 rc = security_sid_to_context(&selinux_state, sid,
1071 &context, &len);
1072 if (!rc) {
1073 bool has_comma = context && strchr(context, ',');
2069f457 1074
442155c1 1075 seq_putc(m, '=');
2069f457
EP
1076 if (has_comma)
1077 seq_putc(m, '\"');
e3489f89 1078 seq_escape(m, context, "\"\n\\");
2069f457
EP
1079 if (has_comma)
1080 seq_putc(m, '\"');
1081 }
e3489f89
AV
1082 kfree(context);
1083 return rc;
2069f457
EP
1084}
1085
1086static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1087{
e3489f89 1088 struct superblock_security_struct *sbsec = sb->s_security;
2069f457
EP
1089 int rc;
1090
e3489f89
AV
1091 if (!(sbsec->flags & SE_SBINITIALIZED))
1092 return 0;
2069f457 1093
e3489f89
AV
1094 if (!selinux_state.initialized)
1095 return 0;
2069f457 1096
e3489f89
AV
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;
1103 }
1104 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, ',');
442155c1 1129 seq_puts(m, SECLABEL_STR);
e3489f89
AV
1130 }
1131 return 0;
2069f457
EP
1132}
1133
1da177e4
LT
1134static 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
13402580
JM
1157static 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
1da177e4
LT
1167static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1168{
aa8e712c 1169 int extsockclass = selinux_policycap_extsockclass();
da69a530 1170
1da177e4
LT
1171 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:
2a764b52 1178 case SOCK_RAW:
1da177e4
LT
1179 return SECCLASS_UNIX_DGRAM_SOCKET;
1180 }
1181 break;
1182 case PF_INET:
1183 case PF_INET6:
1184 switch (type) {
1185 case SOCK_STREAM:
da69a530 1186 case SOCK_SEQPACKET:
13402580
JM
1187 if (default_protocol_stream(protocol))
1188 return SECCLASS_TCP_SOCKET;
da69a530
SS
1189 else if (extsockclass && protocol == IPPROTO_SCTP)
1190 return SECCLASS_SCTP_SOCKET;
13402580
JM
1191 else
1192 return SECCLASS_RAWIP_SOCKET;
1da177e4 1193 case SOCK_DGRAM:
13402580
JM
1194 if (default_protocol_dgram(protocol))
1195 return SECCLASS_UDP_SOCKET;
ef37979a
SS
1196 else if (extsockclass && (protocol == IPPROTO_ICMP ||
1197 protocol == IPPROTO_ICMPV6))
da69a530 1198 return SECCLASS_ICMP_SOCKET;
13402580
JM
1199 else
1200 return SECCLASS_RAWIP_SOCKET;
2ee92d46
JM
1201 case SOCK_DCCP:
1202 return SECCLASS_DCCP_SOCKET;
13402580 1203 default:
1da177e4
LT
1204 return SECCLASS_RAWIP_SOCKET;
1205 }
1206 break;
1207 case PF_NETLINK:
1208 switch (protocol) {
1209 case NETLINK_ROUTE:
1210 return SECCLASS_NETLINK_ROUTE_SOCKET;
7f1fb60c 1211 case NETLINK_SOCK_DIAG:
1da177e4
LT
1212 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;
6c6d2e9b
SS
1219 case NETLINK_ISCSI:
1220 return SECCLASS_NETLINK_ISCSI_SOCKET;
1da177e4
LT
1221 case NETLINK_AUDIT:
1222 return SECCLASS_NETLINK_AUDIT_SOCKET;
6c6d2e9b
SS
1223 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;
1da177e4
LT
1229 case NETLINK_DNRTMSG:
1230 return SECCLASS_NETLINK_DNRT_SOCKET;
0c9b7942
JM
1231 case NETLINK_KOBJECT_UEVENT:
1232 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
6c6d2e9b
SS
1233 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;
1da177e4
LT
1241 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;
3e3ff15e
CP
1248 case PF_APPLETALK:
1249 return SECCLASS_APPLETALK_SOCKET;
1da177e4
LT
1250 }
1251
da69a530
SS
1252 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;
da69a530
SS
1260 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;
da69a530
SS
1278 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;
3051bf36
LT
1306 case PF_SMC:
1307 return SECCLASS_SMC_SOCKET;
68e8b849
BT
1308 case PF_XDP:
1309 return SECCLASS_XDP_SOCKET;
1310#if PF_MAX > 45
da69a530
SS
1311#error New address family defined, please update this function.
1312#endif
1313 }
1314 }
1315
1da177e4
LT
1316 return SECCLASS_SOCKET;
1317}
1318
134509d5
SS
1319static int selinux_genfs_get_sid(struct dentry *dentry,
1320 u16 tclass,
1321 u16 flags,
1322 u32 *sid)
1da177e4 1323{
8e6c9693 1324 int rc;
fc64005c 1325 struct super_block *sb = dentry->d_sb;
8e6c9693 1326 char *buffer, *path;
1da177e4 1327
828dfe1d 1328 buffer = (char *)__get_free_page(GFP_KERNEL);
1da177e4
LT
1329 if (!buffer)
1330 return -ENOMEM;
1331
8e6c9693
LAG
1332 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1333 if (IS_ERR(path))
1334 rc = PTR_ERR(path);
1335 else {
134509d5
SS
1336 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 }
8e6c9693 1344 }
aa8e712c
SS
1345 rc = security_genfs_sid(&selinux_state, sb->s_type->name,
1346 path, tclass, sid);
7bb185ed
SS
1347 if (rc == -ENOENT) {
1348 /* No match in policy, mark as unlabeled. */
1349 *sid = SECINITSID_UNLABELED;
1350 rc = 0;
1351 }
1da177e4 1352 }
1da177e4
LT
1353 free_page((unsigned long)buffer);
1354 return rc;
1355}
1da177e4
LT
1356
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;
80788c22 1361 struct inode_security_struct *isec = selinux_inode(inode);
9287aed2
AG
1362 u32 task_sid, sid = 0;
1363 u16 sclass;
1da177e4
LT
1364 struct dentry *dentry;
1365#define INITCONTEXTLEN 255
1366 char *context = NULL;
1367 unsigned len = 0;
1368 int rc = 0;
1da177e4 1369
6f3be9f5 1370 if (isec->initialized == LABEL_INITIALIZED)
13457d07 1371 return 0;
1da177e4 1372
9287aed2 1373 spin_lock(&isec->lock);
6f3be9f5 1374 if (isec->initialized == LABEL_INITIALIZED)
23970741 1375 goto out_unlock;
1da177e4 1376
13457d07
AG
1377 if (isec->sclass == SECCLASS_FILE)
1378 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1379
1da177e4 1380 sbsec = inode->i_sb->s_security;
0d90a7ec 1381 if (!(sbsec->flags & SE_SBINITIALIZED)) {
1da177e4
LT
1382 /* 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);
23970741 1389 goto out_unlock;
1da177e4
LT
1390 }
1391
9287aed2
AG
1392 sclass = isec->sclass;
1393 task_sid = isec->task_sid;
1394 sid = isec->sid;
1395 isec->initialized = LABEL_PENDING;
1396 spin_unlock(&isec->lock);
1397
1da177e4 1398 switch (sbsec->behavior) {
eb9ae686
DQ
1399 case SECURITY_FS_USE_NATIVE:
1400 break;
1da177e4 1401 case SECURITY_FS_USE_XATTR:
5d6c3191 1402 if (!(inode->i_opflags & IOP_XATTR)) {
9287aed2 1403 sid = sbsec->def_sid;
1da177e4
LT
1404 break;
1405 }
1da177e4
LT
1406 /* 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 {
b127125d
AV
1412 /*
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 */
1da177e4 1418 dentry = d_find_alias(inode);
b127125d
AV
1419 if (!dentry)
1420 dentry = d_find_any_alias(inode);
1da177e4
LT
1421 }
1422 if (!dentry) {
df7f54c0
EP
1423 /*
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 */
9287aed2 1432 goto out;
1da177e4
LT
1433 }
1434
1435 len = INITCONTEXTLEN;
4cb912f1 1436 context = kmalloc(len+1, GFP_NOFS);
1da177e4
LT
1437 if (!context) {
1438 rc = -ENOMEM;
1439 dput(dentry);
9287aed2 1440 goto out;
1da177e4 1441 }
4cb912f1 1442 context[len] = '\0';
5d6c3191 1443 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
1da177e4 1444 if (rc == -ERANGE) {
314dabb8
JM
1445 kfree(context);
1446
1da177e4 1447 /* Need a larger buffer. Query for the right size. */
5d6c3191 1448 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0);
1da177e4
LT
1449 if (rc < 0) {
1450 dput(dentry);
9287aed2 1451 goto out;
1da177e4 1452 }
1da177e4 1453 len = rc;
4cb912f1 1454 context = kmalloc(len+1, GFP_NOFS);
1da177e4
LT
1455 if (!context) {
1456 rc = -ENOMEM;
1457 dput(dentry);
9287aed2 1458 goto out;
1da177e4 1459 }
4cb912f1 1460 context[len] = '\0';
5d6c3191 1461 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
1da177e4
LT
1462 }
1463 dput(dentry);
1464 if (rc < 0) {
1465 if (rc != -ENODATA) {
c103a91e 1466 pr_warn("SELinux: %s: getxattr returned "
dd6f953a 1467 "%d for dev=%s ino=%ld\n", __func__,
1da177e4
LT
1468 -rc, inode->i_sb->s_id, inode->i_ino);
1469 kfree(context);
9287aed2 1470 goto out;
1da177e4
LT
1471 }
1472 /* Map ENODATA to the default file SID */
1473 sid = sbsec->def_sid;
1474 rc = 0;
1475 } else {
aa8e712c
SS
1476 rc = security_context_to_sid_default(&selinux_state,
1477 context, rc, &sid,
869ab514
SS
1478 sbsec->def_sid,
1479 GFP_NOFS);
1da177e4 1480 if (rc) {
4ba0a8ad
EP
1481 char *dev = inode->i_sb->s_id;
1482 unsigned long ino = inode->i_ino;
1483
1484 if (rc == -EINVAL) {
1485 if (printk_ratelimit())
c103a91e 1486 pr_notice("SELinux: inode=%lu on dev=%s was found to have an invalid "
4ba0a8ad
EP
1487 "context=%s. This indicates you may need to relabel the inode or the "
1488 "filesystem in question.\n", ino, dev, context);
1489 } else {
c103a91e 1490 pr_warn("SELinux: %s: context_to_sid(%s) "
4ba0a8ad
EP
1491 "returned %d for dev=%s ino=%ld\n",
1492 __func__, context, -rc, dev, ino);
1493 }
1da177e4
LT
1494 kfree(context);
1495 /* Leave with the unlabeled SID */
1496 rc = 0;
1497 break;
1498 }
1499 }
1500 kfree(context);
1da177e4
LT
1501 break;
1502 case SECURITY_FS_USE_TASK:
9287aed2 1503 sid = task_sid;
1da177e4
LT
1504 break;
1505 case SECURITY_FS_USE_TRANS:
1506 /* Default to the fs SID. */
9287aed2 1507 sid = sbsec->sid;
1da177e4
LT
1508
1509 /* Try to obtain a transition SID. */
aa8e712c
SS
1510 rc = security_transition_sid(&selinux_state, task_sid, sid,
1511 sclass, NULL, &sid);
1da177e4 1512 if (rc)
9287aed2 1513 goto out;
1da177e4 1514 break;
c312feb2 1515 case SECURITY_FS_USE_MNTPOINT:
9287aed2 1516 sid = sbsec->mntpoint_sid;
c312feb2 1517 break;
1da177e4 1518 default:
c312feb2 1519 /* Default to the fs superblock SID. */
9287aed2 1520 sid = sbsec->sid;
1da177e4 1521
134509d5 1522 if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) {
f64410ec
PM
1523 /* We must have a dentry to determine the label on
1524 * procfs inodes */
b127125d 1525 if (opt_dentry) {
f64410ec
PM
1526 /* Called from d_instantiate or
1527 * d_splice_alias. */
1528 dentry = dget(opt_dentry);
b127125d 1529 } else {
f64410ec 1530 /* Called from selinux_complete_init, try to
b127125d
AV
1531 * find a dentry. Some filesystems really want
1532 * a connected one, so try that first.
1533 */
f64410ec 1534 dentry = d_find_alias(inode);
b127125d
AV
1535 if (!dentry)
1536 dentry = d_find_any_alias(inode);
1537 }
f64410ec
PM
1538 /*
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)
9287aed2
AG
1548 goto out;
1549 rc = selinux_genfs_get_sid(dentry, sclass,
134509d5 1550 sbsec->flags, &sid);
f64410ec
PM
1551 dput(dentry);
1552 if (rc)
9287aed2 1553 goto out;
1da177e4
LT
1554 }
1555 break;
1556 }
1557
9287aed2
AG
1558out:
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 }
1da177e4 1569
23970741 1570out_unlock:
9287aed2 1571 spin_unlock(&isec->lock);
1da177e4
LT
1572 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
b68e418c
SS
1602#if CAP_LAST_CAP > 63
1603#error Fix SELinux to handle capabilities > 63.
1604#endif
1605
1da177e4 1606/* Check whether a task is allowed to use a capability. */
6a9de491 1607static int cred_has_capability(const struct cred *cred,
c1a85a00 1608 int cap, unsigned int opts, bool initns)
1da177e4 1609{
2bf49690 1610 struct common_audit_data ad;
06112163 1611 struct av_decision avd;
b68e418c 1612 u16 sclass;
3699c53c 1613 u32 sid = cred_sid(cred);
b68e418c 1614 u32 av = CAP_TO_MASK(cap);
06112163 1615 int rc;
1da177e4 1616
50c205f5 1617 ad.type = LSM_AUDIT_DATA_CAP;
1da177e4
LT
1618 ad.u.cap = cap;
1619
b68e418c
SS
1620 switch (CAP_TO_INDEX(cap)) {
1621 case 0:
8e4ff6f2 1622 sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
b68e418c
SS
1623 break;
1624 case 1:
8e4ff6f2 1625 sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
b68e418c
SS
1626 break;
1627 default:
c103a91e 1628 pr_err("SELinux: out of range capability %d\n", cap);
b68e418c 1629 BUG();
a35c6c83 1630 return -EINVAL;
b68e418c 1631 }
06112163 1632
6b6bc620
SS
1633 rc = avc_has_perm_noaudit(&selinux_state,
1634 sid, sid, sclass, av, 0, &avd);
c1a85a00 1635 if (!(opts & CAP_OPT_NOAUDIT)) {
6b6bc620
SS
1636 int rc2 = avc_audit(&selinux_state,
1637 sid, sid, sclass, av, &avd, rc, &ad, 0);
9ade0cf4
EP
1638 if (rc2)
1639 return rc2;
1640 }
06112163 1641 return rc;
1da177e4
LT
1642}
1643
1da177e4
LT
1644/* 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). */
88e67f3b 1647static int inode_has_perm(const struct cred *cred,
1da177e4
LT
1648 struct inode *inode,
1649 u32 perms,
19e49834 1650 struct common_audit_data *adp)
1da177e4 1651{
1da177e4 1652 struct inode_security_struct *isec;
275bb41e 1653 u32 sid;
1da177e4 1654
e0e81739
DH
1655 validate_creds(cred);
1656
828dfe1d 1657 if (unlikely(IS_PRIVATE(inode)))
bbaca6c2
SS
1658 return 0;
1659
88e67f3b 1660 sid = cred_sid(cred);
80788c22 1661 isec = selinux_inode(inode);
1da177e4 1662
6b6bc620
SS
1663 return avc_has_perm(&selinux_state,
1664 sid, isec->sid, isec->sclass, perms, adp);
1da177e4
LT
1665}
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. */
88e67f3b 1670static inline int dentry_has_perm(const struct cred *cred,
1da177e4
LT
1671 struct dentry *dentry,
1672 u32 av)
1673{
c6f493d6 1674 struct inode *inode = d_backing_inode(dentry);
2bf49690 1675 struct common_audit_data ad;
88e67f3b 1676
50c205f5 1677 ad.type = LSM_AUDIT_DATA_DENTRY;
2875fa00 1678 ad.u.dentry = dentry;
5d226df4 1679 __inode_security_revalidate(inode, dentry, true);
19e49834 1680 return inode_has_perm(cred, inode, av, &ad);
2875fa00
EP
1681}
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,
3f7036a0 1687 const struct path *path,
2875fa00
EP
1688 u32 av)
1689{
c6f493d6 1690 struct inode *inode = d_backing_inode(path->dentry);
2875fa00
EP
1691 struct common_audit_data ad;
1692
50c205f5 1693 ad.type = LSM_AUDIT_DATA_PATH;
2875fa00 1694 ad.u.path = *path;
5d226df4 1695 __inode_security_revalidate(inode, path->dentry, true);
19e49834 1696 return inode_has_perm(cred, inode, av, &ad);
1da177e4
LT
1697}
1698
13f8e981
DH
1699/* 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
43af5de7
VG
1706 ad.type = LSM_AUDIT_DATA_FILE;
1707 ad.u.file = file;
19e49834 1708 return inode_has_perm(cred, file_inode(file), av, &ad);
13f8e981
DH
1709}
1710
f66e448c
CF
1711#ifdef CONFIG_BPF_SYSCALL
1712static int bpf_fd_pass(struct file *file, u32 sid);
1713#endif
1714
1da177e4
LT
1715/* 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. */
88e67f3b
DH
1723static int file_has_perm(const struct cred *cred,
1724 struct file *file,
1725 u32 av)
1da177e4 1726{
bb6c6b02 1727 struct file_security_struct *fsec = selinux_file(file);
496ad9aa 1728 struct inode *inode = file_inode(file);
2bf49690 1729 struct common_audit_data ad;
88e67f3b 1730 u32 sid = cred_sid(cred);
1da177e4
LT
1731 int rc;
1732
43af5de7
VG
1733 ad.type = LSM_AUDIT_DATA_FILE;
1734 ad.u.file = file;
1da177e4 1735
275bb41e 1736 if (sid != fsec->sid) {
6b6bc620
SS
1737 rc = avc_has_perm(&selinux_state,
1738 sid, fsec->sid,
1da177e4
LT
1739 SECCLASS_FD,
1740 FD__USE,
1741 &ad);
1742 if (rc)
88e67f3b 1743 goto out;
1da177e4
LT
1744 }
1745
f66e448c
CF
1746#ifdef CONFIG_BPF_SYSCALL
1747 rc = bpf_fd_pass(file, cred_sid(cred));
1748 if (rc)
1749 return rc;
1750#endif
1751
1da177e4 1752 /* av is zero if only checking access to the descriptor. */
88e67f3b 1753 rc = 0;
1da177e4 1754 if (av)
19e49834 1755 rc = inode_has_perm(cred, inode, av, &ad);
1da177e4 1756
88e67f3b
DH
1757out:
1758 return rc;
1da177e4
LT
1759}
1760
c3c188b2
DH
1761/*
1762 * Determine the label for an inode that might be unioned.
1763 */
c957f6df
VG
1764static 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)
c3c188b2
DH
1769{
1770 const struct superblock_security_struct *sbsec = dir->i_sb->s_security;
c3c188b2
DH
1771
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 {
20cdef8d 1779 const struct inode_security_struct *dsec = inode_security(dir);
aa8e712c
SS
1780 return security_transition_sid(&selinux_state, tsec->sid,
1781 dsec->sid, tclass,
c3c188b2
DH
1782 name, _new_isid);
1783 }
1784
1785 return 0;
1786}
1787
1da177e4
LT
1788/* Check whether a task can create a file. */
1789static int may_create(struct inode *dir,
1790 struct dentry *dentry,
1791 u16 tclass)
1792{
0c6cfa62 1793 const struct task_security_struct *tsec = selinux_cred(current_cred());
1da177e4
LT
1794 struct inode_security_struct *dsec;
1795 struct superblock_security_struct *sbsec;
275bb41e 1796 u32 sid, newsid;
2bf49690 1797 struct common_audit_data ad;
1da177e4
LT
1798 int rc;
1799
83da53c5 1800 dsec = inode_security(dir);
1da177e4
LT
1801 sbsec = dir->i_sb->s_security;
1802
275bb41e 1803 sid = tsec->sid;
275bb41e 1804
50c205f5 1805 ad.type = LSM_AUDIT_DATA_DENTRY;
a269434d 1806 ad.u.dentry = dentry;
1da177e4 1807
6b6bc620
SS
1808 rc = avc_has_perm(&selinux_state,
1809 sid, dsec->sid, SECCLASS_DIR,
1da177e4
LT
1810 DIR__ADD_NAME | DIR__SEARCH,
1811 &ad);
1812 if (rc)
1813 return rc;
1814
0c6cfa62 1815 rc = selinux_determine_inode_label(selinux_cred(current_cred()), dir,
c957f6df 1816 &dentry->d_name, tclass, &newsid);
c3c188b2
DH
1817 if (rc)
1818 return rc;
1da177e4 1819
6b6bc620
SS
1820 rc = avc_has_perm(&selinux_state,
1821 sid, newsid, tclass, FILE__CREATE, &ad);
1da177e4
LT
1822 if (rc)
1823 return rc;
1824
6b6bc620
SS
1825 return avc_has_perm(&selinux_state,
1826 newsid, sbsec->sid,
1da177e4
LT
1827 SECCLASS_FILESYSTEM,
1828 FILESYSTEM__ASSOCIATE, &ad);
1829}
1830
828dfe1d
EP
1831#define MAY_LINK 0
1832#define MAY_UNLINK 1
1833#define MAY_RMDIR 2
1da177e4
LT
1834
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{
1da177e4 1841 struct inode_security_struct *dsec, *isec;
2bf49690 1842 struct common_audit_data ad;
275bb41e 1843 u32 sid = current_sid();
1da177e4
LT
1844 u32 av;
1845 int rc;
1846
83da53c5
AG
1847 dsec = inode_security(dir);
1848 isec = backing_inode_security(dentry);
1da177e4 1849
50c205f5 1850 ad.type = LSM_AUDIT_DATA_DENTRY;
a269434d 1851 ad.u.dentry = dentry;
1da177e4
LT
1852
1853 av = DIR__SEARCH;
1854 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
6b6bc620
SS
1855 rc = avc_has_perm(&selinux_state,
1856 sid, dsec->sid, SECCLASS_DIR, av, &ad);
1da177e4
LT
1857 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:
c103a91e 1871 pr_warn("SELinux: %s: unrecognized kind %d\n",
744ba35e 1872 __func__, kind);
1da177e4
LT
1873 return 0;
1874 }
1875
6b6bc620
SS
1876 rc = avc_has_perm(&selinux_state,
1877 sid, isec->sid, isec->sclass, av, &ad);
1da177e4
LT
1878 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{
1da177e4 1886 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
2bf49690 1887 struct common_audit_data ad;
275bb41e 1888 u32 sid = current_sid();
1da177e4
LT
1889 u32 av;
1890 int old_is_dir, new_is_dir;
1891 int rc;
1892
83da53c5
AG
1893 old_dsec = inode_security(old_dir);
1894 old_isec = backing_inode_security(old_dentry);
e36cb0b8 1895 old_is_dir = d_is_dir(old_dentry);
83da53c5 1896 new_dsec = inode_security(new_dir);
1da177e4 1897
50c205f5 1898 ad.type = LSM_AUDIT_DATA_DENTRY;
1da177e4 1899
a269434d 1900 ad.u.dentry = old_dentry;
6b6bc620
SS
1901 rc = avc_has_perm(&selinux_state,
1902 sid, old_dsec->sid, SECCLASS_DIR,
1da177e4
LT
1903 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1904 if (rc)
1905 return rc;
6b6bc620
SS
1906 rc = avc_has_perm(&selinux_state,
1907 sid, old_isec->sid,
1da177e4
LT
1908 old_isec->sclass, FILE__RENAME, &ad);
1909 if (rc)
1910 return rc;
1911 if (old_is_dir && new_dir != old_dir) {
6b6bc620
SS
1912 rc = avc_has_perm(&selinux_state,
1913 sid, old_isec->sid,
1da177e4
LT
1914 old_isec->sclass, DIR__REPARENT, &ad);
1915 if (rc)
1916 return rc;
1917 }
1918
a269434d 1919 ad.u.dentry = new_dentry;
1da177e4 1920 av = DIR__ADD_NAME | DIR__SEARCH;
2c616d4d 1921 if (d_is_positive(new_dentry))
1da177e4 1922 av |= DIR__REMOVE_NAME;
6b6bc620
SS
1923 rc = avc_has_perm(&selinux_state,
1924 sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
1da177e4
LT
1925 if (rc)
1926 return rc;
2c616d4d 1927 if (d_is_positive(new_dentry)) {
83da53c5 1928 new_isec = backing_inode_security(new_dentry);
e36cb0b8 1929 new_is_dir = d_is_dir(new_dentry);
6b6bc620
SS
1930 rc = avc_has_perm(&selinux_state,
1931 sid, new_isec->sid,
1da177e4
LT
1932 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. */
88e67f3b 1942static int superblock_has_perm(const struct cred *cred,
1da177e4
LT
1943 struct super_block *sb,
1944 u32 perms,
2bf49690 1945 struct common_audit_data *ad)
1da177e4 1946{
1da177e4 1947 struct superblock_security_struct *sbsec;
88e67f3b 1948 u32 sid = cred_sid(cred);
1da177e4 1949
1da177e4 1950 sbsec = sb->s_security;
6b6bc620
SS
1951 return avc_has_perm(&selinux_state,
1952 sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
1da177e4
LT
1953}
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
dba19c60 1960 if (!S_ISDIR(mode)) {
1da177e4
LT
1961 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
8b6a5a37
EP
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 }
1996 if (!av) {
1997 /*
1998 * Special file opened with flags 3 for ioctl-only use.
1999 */
2000 av = FILE__IOCTL;
2001 }
2002
2003 return av;
2004}
2005
b0c636b9 2006/*
8b6a5a37 2007 * Convert a file to an access vector and include the correct open
b0c636b9
EP
2008 * open permission.
2009 */
8b6a5a37 2010static inline u32 open_file_to_av(struct file *file)
b0c636b9 2011{
8b6a5a37 2012 u32 av = file_to_av(file);
ccb54478 2013 struct inode *inode = file_inode(file);
b0c636b9 2014
aa8e712c
SS
2015 if (selinux_policycap_openperm() &&
2016 inode->i_sb->s_magic != SOCKFS_MAGIC)
49b7b8de
EP
2017 av |= FILE__OPEN;
2018
b0c636b9
EP
2019 return av;
2020}
2021
1da177e4
LT
2022/* Hook functions begin here. */
2023
79af7307
SS
2024static int selinux_binder_set_context_mgr(struct task_struct *mgr)
2025{
2026 u32 mysid = current_sid();
2027 u32 mgrsid = task_sid(mgr);
2028
6b6bc620
SS
2029 return avc_has_perm(&selinux_state,
2030 mysid, mgrsid, SECCLASS_BINDER,
79af7307
SS
2031 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) {
6b6bc620
SS
2043 rc = avc_has_perm(&selinux_state,
2044 mysid, fromsid, SECCLASS_BINDER,
79af7307
SS
2045 BINDER__IMPERSONATE, NULL);
2046 if (rc)
2047 return rc;
2048 }
2049
6b6bc620
SS
2050 return avc_has_perm(&selinux_state,
2051 fromsid, tosid, SECCLASS_BINDER, BINDER__CALL,
79af7307
SS
2052 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
6b6bc620
SS
2061 return avc_has_perm(&selinux_state,
2062 fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER,
79af7307
SS
2063 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);
bb6c6b02 2071 struct file_security_struct *fsec = selinux_file(file);
83da53c5 2072 struct dentry *dentry = file->f_path.dentry;
20cdef8d 2073 struct inode_security_struct *isec;
79af7307
SS
2074 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) {
6b6bc620
SS
2081 rc = avc_has_perm(&selinux_state,
2082 sid, fsec->sid,
79af7307
SS
2083 SECCLASS_FD,
2084 FD__USE,
2085 &ad);
2086 if (rc)
2087 return rc;
2088 }
2089
f66e448c
CF
2090#ifdef CONFIG_BPF_SYSCALL
2091 rc = bpf_fd_pass(file, sid);
2092 if (rc)
2093 return rc;
2094#endif
2095
83da53c5 2096 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
79af7307
SS
2097 return 0;
2098
20cdef8d 2099 isec = backing_inode_security(dentry);
6b6bc620
SS
2100 return avc_has_perm(&selinux_state,
2101 sid, isec->sid, isec->sclass, file_to_av(file),
79af7307
SS
2102 &ad);
2103}
2104
9e48858f 2105static int selinux_ptrace_access_check(struct task_struct *child,
5cd9c58f 2106 unsigned int mode)
1da177e4 2107{
be0554c9
SS
2108 u32 sid = current_sid();
2109 u32 csid = task_sid(child);
2110
2111 if (mode & PTRACE_MODE_READ)
6b6bc620
SS
2112 return avc_has_perm(&selinux_state,
2113 sid, csid, SECCLASS_FILE, FILE__READ, NULL);
006ebb40 2114
6b6bc620
SS
2115 return avc_has_perm(&selinux_state,
2116 sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
5cd9c58f
DH
2117}
2118
2119static int selinux_ptrace_traceme(struct task_struct *parent)
2120{
6b6bc620
SS
2121 return avc_has_perm(&selinux_state,
2122 task_sid(parent), current_sid(), SECCLASS_PROCESS,
be0554c9 2123 PROCESS__PTRACE, NULL);
1da177e4
LT
2124}
2125
2126static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
828dfe1d 2127 kernel_cap_t *inheritable, kernel_cap_t *permitted)
1da177e4 2128{
6b6bc620
SS
2129 return avc_has_perm(&selinux_state,
2130 current_sid(), task_sid(target), SECCLASS_PROCESS,
be0554c9 2131 PROCESS__GETCAP, NULL);
1da177e4
LT
2132}
2133
d84f4f99
DH
2134static 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)
1da177e4 2138{
6b6bc620
SS
2139 return avc_has_perm(&selinux_state,
2140 cred_sid(old), cred_sid(new), SECCLASS_PROCESS,
be0554c9 2141 PROCESS__SETCAP, NULL);
1da177e4
LT
2142}
2143
5626d3e8
JM
2144/*
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
6a9de491 2154static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
c1a85a00 2155 int cap, unsigned int opts)
1da177e4 2156{
c1a85a00 2157 return cred_has_capability(cred, cap, opts, ns == &init_user_ns);
1da177e4
LT
2158}
2159
1da177e4
LT
2160static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
2161{
88e67f3b 2162 const struct cred *cred = current_cred();
1da177e4
LT
2163 int rc = 0;
2164
2165 if (!sb)
2166 return 0;
2167
2168 switch (cmds) {
828dfe1d
EP
2169 case Q_SYNC:
2170 case Q_QUOTAON:
2171 case Q_QUOTAOFF:
2172 case Q_SETINFO:
2173 case Q_SETQUOTA:
88e67f3b 2174 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
828dfe1d
EP
2175 break;
2176 case Q_GETFMT:
2177 case Q_GETINFO:
2178 case Q_GETQUOTA:
88e67f3b 2179 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
828dfe1d
EP
2180 break;
2181 default:
2182 rc = 0; /* let the kernel handle invalid cmds */
2183 break;
1da177e4
LT
2184 }
2185 return rc;
2186}
2187
2188static int selinux_quota_on(struct dentry *dentry)
2189{
88e67f3b
DH
2190 const struct cred *cred = current_cred();
2191
2875fa00 2192 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
1da177e4
LT
2193}
2194
12b3052c 2195static int selinux_syslog(int type)
1da177e4 2196{
1da177e4 2197 switch (type) {
d78ca3cd
KC
2198 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2199 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
6b6bc620
SS
2200 return avc_has_perm(&selinux_state,
2201 current_sid(), SECINITSID_KERNEL,
be0554c9 2202 SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL);
d78ca3cd
KC
2203 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:
6b6bc620
SS
2207 return avc_has_perm(&selinux_state,
2208 current_sid(), SECINITSID_KERNEL,
be0554c9
SS
2209 SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE,
2210 NULL);
1da177e4 2211 }
be0554c9 2212 /* All other syslog types */
6b6bc620
SS
2213 return avc_has_perm(&selinux_state,
2214 current_sid(), SECINITSID_KERNEL,
be0554c9 2215 SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL);
1da177e4
LT
2216}
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 *
1da177e4
LT
2223 * Do not audit the selinux permission check, as this is applied to all
2224 * processes that allocate mappings.
2225 */
34b4e4aa 2226static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
1da177e4
LT
2227{
2228 int rc, cap_sys_admin = 0;
1da177e4 2229
b1d9e6b0 2230 rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
c1a85a00 2231 CAP_OPT_NOAUDIT, true);
1da177e4
LT
2232 if (rc == 0)
2233 cap_sys_admin = 1;
2234
b1d9e6b0 2235 return cap_sys_admin;
1da177e4
LT
2236}
2237
2238/* binprm security operations */
2239
be0554c9 2240static u32 ptrace_parent_sid(void)
0c6181cb
PM
2241{
2242 u32 sid = 0;
2243 struct task_struct *tracer;
2244
2245 rcu_read_lock();
be0554c9 2246 tracer = ptrace_parent(current);
0c6181cb
PM
2247 if (tracer)
2248 sid = task_sid(tracer);
2249 rcu_read_unlock();
2250
2251 return sid;
2252}
2253
7b0d0b40
SS
2254static 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);
380cf5ba 2259 int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
7b0d0b40 2260 int rc;
af63f419 2261 u32 av;
7b0d0b40
SS
2262
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 /*
af63f419
SS
2270 * 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.
7b0d0b40 2274 */
aa8e712c 2275 if (selinux_policycap_nnp_nosuid_transition()) {
af63f419 2276 av = 0;
7b0d0b40 2277 if (nnp)
af63f419
SS
2278 av |= PROCESS2__NNP_TRANSITION;
2279 if (nosuid)
2280 av |= PROCESS2__NOSUID_TRANSITION;
6b6bc620
SS
2281 rc = avc_has_perm(&selinux_state,
2282 old_tsec->sid, new_tsec->sid,
af63f419
SS
2283 SECCLASS_PROCESS2, av, NULL);
2284 if (!rc)
2285 return 0;
7b0d0b40 2286 }
af63f419
SS
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.
2292 */
aa8e712c
SS
2293 rc = security_bounded_transition(&selinux_state, old_tsec->sid,
2294 new_tsec->sid);
af63f419
SS
2295 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;
7b0d0b40
SS
2306}
2307
a6f76f23 2308static int selinux_bprm_set_creds(struct linux_binprm *bprm)
1da177e4 2309{
a6f76f23
DH
2310 const struct task_security_struct *old_tsec;
2311 struct task_security_struct *new_tsec;
1da177e4 2312 struct inode_security_struct *isec;
2bf49690 2313 struct common_audit_data ad;
496ad9aa 2314 struct inode *inode = file_inode(bprm->file);
1da177e4
LT
2315 int rc;
2316
a6f76f23
DH
2317 /* SELinux context only depends on initial program or script and not
2318 * the script interpreter */
ddb4a144 2319 if (bprm->called_set_creds)
1da177e4
LT
2320 return 0;
2321
0c6cfa62
CS
2322 old_tsec = selinux_cred(current_cred());
2323 new_tsec = selinux_cred(bprm->cred);
83da53c5 2324 isec = inode_security(inode);
1da177e4
LT
2325
2326 /* Default to the current task SID. */
a6f76f23
DH
2327 new_tsec->sid = old_tsec->sid;
2328 new_tsec->osid = old_tsec->sid;
1da177e4 2329
28eba5bf 2330 /* Reset fs, key, and sock SIDs on execve. */
a6f76f23
DH
2331 new_tsec->create_sid = 0;
2332 new_tsec->keycreate_sid = 0;
2333 new_tsec->sockcreate_sid = 0;
1da177e4 2334
a6f76f23
DH
2335 if (old_tsec->exec_sid) {
2336 new_tsec->sid = old_tsec->exec_sid;
1da177e4 2337 /* Reset exec SID on execve. */
a6f76f23 2338 new_tsec->exec_sid = 0;
259e5e6c 2339
7b0d0b40
SS
2340 /* 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;
1da177e4
LT
2344 } else {
2345 /* Check for a default transition on this program. */
aa8e712c
SS
2346 rc = security_transition_sid(&selinux_state, old_tsec->sid,
2347 isec->sid, SECCLASS_PROCESS, NULL,
652bb9b0 2348 &new_tsec->sid);
1da177e4
LT
2349 if (rc)
2350 return rc;
7b0d0b40
SS
2351
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;
1da177e4
LT
2359 }
2360
43af5de7
VG
2361 ad.type = LSM_AUDIT_DATA_FILE;
2362 ad.u.file = bprm->file;
1da177e4 2363
a6f76f23 2364 if (new_tsec->sid == old_tsec->sid) {
6b6bc620
SS
2365 rc = avc_has_perm(&selinux_state,
2366 old_tsec->sid, isec->sid,
1da177e4
LT
2367 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2368 if (rc)
2369 return rc;
2370 } else {
2371 /* Check permissions for the transition. */
6b6bc620
SS
2372 rc = avc_has_perm(&selinux_state,
2373 old_tsec->sid, new_tsec->sid,
1da177e4
LT
2374 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2375 if (rc)
2376 return rc;
2377
6b6bc620
SS
2378 rc = avc_has_perm(&selinux_state,
2379 new_tsec->sid, isec->sid,
1da177e4
LT
2380 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2381 if (rc)
2382 return rc;
2383
a6f76f23
DH
2384 /* Check for shared state */
2385 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
6b6bc620
SS
2386 rc = avc_has_perm(&selinux_state,
2387 old_tsec->sid, new_tsec->sid,
a6f76f23
DH
2388 SECCLASS_PROCESS, PROCESS__SHARE,
2389 NULL);
2390 if (rc)
2391 return -EPERM;
2392 }
2393
2394 /* Make sure that anyone attempting to ptrace over a task that
2395 * changes its SID has the appropriate permit */
9227dd2a 2396 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
be0554c9 2397 u32 ptsid = ptrace_parent_sid();
a6f76f23 2398 if (ptsid != 0) {
6b6bc620
SS
2399 rc = avc_has_perm(&selinux_state,
2400 ptsid, new_tsec->sid,
a6f76f23
DH
2401 SECCLASS_PROCESS,
2402 PROCESS__PTRACE, NULL);
2403 if (rc)
2404 return -EPERM;
2405 }
2406 }
1da177e4 2407
a6f76f23
DH
2408 /* Clear any possibly unsafe personality bits on exec: */
2409 bprm->per_clear |= PER_CLEAR_ON_SETID;
275bb41e 2410
1da177e4
LT
2411 /* Enable secure mode for SIDs transitions unless
2412 the noatsecure permission is granted between
2413 the two SIDs, i.e. ahp returns 0. */
6b6bc620
SS
2414 rc = avc_has_perm(&selinux_state,
2415 old_tsec->sid, new_tsec->sid,
62874c3a
KC
2416 SECCLASS_PROCESS, PROCESS__NOATSECURE,
2417 NULL);
2418 bprm->secureexec |= !!rc;
1da177e4
LT
2419 }
2420
62874c3a 2421 return 0;
1da177e4
LT
2422}
2423
c3c073f8
AV
2424static 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
1da177e4 2429/* Derived from fs/exec.c:flush_old_files. */
745ca247
DH
2430static inline void flush_unauthorized_files(const struct cred *cred,
2431 struct files_struct *files)
1da177e4 2432{
1da177e4 2433 struct file *file, *devnull = NULL;
b20c8122 2434 struct tty_struct *tty;
24ec839c 2435 int drop_tty = 0;
c3c073f8 2436 unsigned n;
1da177e4 2437
24ec839c 2438 tty = get_current_tty();
1da177e4 2439 if (tty) {
4a510969 2440 spin_lock(&tty->files_lock);
37dd0bd0 2441 if (!list_empty(&tty->tty_files)) {
d996b62a 2442 struct tty_file_private *file_priv;
37dd0bd0 2443
1da177e4 2444 /* Revalidate access to controlling tty.
13f8e981
DH
2445 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. */
d996b62a
NP
2449 file_priv = list_first_entry(&tty->tty_files,
2450 struct tty_file_private, list);
2451 file = file_priv->file;
13f8e981 2452 if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
24ec839c 2453 drop_tty = 1;
1da177e4 2454 }
4a510969 2455 spin_unlock(&tty->files_lock);
452a00d2 2456 tty_kref_put(tty);
1da177e4 2457 }
98a27ba4
EB
2458 /* Reset controlling tty. */
2459 if (drop_tty)
2460 no_tty();
1da177e4
LT
2461
2462 /* Revalidate access to inherited open files. */
c3c073f8
AV
2463 n = iterate_fd(files, 0, match_file, cred);
2464 if (!n) /* none found? */
2465 return;
1da177e4 2466
c3c073f8 2467 devnull = dentry_open(&selinux_null, O_RDWR, cred);
45525b26
AV
2468 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)
c3c073f8 2475 fput(devnull);
1da177e4
LT
2476}
2477
a6f76f23
DH
2478/*
2479 * Prepare a process for imminent new credential changes due to exec
2480 */
2481static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
1da177e4 2482{
a6f76f23
DH
2483 struct task_security_struct *new_tsec;
2484 struct rlimit *rlim, *initrlim;
2485 int rc, i;
d84f4f99 2486
0c6cfa62 2487 new_tsec = selinux_cred(bprm->cred);
a6f76f23
DH
2488 if (new_tsec->sid == new_tsec->osid)
2489 return;
1da177e4 2490
a6f76f23
DH
2491 /* Close files for which the new task SID is not authorized. */
2492 flush_unauthorized_files(bprm->cred, current->files);
0356357c 2493
a6f76f23
DH
2494 /* Always clear parent death signal on SID transitions. */
2495 current->pdeath_signal = 0;
0356357c 2496
a6f76f23
DH
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 */
6b6bc620
SS
2507 rc = avc_has_perm(&selinux_state,
2508 new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
a6f76f23
DH
2509 PROCESS__RLIMITINH, NULL);
2510 if (rc) {
eb2d55a3
ON
2511 /* protect against do_prlimit() */
2512 task_lock(current);
a6f76f23
DH
2513 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);
1da177e4 2517 }
eb2d55a3 2518 task_unlock(current);
baa73d9e
NP
2519 if (IS_ENABLED(CONFIG_POSIX_TIMERS))
2520 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
1da177e4
LT
2521 }
2522}
2523
2524/*
a6f76f23
DH
2525 * Clean up the process immediately after the installation of new credentials
2526 * due to exec
1da177e4 2527 */
a6f76f23 2528static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
1da177e4 2529{
0c6cfa62 2530 const struct task_security_struct *tsec = selinux_cred(current_cred());
1da177e4 2531 struct itimerval itimer;
a6f76f23 2532 u32 osid, sid;
1da177e4
LT
2533 int rc, i;
2534
a6f76f23
DH
2535 osid = tsec->osid;
2536 sid = tsec->sid;
2537
2538 if (sid == osid)
1da177e4
LT
2539 return;
2540
a6f76f23
DH
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 */
6b6bc620
SS
2548 rc = avc_has_perm(&selinux_state,
2549 osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
1da177e4 2550 if (rc) {
baa73d9e
NP
2551 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 }
1da177e4 2556 spin_lock_irq(&current->sighand->siglock);
9e7c8f8c
ON
2557 if (!fatal_signal_pending(current)) {
2558 flush_sigqueue(&current->pending);
2559 flush_sigqueue(&current->signal->shared_pending);
3bcac026
DH
2560 flush_signal_handlers(current, 1);
2561 sigemptyset(&current->blocked);
9e7c8f8c 2562 recalc_sigpending();
3bcac026 2563 }
1da177e4
LT
2564 spin_unlock_irq(&current->sighand->siglock);
2565 }
2566
a6f76f23
DH
2567 /* Wake up the parent if it is waiting so that it can recheck
2568 * wait permission to the new task SID. */
ecd6de3c 2569 read_lock(&tasklist_lock);
0b7570e7 2570 __wake_up_parent(current, current->real_parent);
ecd6de3c 2571 read_unlock(&tasklist_lock);
1da177e4
LT
2572}
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
99dbbb59 2586static inline int opt_len(const char *s)
1da177e4 2587{
99dbbb59
AV
2588 bool open_quote = false;
2589 int len;
2590 char c;
1da177e4 2591
99dbbb59
AV
2592 for (len = 0; (c = s[len]) != '\0'; len++) {
2593 if (c == '"')
2594 open_quote = !open_quote;
2595 if (c == ',' && !open_quote)
2596 break;
3528a953 2597 }
99dbbb59 2598 return len;
3528a953
CO
2599}
2600
99dbbb59 2601static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
1da177e4 2602{
99dbbb59
AV
2603 char *from = options;
2604 char *to = options;
2605 bool first = true;
1da177e4 2606
99dbbb59
AV
2607 while (1) {
2608 int len = opt_len(from);
2609 int token, rc;
2610 char *arg = NULL;
1da177e4 2611
99dbbb59 2612 token = match_opt_prefix(from, len, &arg);
1da177e4 2613
99dbbb59
AV
2614 if (token != Opt_error) {
2615 char *p, *q;
1da177e4 2616
99dbbb59
AV
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;
1da177e4 2644 }
99dbbb59
AV
2645 if (!from[len])
2646 break;
2647 from += len + 1;
2648 }
2649 *to = '\0';
2650 return 0;
1da177e4
LT
2651}
2652
204cc0cc 2653static int selinux_sb_remount(struct super_block *sb, void *mnt_opts)
026eb167 2654{
bd323655 2655 struct selinux_mnt_opts *opts = mnt_opts;
026eb167 2656 struct superblock_security_struct *sbsec = sb->s_security;
bd323655
AV
2657 u32 sid;
2658 int rc;
026eb167
EP
2659
2660 if (!(sbsec->flags & SE_SBINITIALIZED))
2661 return 0;
2662
204cc0cc 2663 if (!opts)
026eb167
EP
2664 return 0;
2665
bd323655
AV
2666 if (opts->fscontext) {
2667 rc = parse_sid(sb, opts->fscontext, &sid);
2668 if (rc)
c039bc3c 2669 return rc;
bd323655
AV
2670 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2671 goto out_bad_option;
026eb167 2672 }
bd323655
AV
2673 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;
026eb167 2695 }
c039bc3c 2696 return 0;
026eb167 2697
026eb167 2698out_bad_option:
c103a91e 2699 pr_warn("SELinux: unable to change security options "
29b1deb2
LT
2700 "during remount (dev %s, type=%s)\n", sb->s_id,
2701 sb->s_type->name);
c039bc3c 2702 return -EINVAL;
026eb167
EP
2703}
2704
a10d7c22 2705static int selinux_sb_kern_mount(struct super_block *sb)
1da177e4 2706{
88e67f3b 2707 const struct cred *cred = current_cred();
2bf49690 2708 struct common_audit_data ad;
74192246 2709
50c205f5 2710 ad.type = LSM_AUDIT_DATA_DENTRY;
a269434d 2711 ad.u.dentry = sb->s_root;
88e67f3b 2712 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
1da177e4
LT
2713}
2714
726c3342 2715static int selinux_sb_statfs(struct dentry *dentry)
1da177e4 2716{
88e67f3b 2717 const struct cred *cred = current_cred();
2bf49690 2718 struct common_audit_data ad;
1da177e4 2719
50c205f5 2720 ad.type = LSM_AUDIT_DATA_DENTRY;
a269434d 2721 ad.u.dentry = dentry->d_sb->s_root;
88e67f3b 2722 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
1da177e4
LT
2723}
2724
808d4e3c 2725static int selinux_mount(const char *dev_name,
8a04c43b 2726 const struct path *path,
808d4e3c 2727 const char *type,
828dfe1d
EP
2728 unsigned long flags,
2729 void *data)
1da177e4 2730{
88e67f3b 2731 const struct cred *cred = current_cred();
1da177e4
LT
2732
2733 if (flags & MS_REMOUNT)
d8c9584e 2734 return superblock_has_perm(cred, path->dentry->d_sb,
828dfe1d 2735 FILESYSTEM__REMOUNT, NULL);
1da177e4 2736 else
2875fa00 2737 return path_has_perm(cred, path, FILE__MOUNTON);
1da177e4
LT
2738}
2739
2740static int selinux_umount(struct vfsmount *mnt, int flags)
2741{
88e67f3b 2742 const struct cred *cred = current_cred();
1da177e4 2743
88e67f3b 2744 return superblock_has_perm(cred, mnt->mnt_sb,
828dfe1d 2745 FILESYSTEM__UNMOUNT, NULL);
1da177e4
LT
2746}
2747
0b52075e
AV
2748static 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
442155c1
DH
2786static 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
1da177e4
LT
2818/* 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
d47be3df 2830static int selinux_dentry_init_security(struct dentry *dentry, int mode,
4f3ccd76 2831 const struct qstr *name, void **ctx,
d47be3df
DQ
2832 u32 *ctxlen)
2833{
d47be3df
DQ
2834 u32 newsid;
2835 int rc;
2836
0c6cfa62 2837 rc = selinux_determine_inode_label(selinux_cred(current_cred()),
c957f6df 2838 d_inode(dentry->d_parent), name,
c3c188b2
DH
2839 inode_mode_to_security_class(mode),
2840 &newsid);
2841 if (rc)
2842 return rc;
d47be3df 2843
aa8e712c
SS
2844 return security_sid_to_context(&selinux_state, newsid, (char **)ctx,
2845 ctxlen);
d47be3df
DQ
2846}
2847
a518b0a5
VG
2848static 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
0c6cfa62 2857 rc = selinux_determine_inode_label(selinux_cred(old),
a518b0a5
VG
2858 d_inode(dentry->d_parent), name,
2859 inode_mode_to_security_class(mode),
2860 &newsid);
2861 if (rc)
2862 return rc;
2863
0c6cfa62 2864 tsec = selinux_cred(new);
a518b0a5
VG
2865 tsec->create_sid = newsid;
2866 return 0;
2867}
2868
5e41ff9e 2869static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
9548906b
TH
2870 const struct qstr *qstr,
2871 const char **name,
2a7dba39 2872 void **value, size_t *len)
5e41ff9e 2873{
0c6cfa62 2874 const struct task_security_struct *tsec = selinux_cred(current_cred());
5e41ff9e 2875 struct superblock_security_struct *sbsec;
c0d4f464 2876 u32 newsid, clen;
5e41ff9e 2877 int rc;
9548906b 2878 char *context;
5e41ff9e 2879
5e41ff9e 2880 sbsec = dir->i_sb->s_security;
5e41ff9e 2881
275bb41e
DH
2882 newsid = tsec->create_sid;
2883
0c6cfa62 2884 rc = selinux_determine_inode_label(selinux_cred(current_cred()),
c3c188b2
DH
2885 dir, qstr,
2886 inode_mode_to_security_class(inode->i_mode),
2887 &newsid);
2888 if (rc)
2889 return rc;
5e41ff9e 2890
296fddf7 2891 /* Possibly defer initialization to selinux_complete_init. */
0d90a7ec 2892 if (sbsec->flags & SE_SBINITIALIZED) {
80788c22 2893 struct inode_security_struct *isec = selinux_inode(inode);
296fddf7
EP
2894 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2895 isec->sid = newsid;
6f3be9f5 2896 isec->initialized = LABEL_INITIALIZED;
296fddf7 2897 }
5e41ff9e 2898
aa8e712c 2899 if (!selinux_state.initialized || !(sbsec->flags & SBLABEL_MNT))
25a74f3b
SS
2900 return -EOPNOTSUPP;
2901
9548906b
TH
2902 if (name)
2903 *name = XATTR_SELINUX_SUFFIX;
5e41ff9e 2904
570bc1c2 2905 if (value && len) {
aa8e712c
SS
2906 rc = security_sid_to_context_force(&selinux_state, newsid,
2907 &context, &clen);
9548906b 2908 if (rc)
570bc1c2 2909 return rc;
570bc1c2
SS
2910 *value = context;
2911 *len = clen;
5e41ff9e 2912 }
5e41ff9e 2913
5e41ff9e
SS
2914 return 0;
2915}
2916
4acdaf27 2917static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
1da177e4
LT
2918{
2919 return may_create(dir, dentry, SECCLASS_FILE);
2920}
2921
1da177e4
LT
2922static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2923{
1da177e4
LT
2924 return may_link(dir, old_dentry, MAY_LINK);
2925}
2926
1da177e4
LT
2927static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2928{
1da177e4
LT
2929 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
18bb1db3 2937static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
1da177e4
LT
2938{
2939 return may_create(dir, dentry, SECCLASS_DIR);
2940}
2941
1da177e4
LT
2942static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2943{
2944 return may_link(dir, dentry, MAY_RMDIR);
2945}
2946
1a67aafb 2947static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
1da177e4 2948{
1da177e4
LT
2949 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2950}
2951
1da177e4 2952static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
828dfe1d 2953 struct inode *new_inode, struct dentry *new_dentry)
1da177e4
LT
2954{
2955 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2956}
2957
1da177e4
LT
2958static int selinux_inode_readlink(struct dentry *dentry)
2959{
88e67f3b
DH
2960 const struct cred *cred = current_cred();
2961
2875fa00 2962 return dentry_has_perm(cred, dentry, FILE__READ);
1da177e4
LT
2963}
2964
bda0be7a
N
2965static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
2966 bool rcu)
1da177e4 2967{
88e67f3b 2968 const struct cred *cred = current_cred();
bda0be7a
N
2969 struct common_audit_data ad;
2970 struct inode_security_struct *isec;
2971 u32 sid;
1da177e4 2972
bda0be7a
N
2973 validate_creds(cred);
2974
2975 ad.type = LSM_AUDIT_DATA_DENTRY;
2976 ad.u.dentry = dentry;
2977 sid = cred_sid(cred);
5d226df4
AG
2978 isec = inode_security_rcu(inode, rcu);
2979 if (IS_ERR(isec))
2980 return PTR_ERR(isec);
bda0be7a 2981
e46e01ee
SS
2982 return avc_has_perm(&selinux_state,
2983 sid, isec->sid, isec->sclass, FILE__READ, &ad);
1da177e4
LT
2984}
2985
d4cf970d
EP
2986static noinline int audit_inode_permission(struct inode *inode,
2987 u32 perms, u32 audited, u32 denied,
626b9740 2988 int result,
d4cf970d 2989 unsigned flags)
1da177e4 2990{
b782e0a6 2991 struct common_audit_data ad;
80788c22 2992 struct inode_security_struct *isec = selinux_inode(inode);
d4cf970d
EP
2993 int rc;
2994
50c205f5 2995 ad.type = LSM_AUDIT_DATA_INODE;
d4cf970d
EP
2996 ad.u.inode = inode;
2997
6b6bc620
SS
2998 rc = slow_avc_audit(&selinux_state,
2999 current_sid(), isec->sid, isec->sclass, perms,
626b9740 3000 audited, denied, result, &ad, flags);
d4cf970d
EP
3001 if (rc)
3002 return rc;
3003 return 0;
3004}
3005
e74f71eb 3006static int selinux_inode_permission(struct inode *inode, int mask)
1da177e4 3007{
88e67f3b 3008 const struct cred *cred = current_cred();
b782e0a6
EP
3009 u32 perms;
3010 bool from_access;
cf1dd1da 3011 unsigned flags = mask & MAY_NOT_BLOCK;
2e334057
EP
3012 struct inode_security_struct *isec;
3013 u32 sid;
3014 struct av_decision avd;
3015 int rc, rc2;
3016 u32 audited, denied;
1da177e4 3017
b782e0a6 3018 from_access = mask & MAY_ACCESS;
d09ca739
EP
3019 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
3020
b782e0a6
EP
3021 /* No permission to check. Existence test. */
3022 if (!mask)
1da177e4 3023 return 0;
1da177e4 3024
2e334057 3025 validate_creds(cred);
b782e0a6 3026
2e334057
EP
3027 if (unlikely(IS_PRIVATE(inode)))
3028 return 0;
b782e0a6
EP
3029
3030 perms = file_mask_to_av(inode->i_mode, mask);
3031
2e334057 3032 sid = cred_sid(cred);
5d226df4
AG
3033 isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK);
3034 if (IS_ERR(isec))
3035 return PTR_ERR(isec);
2e334057 3036
6b6bc620 3037 rc = avc_has_perm_noaudit(&selinux_state,
3a28cff3
SS
3038 sid, isec->sid, isec->sclass, perms,
3039 (flags & MAY_NOT_BLOCK) ? AVC_NONBLOCKING : 0,
3040 &avd);
2e334057
EP
3041 audited = avc_audit_required(perms, &avd, rc,
3042 from_access ? FILE__AUDIT_ACCESS : 0,
3043 &denied);
3044 if (likely(!audited))
3045 return rc;
3046
626b9740 3047 rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags);
2e334057
EP
3048 if (rc2)
3049 return rc2;
3050 return rc;
1da177e4
LT
3051}
3052
3053static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
3054{
88e67f3b 3055 const struct cred *cred = current_cred();
ccb54478 3056 struct inode *inode = d_backing_inode(dentry);
bc6a6008 3057 unsigned int ia_valid = iattr->ia_valid;
95dbf739 3058 __u32 av = FILE__WRITE;
1da177e4 3059
bc6a6008
AW
3060 /* 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 }
1da177e4 3067
bc6a6008
AW
3068 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3069 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
2875fa00 3070 return dentry_has_perm(cred, dentry, FILE__SETATTR);
1da177e4 3071
aa8e712c 3072 if (selinux_policycap_openperm() &&
ccb54478
SS
3073 inode->i_sb->s_magic != SOCKFS_MAGIC &&
3074 (ia_valid & ATTR_SIZE) &&
3075 !(ia_valid & ATTR_FILE))
95dbf739
EP
3076 av |= FILE__OPEN;
3077
3078 return dentry_has_perm(cred, dentry, av);
1da177e4
LT
3079}
3080
3f7036a0 3081static int selinux_inode_getattr(const struct path *path)
1da177e4 3082{
3f7036a0 3083 return path_has_perm(current_cred(), path, FILE__GETATTR);
1da177e4
LT
3084}
3085
db59000a
SS
3086static bool has_cap_mac_admin(bool audit)
3087{
3088 const struct cred *cred = current_cred();
c1a85a00 3089 unsigned int opts = audit ? CAP_OPT_NONE : CAP_OPT_NOAUDIT;
db59000a 3090
c1a85a00 3091 if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, opts))
db59000a 3092 return false;
c1a85a00 3093 if (cred_has_capability(cred, CAP_MAC_ADMIN, opts, true))
db59000a
SS
3094 return false;
3095 return true;
3096}
3097
8f0cfa52
DH
3098static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
3099 const void *value, size_t size, int flags)
1da177e4 3100{
c6f493d6 3101 struct inode *inode = d_backing_inode(dentry);
20cdef8d 3102 struct inode_security_struct *isec;
1da177e4 3103 struct superblock_security_struct *sbsec;
2bf49690 3104 struct common_audit_data ad;
275bb41e 3105 u32 newsid, sid = current_sid();
1da177e4
LT
3106 int rc = 0;
3107
6b240306
EB
3108 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 }
1da177e4
LT
3117
3118 sbsec = inode->i_sb->s_security;
12f348b9 3119 if (!(sbsec->flags & SBLABEL_MNT))
1da177e4
LT
3120 return -EOPNOTSUPP;
3121
2e149670 3122 if (!inode_owner_or_capable(inode))
1da177e4
LT
3123 return -EPERM;
3124
50c205f5 3125 ad.type = LSM_AUDIT_DATA_DENTRY;
a269434d 3126 ad.u.dentry = dentry;
1da177e4 3127
20cdef8d 3128 isec = backing_inode_security(dentry);
6b6bc620
SS
3129 rc = avc_has_perm(&selinux_state,
3130 sid, isec->sid, isec->sclass,
1da177e4
LT
3131 FILE__RELABELFROM, &ad);
3132 if (rc)
3133 return rc;
3134
aa8e712c
SS
3135 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3136 GFP_KERNEL);
12b29f34 3137 if (rc == -EINVAL) {
db59000a 3138 if (!has_cap_mac_admin(true)) {
d6ea83ec
EP
3139 struct audit_buffer *ab;
3140 size_t audit_size;
d6ea83ec
EP
3141
3142 /* We strip a nul only if it is at the end, otherwise the
3143 * context contains a nul and we should audit that */
e3fea3f7 3144 if (value) {
add24372
CIK
3145 const char *str = value;
3146
e3fea3f7
AV
3147 if (str[size - 1] == '\0')
3148 audit_size = size - 1;
3149 else
3150 audit_size = size;
3151 } else {
e3fea3f7
AV
3152 audit_size = 0;
3153 }
cdfb6b34
RGB
3154 ab = audit_log_start(audit_context(),
3155 GFP_ATOMIC, AUDIT_SELINUX_ERR);
d6ea83ec
EP
3156 audit_log_format(ab, "op=setxattr invalid_context=");
3157 audit_log_n_untrustedstring(ab, value, audit_size);
3158 audit_log_end(ab);
3159
12b29f34 3160 return rc;
d6ea83ec 3161 }
aa8e712c
SS
3162 rc = security_context_to_sid_force(&selinux_state, value,
3163 size, &newsid);
12b29f34 3164 }
1da177e4
LT
3165 if (rc)
3166 return rc;
3167
6b6bc620
SS
3168 rc = avc_has_perm(&selinux_state,
3169 sid, newsid, isec->sclass,
1da177e4
LT
3170 FILE__RELABELTO, &ad);
3171 if (rc)
3172 return rc;
3173
aa8e712c
SS
3174 rc = security_validate_transition(&selinux_state, isec->sid, newsid,
3175 sid, isec->sclass);
1da177e4
LT
3176 if (rc)
3177 return rc;
3178
6b6bc620
SS
3179 return avc_has_perm(&selinux_state,
3180 newsid,
1da177e4
LT
3181 sbsec->sid,
3182 SECCLASS_FILESYSTEM,
3183 FILESYSTEM__ASSOCIATE,
3184 &ad);
3185}
3186
8f0cfa52 3187static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
f5269710 3188 const void *value, size_t size,
8f0cfa52 3189 int flags)
1da177e4 3190{
c6f493d6 3191 struct inode *inode = d_backing_inode(dentry);
20cdef8d 3192 struct inode_security_struct *isec;
1da177e4
LT
3193 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
aa8e712c
SS
3201 rc = security_context_to_sid_force(&selinux_state, value, size,
3202 &newsid);
1da177e4 3203 if (rc) {
c103a91e 3204 pr_err("SELinux: unable to map context to SID"
12b29f34
SS
3205 "for (%s, %lu), rc=%d\n",
3206 inode->i_sb->s_id, inode->i_ino, -rc);
1da177e4
LT
3207 return;
3208 }
3209
20cdef8d 3210 isec = backing_inode_security(dentry);
9287aed2 3211 spin_lock(&isec->lock);
aa9c2669 3212 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1da177e4 3213 isec->sid = newsid;
6f3be9f5 3214 isec->initialized = LABEL_INITIALIZED;
9287aed2 3215 spin_unlock(&isec->lock);
aa9c2669 3216
1da177e4
LT
3217 return;
3218}
3219
8f0cfa52 3220static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
1da177e4 3221{
88e67f3b
DH
3222 const struct cred *cred = current_cred();
3223
2875fa00 3224 return dentry_has_perm(cred, dentry, FILE__GETATTR);
1da177e4
LT
3225}
3226
828dfe1d 3227static int selinux_inode_listxattr(struct dentry *dentry)
1da177e4 3228{
88e67f3b
DH
3229 const struct cred *cred = current_cred();
3230
2875fa00 3231 return dentry_has_perm(cred, dentry, FILE__GETATTR);
1da177e4
LT
3232}
3233
8f0cfa52 3234static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
1da177e4 3235{
6b240306
EB
3236 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 }
1da177e4
LT
3245
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
d381d8a9 3251/*
abc69bb6 3252 * Copy the inode security context value to the user.
d381d8a9
JM
3253 *
3254 * Permission check is handled by selinux_inode_getxattr hook.
3255 */
ea861dfd 3256static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
1da177e4 3257{
42492594
DQ
3258 u32 size;
3259 int error;
3260 char *context = NULL;
20cdef8d 3261 struct inode_security_struct *isec;
d381d8a9 3262
8c8570fb
DK
3263 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3264 return -EOPNOTSUPP;
d381d8a9 3265
abc69bb6
SS
3266 /*
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 */
20cdef8d 3275 isec = inode_security(inode);
db59000a 3276 if (has_cap_mac_admin(false))
aa8e712c
SS
3277 error = security_sid_to_context_force(&selinux_state,
3278 isec->sid, &context,
abc69bb6
SS
3279 &size);
3280 else
aa8e712c
SS
3281 error = security_sid_to_context(&selinux_state, isec->sid,
3282 &context, &size);
42492594
DQ
3283 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;
1da177e4
LT
3293}
3294
3295static int selinux_inode_setsecurity(struct inode *inode, const char *name,
828dfe1d 3296 const void *value, size_t size, int flags)
1da177e4 3297{
2c97165b 3298 struct inode_security_struct *isec = inode_security_novalidate(inode);
53e0c2aa 3299 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
1da177e4
LT
3300 u32 newsid;
3301 int rc;
3302
3303 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3304 return -EOPNOTSUPP;
3305
53e0c2aa
OM
3306 if (!(sbsec->flags & SBLABEL_MNT))
3307 return -EOPNOTSUPP;
3308
1da177e4
LT
3309 if (!value || !size)
3310 return -EACCES;
3311
aa8e712c
SS
3312 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3313 GFP_KERNEL);
1da177e4
LT
3314 if (rc)
3315 return rc;
3316
9287aed2 3317 spin_lock(&isec->lock);
aa9c2669 3318 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1da177e4 3319 isec->sid = newsid;
6f3be9f5 3320 isec->initialized = LABEL_INITIALIZED;
9287aed2 3321 spin_unlock(&isec->lock);
1da177e4
LT
3322 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
d6335d77 3333static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
713a04ae 3334{
e817c2f3 3335 struct inode_security_struct *isec = inode_security_novalidate(inode);
713a04ae
AD
3336 *secid = isec->sid;
3337}
3338
56909eb3
VG
3339static 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
0c6cfa62 3351 tsec = selinux_cred(new_creds);
56909eb3
VG
3352 /* 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
19472b69
VG
3359static 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
1da177e4
LT
3374/* file security operations */
3375
788e7dd4 3376static int selinux_revalidate_file_permission(struct file *file, int mask)
1da177e4 3377{
88e67f3b 3378 const struct cred *cred = current_cred();
496ad9aa 3379 struct inode *inode = file_inode(file);
1da177e4 3380
1da177e4
LT
3381 /* 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
389fb800
PM
3385 return file_has_perm(cred, file,
3386 file_mask_to_av(inode->i_mode, mask));
1da177e4
LT
3387}
3388
788e7dd4
YN
3389static int selinux_file_permission(struct file *file, int mask)
3390{
496ad9aa 3391 struct inode *inode = file_inode(file);
bb6c6b02 3392 struct file_security_struct *fsec = selinux_file(file);
b197367e 3393 struct inode_security_struct *isec;
20dda18b
SS
3394 u32 sid = current_sid();
3395
389fb800 3396 if (!mask)
788e7dd4
YN
3397 /* No permission to check. Existence test. */
3398 return 0;
788e7dd4 3399
b197367e 3400 isec = inode_security(inode);
20dda18b 3401 if (sid == fsec->sid && fsec->isid == isec->sid &&
6b6bc620 3402 fsec->pseqno == avc_policy_seqno(&selinux_state))
83d49856 3403 /* No change since file_open check. */
20dda18b
SS
3404 return 0;
3405
788e7dd4
YN
3406 return selinux_revalidate_file_permission(file, mask);
3407}
3408
1da177e4
LT
3409static int selinux_file_alloc_security(struct file *file)
3410{
3411 return file_alloc_security(file);
3412}
3413
fa1aa143
JVS
3414/*
3415 * Check whether a task has the ioctl permission and cmd
3416 * operation to an inode.
3417 */
1d2a168a 3418static int ioctl_has_perm(const struct cred *cred, struct file *file,
fa1aa143
JVS
3419 u32 requested, u16 cmd)
3420{
3421 struct common_audit_data ad;
bb6c6b02 3422 struct file_security_struct *fsec = selinux_file(file);
fa1aa143 3423 struct inode *inode = file_inode(file);
20cdef8d 3424 struct inode_security_struct *isec;
fa1aa143
JVS
3425 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) {
6b6bc620
SS
3437 rc = avc_has_perm(&selinux_state,
3438 ssid, fsec->sid,
fa1aa143
JVS
3439 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
20cdef8d 3449 isec = inode_security(inode);
6b6bc620
SS
3450 rc = avc_has_extended_perms(&selinux_state,
3451 ssid, isec->sid, isec->sclass,
3452 requested, driver, xperm, &ad);
fa1aa143
JVS
3453out:
3454 return rc;
3455}
3456
1da177e4
LT
3457static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3458 unsigned long arg)
3459{
88e67f3b 3460 const struct cred *cred = current_cred();
0b24dcb7 3461 int error = 0;
1da177e4 3462
0b24dcb7
EP
3463 switch (cmd) {
3464 case FIONREAD:
3465 /* fall through */
3466 case FIBMAP:
3467 /* fall through */
3468 case FIGETBSZ:
3469 /* fall through */
2f99c369 3470 case FS_IOC_GETFLAGS:
0b24dcb7 3471 /* fall through */
2f99c369 3472 case FS_IOC_GETVERSION:
0b24dcb7
EP
3473 error = file_has_perm(cred, file, FILE__GETATTR);
3474 break;
1da177e4 3475
2f99c369 3476 case FS_IOC_SETFLAGS:
0b24dcb7 3477 /* fall through */
2f99c369 3478 case FS_IOC_SETVERSION:
0b24dcb7
EP
3479 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;
1da177e4 3488
0b24dcb7
EP
3489 case KDSKBENT:
3490 case KDSKBSENT:
6a9de491 3491 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
c1a85a00 3492 CAP_OPT_NONE, true);
0b24dcb7
EP
3493 break;
3494
3495 /* default case assumes that the command will go
3496 * to the file's ioctl() function.
3497 */
3498 default:
fa1aa143 3499 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
0b24dcb7
EP
3500 }
3501 return error;
1da177e4
LT
3502}
3503
fcaaade1
SS
3504static int default_noexec;
3505
1da177e4
LT
3506static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3507{
88e67f3b 3508 const struct cred *cred = current_cred();
be0554c9 3509 u32 sid = cred_sid(cred);
d84f4f99 3510 int rc = 0;
88e67f3b 3511
fcaaade1 3512 if (default_noexec &&
892e8cac
SS
3513 (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
3514 (!shared && (prot & PROT_WRITE)))) {
1da177e4
LT
3515 /*
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 */
6b6bc620
SS
3520 rc = avc_has_perm(&selinux_state,
3521 sid, sid, SECCLASS_PROCESS,
be0554c9 3522 PROCESS__EXECMEM, NULL);
1da177e4 3523 if (rc)
d84f4f99 3524 goto error;
1da177e4 3525 }
1da177e4
LT
3526
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
88e67f3b 3538 return file_has_perm(cred, file, av);
1da177e4 3539 }
d84f4f99
DH
3540
3541error:
3542 return rc;
1da177e4
LT
3543}
3544
e5467859 3545static int selinux_mmap_addr(unsigned long addr)
1da177e4 3546{
b1d9e6b0 3547 int rc = 0;
1da177e4 3548
a2551df7 3549 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
98883bfd 3550 u32 sid = current_sid();
6b6bc620
SS
3551 rc = avc_has_perm(&selinux_state,
3552 sid, sid, SECCLASS_MEMPROTECT,
ed032189 3553 MEMPROTECT__MMAP_ZERO, NULL);
84336d1a
EP
3554 }
3555
98883bfd 3556 return rc;
e5467859 3557}
1da177e4 3558
e5467859
AV
3559static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3560 unsigned long prot, unsigned long flags)
3561{
3ba4bf5f
SS
3562 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
aa8e712c 3574 if (selinux_state.checkreqprot)
1da177e4
LT
3575 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{
88e67f3b 3585 const struct cred *cred = current_cred();
be0554c9 3586 u32 sid = cred_sid(cred);
1da177e4 3587
aa8e712c 3588 if (selinux_state.checkreqprot)
1da177e4
LT
3589 prot = reqprot;
3590
fcaaade1
SS
3591 if (default_noexec &&
3592 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
d541bbee 3593 int rc = 0;
db4c9641
SS
3594 if (vma->vm_start >= vma->vm_mm->start_brk &&
3595 vma->vm_end <= vma->vm_mm->brk) {
6b6bc620
SS
3596 rc = avc_has_perm(&selinux_state,
3597 sid, sid, SECCLASS_PROCESS,
be0554c9 3598 PROCESS__EXECHEAP, NULL);
db4c9641 3599 } else if (!vma->vm_file &&
c2316dbf
SS
3600 ((vma->vm_start <= vma->vm_mm->start_stack &&
3601 vma->vm_end >= vma->vm_mm->start_stack) ||
d17af505 3602 vma_is_stack_for_current(vma))) {
6b6bc620
SS
3603 rc = avc_has_perm(&selinux_state,
3604 sid, sid, SECCLASS_PROCESS,
be0554c9 3605 PROCESS__EXECSTACK, NULL);
db4c9641
SS
3606 } 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 */
d84f4f99 3614 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
db4c9641 3615 }