security.c 44.7 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6
/*
 * Security plug functions
 *
 * Copyright (C) 2001 WireX Communications, Inc <chris@wirex.com>
 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
 * Copyright (C) 2001 Networks Associates Technology, Inc <ssmalley@nai.com>
7
 * Copyright (C) 2016 Mellanox Technologies
Linus Torvalds's avatar
Linus Torvalds committed
8 9 10 11 12 13 14
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 2 of the License, or
 *	(at your option) any later version.
 */

15
#include <linux/bpf.h>
16
#include <linux/capability.h>
17
#include <linux/dcache.h>
Linus Torvalds's avatar
Linus Torvalds committed
18 19 20
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
Casey Schaufler's avatar
Casey Schaufler committed
21
#include <linux/lsm_hooks.h>
22
#include <linux/integrity.h>
23
#include <linux/ima.h>
24
#include <linux/evm.h>
Al Viro's avatar
Al Viro committed
25
#include <linux/fsnotify.h>
26 27 28
#include <linux/mman.h>
#include <linux/mount.h>
#include <linux/personality.h>
Paul Mundt's avatar
Paul Mundt committed
29
#include <linux/backing-dev.h>
30
#include <linux/string.h>
Al Viro's avatar
Al Viro committed
31
#include <net/flow.h>
Linus Torvalds's avatar
Linus Torvalds committed
32

33 34
#include <trace/events/initcall.h>

35
#define MAX_LSM_EVM_XATTR	2
Linus Torvalds's avatar
Linus Torvalds committed
36

37 38 39
/* Maximum number of letters for an LSM name string */
#define SECURITY_NAME_MAX	10

40
struct security_hook_heads security_hook_heads __lsm_ro_after_init;
41 42
static ATOMIC_NOTIFIER_HEAD(lsm_notifier_chain);

43
char *lsm_names;
44
/* Boot-time LSM user choice */
45 46
static __initdata char chosen_lsm[SECURITY_NAME_MAX + 1] =
	CONFIG_DEFAULT_SECURITY;
Linus Torvalds's avatar
Linus Torvalds committed
47 48 49

static void __init do_security_initcalls(void)
{
50
	int ret;
Linus Torvalds's avatar
Linus Torvalds committed
51 52
	initcall_t *call;
	call = __security_initcall_start;
53
	trace_initcall_level("security");
Linus Torvalds's avatar
Linus Torvalds committed
54
	while (call < __security_initcall_end) {
55 56 57
		trace_initcall_start((*call));
		ret = (*call) ();
		trace_initcall_finish((*call), ret);
Linus Torvalds's avatar
Linus Torvalds committed
58 59 60 61 62 63 64 65 66 67 68
		call++;
	}
}

/**
 * security_init - initializes the security framework
 *
 * This should be called early in the kernel initialization sequence.
 */
int __init security_init(void)
{
69
	int i;
70
	struct hlist_head *list = (struct hlist_head *) &security_hook_heads;
71

72
	for (i = 0; i < sizeof(security_hook_heads) / sizeof(struct hlist_head);
73
	     i++)
74
		INIT_HLIST_HEAD(&list[i]);
75
	pr_info("Security Framework initialized\n");
Linus Torvalds's avatar
Linus Torvalds committed
76

77
	/*
78
	 * Load minor LSMs, with the capability module always first.
79 80 81
	 */
	capability_add_hooks();
	yama_add_hooks();
82
	loadpin_add_hooks();
83

84
	/*
85
	 * Load all the remaining security modules.
86
	 */
Linus Torvalds's avatar
Linus Torvalds committed
87 88 89 90 91
	do_security_initcalls();

	return 0;
}

92 93 94 95 96 97 98 99
/* Save user chosen LSM */
static int __init choose_lsm(char *str)
{
	strncpy(chosen_lsm, str, SECURITY_NAME_MAX);
	return 1;
}
__setup("security=", choose_lsm);

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
static bool match_last_lsm(const char *list, const char *lsm)
{
	const char *last;

	if (WARN_ON(!list || !lsm))
		return false;
	last = strrchr(list, ',');
	if (last)
		/* Pass the comma, strcmp() will check for '\0' */
		last++;
	else
		last = list;
	return !strcmp(last, lsm);
}

115 116 117 118 119 120 121
static int lsm_append(char *new, char **result)
{
	char *cp;

	if (*result == NULL) {
		*result = kstrdup(new, GFP_KERNEL);
	} else {
122 123 124
		/* Check if it is the last registered name */
		if (match_last_lsm(*result, new))
			return 0;
125 126 127 128 129 130 131 132 133
		cp = kasprintf(GFP_KERNEL, "%s,%s", *result, new);
		if (cp == NULL)
			return -ENOMEM;
		kfree(*result);
		*result = cp;
	}
	return 0;
}

134 135
/**
 * security_module_enable - Load given security module on boot ?
136
 * @module: the name of the module
137 138 139
 *
 * Each LSM must pass this method before registering its own operations
 * to avoid security registration races. This method may also be used
140
 * to check if your LSM is currently loaded during kernel initialization.
141
 *
142 143 144 145 146 147 148 149
 * Returns:
 *
 * true if:
 *
 * - The passed LSM is the one chosen by user at boot time,
 * - or the passed LSM is configured as the default and the user did not
 *   choose an alternate LSM at boot time.
 *
150 151
 * Otherwise, return false.
 */
152
int __init security_module_enable(const char *module)
153
{
154
	return !strcmp(module, chosen_lsm);
155 156
}

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
/**
 * security_add_hooks - Add a modules hooks to the hook lists.
 * @hooks: the hooks to add
 * @count: the number of hooks to add
 * @lsm: the name of the security module
 *
 * Each LSM has to register its hooks with the infrastructure.
 */
void __init security_add_hooks(struct security_hook_list *hooks, int count,
				char *lsm)
{
	int i;

	for (i = 0; i < count; i++) {
		hooks[i].lsm = lsm;
172
		hlist_add_tail_rcu(&hooks[i].list, hooks[i].head);
173 174 175 176 177
	}
	if (lsm_append(lsm, &lsm_names) < 0)
		panic("%s - Cannot get early memory.\n", __func__);
}

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
int call_lsm_notifier(enum lsm_event event, void *data)
{
	return atomic_notifier_call_chain(&lsm_notifier_chain, event, data);
}
EXPORT_SYMBOL(call_lsm_notifier);

int register_lsm_notifier(struct notifier_block *nb)
{
	return atomic_notifier_chain_register(&lsm_notifier_chain, nb);
}
EXPORT_SYMBOL(register_lsm_notifier);

int unregister_lsm_notifier(struct notifier_block *nb)
{
	return atomic_notifier_chain_unregister(&lsm_notifier_chain, nb);
}
EXPORT_SYMBOL(unregister_lsm_notifier);

196
/*
197
 * Hook list operation macros.
Linus Torvalds's avatar
Linus Torvalds committed
198
 *
199 200
 * call_void_hook:
 *	This is a hook that does not return a value.
Linus Torvalds's avatar
Linus Torvalds committed
201
 *
202 203
 * call_int_hook:
 *	This is a hook that returns a value.
Linus Torvalds's avatar
Linus Torvalds committed
204 205
 */

206 207 208 209
#define call_void_hook(FUNC, ...)				\
	do {							\
		struct security_hook_list *P;			\
								\
210
		hlist_for_each_entry(P, &security_hook_heads.FUNC, list) \
211 212 213 214 215 216 217 218
			P->hook.FUNC(__VA_ARGS__);		\
	} while (0)

#define call_int_hook(FUNC, IRC, ...) ({			\
	int RC = IRC;						\
	do {							\
		struct security_hook_list *P;			\
								\
219
		hlist_for_each_entry(P, &security_hook_heads.FUNC, list) { \
220 221 222 223 224 225 226
			RC = P->hook.FUNC(__VA_ARGS__);		\
			if (RC != 0)				\
				break;				\
		}						\
	} while (0);						\
	RC;							\
})
Linus Torvalds's avatar
Linus Torvalds committed
227

228 229
/* Security operations */

230 231
int security_binder_set_context_mgr(struct task_struct *mgr)
{
232
	return call_int_hook(binder_set_context_mgr, 0, mgr);
233 234 235 236 237
}

int security_binder_transaction(struct task_struct *from,
				struct task_struct *to)
{
238
	return call_int_hook(binder_transaction, 0, from, to);
239 240 241 242 243
}

int security_binder_transfer_binder(struct task_struct *from,
				    struct task_struct *to)
{
244
	return call_int_hook(binder_transfer_binder, 0, from, to);
245 246 247 248 249
}

int security_binder_transfer_file(struct task_struct *from,
				  struct task_struct *to, struct file *file)
{
250
	return call_int_hook(binder_transfer_file, 0, from, to, file);
251 252
}

253
int security_ptrace_access_check(struct task_struct *child, unsigned int mode)
254
{
255
	return call_int_hook(ptrace_access_check, 0, child, mode);
256 257 258 259
}

int security_ptrace_traceme(struct task_struct *parent)
{
260
	return call_int_hook(ptrace_traceme, 0, parent);
261 262 263 264 265 266 267
}

int security_capget(struct task_struct *target,
		     kernel_cap_t *effective,
		     kernel_cap_t *inheritable,
		     kernel_cap_t *permitted)
{
268 269
	return call_int_hook(capget, 0, target,
				effective, inheritable, permitted);
270 271
}

272 273 274 275
int security_capset(struct cred *new, const struct cred *old,
		    const kernel_cap_t *effective,
		    const kernel_cap_t *inheritable,
		    const kernel_cap_t *permitted)
276
{
277 278
	return call_int_hook(capset, 0, new, old,
				effective, inheritable, permitted);
279 280
}

281
int security_capable(const struct cred *cred, struct user_namespace *ns,
282
		     int cap)
283
{
284
	return call_int_hook(capable, 0, cred, ns, cap, SECURITY_CAP_AUDIT);
285 286
}

287 288
int security_capable_noaudit(const struct cred *cred, struct user_namespace *ns,
			     int cap)
289
{
290
	return call_int_hook(capable, 0, cred, ns, cap, SECURITY_CAP_NOAUDIT);
291 292 293 294
}

int security_quotactl(int cmds, int type, int id, struct super_block *sb)
{
295
	return call_int_hook(quotactl, 0, cmds, type, id, sb);
296 297 298 299
}

int security_quota_on(struct dentry *dentry)
{
300
	return call_int_hook(quota_on, 0, dentry);
301 302
}

303
int security_syslog(int type)
304
{
305
	return call_int_hook(syslog, 0, type);
306 307
}

308
int security_settime64(const struct timespec64 *ts, const struct timezone *tz)
309
{
310
	return call_int_hook(settime, 0, ts, tz);
311 312 313 314
}

int security_vm_enough_memory_mm(struct mm_struct *mm, long pages)
{
315 316 317 318 319 320 321 322 323 324 325
	struct security_hook_list *hp;
	int cap_sys_admin = 1;
	int rc;

	/*
	 * The module will respond with a positive value if
	 * it thinks the __vm_enough_memory() call should be
	 * made with the cap_sys_admin set. If all of the modules
	 * agree that it should be set it will. If any module
	 * thinks it should not be set it won't.
	 */
326
	hlist_for_each_entry(hp, &security_hook_heads.vm_enough_memory, list) {
327 328 329 330 331 332 333
		rc = hp->hook.vm_enough_memory(mm, pages);
		if (rc <= 0) {
			cap_sys_admin = 0;
			break;
		}
	}
	return __vm_enough_memory(mm, pages, cap_sys_admin);
334 335
}

336
int security_bprm_set_creds(struct linux_binprm *bprm)
337
{
338
	return call_int_hook(bprm_set_creds, 0, bprm);
339 340
}

341
int security_bprm_check(struct linux_binprm *bprm)
342
{
343 344
	int ret;

345
	ret = call_int_hook(bprm_check_security, 0, bprm);
346 347 348
	if (ret)
		return ret;
	return ima_bprm_check(bprm);
349 350
}

351
void security_bprm_committing_creds(struct linux_binprm *bprm)
352
{
353
	call_void_hook(bprm_committing_creds, bprm);
354 355
}

356
void security_bprm_committed_creds(struct linux_binprm *bprm)
357
{
358
	call_void_hook(bprm_committed_creds, bprm);
359 360 361 362
}

int security_sb_alloc(struct super_block *sb)
{
363
	return call_int_hook(sb_alloc_security, 0, sb);
364 365 366 367
}

void security_sb_free(struct super_block *sb)
{
368
	call_void_hook(sb_free_security, sb);
369 370
}

371
int security_sb_copy_data(char *orig, char *copy)
372
{
373
	return call_int_hook(sb_copy_data, 0, orig, copy);
374
}
375
EXPORT_SYMBOL(security_sb_copy_data);
376

377 378
int security_sb_remount(struct super_block *sb, void *data)
{
379
	return call_int_hook(sb_remount, 0, sb, data);
380 381
}

382
int security_sb_kern_mount(struct super_block *sb, int flags, void *data)
383
{
384
	return call_int_hook(sb_kern_mount, 0, sb, flags, data);
385 386
}

387 388
int security_sb_show_options(struct seq_file *m, struct super_block *sb)
{
389
	return call_int_hook(sb_show_options, 0, m, sb);
390 391
}

392 393
int security_sb_statfs(struct dentry *dentry)
{
394
	return call_int_hook(sb_statfs, 0, dentry);
395 396
}

Al Viro's avatar
Al Viro committed
397
int security_sb_mount(const char *dev_name, const struct path *path,
Al Viro's avatar
Al Viro committed
398
                       const char *type, unsigned long flags, void *data)
399
{
400
	return call_int_hook(sb_mount, 0, dev_name, path, type, flags, data);
401 402 403 404
}

int security_sb_umount(struct vfsmount *mnt, int flags)
{
405
	return call_int_hook(sb_umount, 0, mnt, flags);
406 407
}

Al Viro's avatar
Al Viro committed
408
int security_sb_pivotroot(const struct path *old_path, const struct path *new_path)
409
{
410
	return call_int_hook(sb_pivotroot, 0, old_path, new_path);
411 412
}

413
int security_sb_set_mnt_opts(struct super_block *sb,
414 415 416
				struct security_mnt_opts *opts,
				unsigned long kern_flags,
				unsigned long *set_kern_flags)
417
{
418 419 420
	return call_int_hook(sb_set_mnt_opts,
				opts->num_mnt_opts ? -EOPNOTSUPP : 0, sb,
				opts, kern_flags, set_kern_flags);
421
}
422
EXPORT_SYMBOL(security_sb_set_mnt_opts);
423

424
int security_sb_clone_mnt_opts(const struct super_block *oldsb,
425 426 427
				struct super_block *newsb,
				unsigned long kern_flags,
				unsigned long *set_kern_flags)
428
{
429 430
	return call_int_hook(sb_clone_mnt_opts, 0, oldsb, newsb,
				kern_flags, set_kern_flags);
431
}
432 433 434 435
EXPORT_SYMBOL(security_sb_clone_mnt_opts);

int security_sb_parse_opts_str(char *options, struct security_mnt_opts *opts)
{
436
	return call_int_hook(sb_parse_opts_str, 0, options, opts);
437 438
}
EXPORT_SYMBOL(security_sb_parse_opts_str);
439

440 441 442
int security_inode_alloc(struct inode *inode)
{
	inode->i_security = NULL;
443
	return call_int_hook(inode_alloc_security, 0, inode);
444 445 446 447
}

void security_inode_free(struct inode *inode)
{
448
	integrity_inode_free(inode);
449
	call_void_hook(inode_free_security, inode);
450 451
}

452
int security_dentry_init_security(struct dentry *dentry, int mode,
453
					const struct qstr *name, void **ctx,
454 455
					u32 *ctxlen)
{
456 457
	return call_int_hook(dentry_init_security, -EOPNOTSUPP, dentry, mode,
				name, ctx, ctxlen);
458 459 460
}
EXPORT_SYMBOL(security_dentry_init_security);

461 462 463 464 465 466 467 468 469
int security_dentry_create_files_as(struct dentry *dentry, int mode,
				    struct qstr *name,
				    const struct cred *old, struct cred *new)
{
	return call_int_hook(dentry_create_files_as, 0, dentry, mode,
				name, old, new);
}
EXPORT_SYMBOL(security_dentry_create_files_as);

470
int security_inode_init_security(struct inode *inode, struct inode *dir,
471 472
				 const struct qstr *qstr,
				 const initxattrs initxattrs, void *fs_data)
473
{
474 475
	struct xattr new_xattrs[MAX_LSM_EVM_XATTR + 1];
	struct xattr *lsm_xattr, *evm_xattr, *xattr;
476 477
	int ret;

478
	if (unlikely(IS_PRIVATE(inode)))
479
		return 0;
480 481

	if (!initxattrs)
482 483
		return call_int_hook(inode_init_security, -EOPNOTSUPP, inode,
				     dir, qstr, NULL, NULL, NULL);
484
	memset(new_xattrs, 0, sizeof(new_xattrs));
485
	lsm_xattr = new_xattrs;
486
	ret = call_int_hook(inode_init_security, -EOPNOTSUPP, inode, dir, qstr,
487 488 489 490 491
						&lsm_xattr->name,
						&lsm_xattr->value,
						&lsm_xattr->value_len);
	if (ret)
		goto out;
492 493 494 495 496

	evm_xattr = lsm_xattr + 1;
	ret = evm_inode_init_security(inode, lsm_xattr, evm_xattr);
	if (ret)
		goto out;
497 498
	ret = initxattrs(inode, new_xattrs, fs_data);
out:
499
	for (xattr = new_xattrs; xattr->value != NULL; xattr++)
500
		kfree(xattr->value);
501 502 503 504 505
	return (ret == -EOPNOTSUPP) ? 0 : ret;
}
EXPORT_SYMBOL(security_inode_init_security);

int security_old_inode_init_security(struct inode *inode, struct inode *dir,
506
				     const struct qstr *qstr, const char **name,
507
				     void **value, size_t *len)
508 509
{
	if (unlikely(IS_PRIVATE(inode)))
510
		return -EOPNOTSUPP;
511 512
	return call_int_hook(inode_init_security, -EOPNOTSUPP, inode, dir,
			     qstr, name, value, len);
513
}
514
EXPORT_SYMBOL(security_old_inode_init_security);
515

516
#ifdef CONFIG_SECURITY_PATH
517
int security_path_mknod(const struct path *dir, struct dentry *dentry, umode_t mode,
518 519
			unsigned int dev)
{
520
	if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
521
		return 0;
522
	return call_int_hook(path_mknod, 0, dir, dentry, mode, dev);
523 524 525
}
EXPORT_SYMBOL(security_path_mknod);

526
int security_path_mkdir(const struct path *dir, struct dentry *dentry, umode_t mode)
527
{
528
	if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
529
		return 0;
530
	return call_int_hook(path_mkdir, 0, dir, dentry, mode);
531
}
532
EXPORT_SYMBOL(security_path_mkdir);
533

534
int security_path_rmdir(const struct path *dir, struct dentry *dentry)
535
{
536
	if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
537
		return 0;
538
	return call_int_hook(path_rmdir, 0, dir, dentry);
539 540
}

541
int security_path_unlink(const struct path *dir, struct dentry *dentry)
542
{
543
	if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
544
		return 0;
545
	return call_int_hook(path_unlink, 0, dir, dentry);
546
}
547
EXPORT_SYMBOL(security_path_unlink);
548

549
int security_path_symlink(const struct path *dir, struct dentry *dentry,
550 551
			  const char *old_name)
{
552
	if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
553
		return 0;
554
	return call_int_hook(path_symlink, 0, dir, dentry, old_name);
555 556
}

557
int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
558 559
		       struct dentry *new_dentry)
{
560
	if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry))))
561
		return 0;
562
	return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
563 564
}

565 566
int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
			 const struct path *new_dir, struct dentry *new_dentry,
567
			 unsigned int flags)
568
{
569 570
	if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry)) ||
		     (d_is_positive(new_dentry) && IS_PRIVATE(d_backing_inode(new_dentry)))))
571
		return 0;
Miklos Szeredi's avatar
Miklos Szeredi committed
572 573

	if (flags & RENAME_EXCHANGE) {
574 575
		int err = call_int_hook(path_rename, 0, new_dir, new_dentry,
					old_dir, old_dentry);
Miklos Szeredi's avatar
Miklos Szeredi committed
576 577 578 579
		if (err)
			return err;
	}

580 581
	return call_int_hook(path_rename, 0, old_dir, old_dentry, new_dir,
				new_dentry);
582
}
583
EXPORT_SYMBOL(security_path_rename);
584

Al Viro's avatar
Al Viro committed
585
int security_path_truncate(const struct path *path)
586
{
587
	if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
588
		return 0;
589
	return call_int_hook(path_truncate, 0, path);
590
}
591

592
int security_path_chmod(const struct path *path, umode_t mode)
593
{
594
	if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
595
		return 0;
596
	return call_int_hook(path_chmod, 0, path, mode);
597 598
}

599
int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
600
{
601
	if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
602
		return 0;
603
	return call_int_hook(path_chown, 0, path, uid, gid);
604
}
605

Al Viro's avatar
Al Viro committed
606
int security_path_chroot(const struct path *path)
607
{
608
	return call_int_hook(path_chroot, 0, path);
609
}
610 611
#endif

Al Viro's avatar
Al Viro committed
612
int security_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
613 614 615
{
	if (unlikely(IS_PRIVATE(dir)))
		return 0;
616
	return call_int_hook(inode_create, 0, dir, dentry, mode);
617
}
618
EXPORT_SYMBOL_GPL(security_inode_create);
619 620 621 622

int security_inode_link(struct dentry *old_dentry, struct inode *dir,
			 struct dentry *new_dentry)
{
623
	if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry))))
624
		return 0;
625
	return call_int_hook(inode_link, 0, old_dentry, dir, new_dentry);
626 627 628 629
}

int security_inode_unlink(struct inode *dir, struct dentry *dentry)
{
630
	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
631
		return 0;
632
	return call_int_hook(inode_unlink, 0, dir, dentry);
633 634 635 636 637 638 639
}

int security_inode_symlink(struct inode *dir, struct dentry *dentry,
			    const char *old_name)
{
	if (unlikely(IS_PRIVATE(dir)))
		return 0;
640
	return call_int_hook(inode_symlink, 0, dir, dentry, old_name);
641 642
}

643
int security_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
644 645 646
{
	if (unlikely(IS_PRIVATE(dir)))
		return 0;
647
	return call_int_hook(inode_mkdir, 0, dir, dentry, mode);
648
}
649
EXPORT_SYMBOL_GPL(security_inode_mkdir);
650 651 652

int security_inode_rmdir(struct inode *dir, struct dentry *dentry)
{
653
	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
654
		return 0;
655
	return call_int_hook(inode_rmdir, 0, dir, dentry);
656 657
}

Al Viro's avatar
Al Viro committed
658
int security_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
659 660 661
{
	if (unlikely(IS_PRIVATE(dir)))
		return 0;
662
	return call_int_hook(inode_mknod, 0, dir, dentry, mode, dev);
663 664 665
}

int security_inode_rename(struct inode *old_dir, struct dentry *old_dentry,
666 667
			   struct inode *new_dir, struct dentry *new_dentry,
			   unsigned int flags)
668
{
669 670
        if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry)) ||
            (d_is_positive(new_dentry) && IS_PRIVATE(d_backing_inode(new_dentry)))))
671
		return 0;
Miklos Szeredi's avatar
Miklos Szeredi committed
672 673

	if (flags & RENAME_EXCHANGE) {
674
		int err = call_int_hook(inode_rename, 0, new_dir, new_dentry,
Miklos Szeredi's avatar
Miklos Szeredi committed
675 676 677 678 679
						     old_dir, old_dentry);
		if (err)
			return err;
	}

680
	return call_int_hook(inode_rename, 0, old_dir, old_dentry,
681 682 683 684 685
					   new_dir, new_dentry);
}

int security_inode_readlink(struct dentry *dentry)
{
686
	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
687
		return 0;
688
	return call_int_hook(inode_readlink, 0, dentry);
689 690
}

691 692
int security_inode_follow_link(struct dentry *dentry, struct inode *inode,
			       bool rcu)
693
{
694
	if (unlikely(IS_PRIVATE(inode)))
695
		return 0;
696
	return call_int_hook(inode_follow_link, 0, dentry, inode, rcu);
697 698
}

699
int security_inode_permission(struct inode *inode, int mask)
700 701 702
{
	if (unlikely(IS_PRIVATE(inode)))
		return 0;
703
	return call_int_hook(inode_permission, 0, inode, mask);
704 705 706 707
}

int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
{
708 709
	int ret;

710
	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
711
		return 0;
712
	ret = call_int_hook(inode_setattr, 0, dentry, attr);
713 714 715
	if (ret)
		return ret;
	return evm_inode_setattr(dentry, attr);
716
}
717
EXPORT_SYMBOL_GPL(security_inode_setattr);
718

719
int security_inode_getattr(const struct path *path)
720
{
721
	if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
722
		return 0;
723
	return call_int_hook(inode_getattr, 0, path);
724 725
}

726 727
int security_inode_setxattr(struct dentry *dentry, const char *name,
			    const void *value, size_t size, int flags)
728
{
729 730
	int ret;

731
	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
732
		return 0;
733 734 735 736 737
	/*
	 * SELinux and Smack integrate the cap call,
	 * so assume that all LSMs supplying this call do so.
	 */
	ret = call_int_hook(inode_setxattr, 1, dentry, name, value, size,
738
				flags);
739 740 741

	if (ret == 1)
		ret = cap_inode_setxattr(dentry, name, value, size, flags);
742 743 744
	if (ret)
		return ret;
	ret = ima_inode_setxattr(dentry, name, value, size);
745 746 747
	if (ret)
		return ret;
	return evm_inode_setxattr(dentry, name, value, size);
748 749
}

750 751
void security_inode_post_setxattr(struct dentry *dentry, const char *name,
				  const void *value, size_t size, int flags)
752
{
753
	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
754
		return;
755
	call_void_hook(inode_post_setxattr, dentry, name, value, size, flags);
756
	evm_inode_post_setxattr(dentry, name, value, size);
757 758
}

759
int security_inode_getxattr(struct dentry *dentry, const char *name)
760
{
761
	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
762
		return 0;
763
	return call_int_hook(inode_getxattr, 0, dentry, name);
764 765 766 767
}

int security_inode_listxattr(struct dentry *dentry)
{
768
	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
769
		return 0;
770
	return call_int_hook(inode_listxattr, 0, dentry);
771 772
}

773
int security_inode_removexattr(struct dentry *dentry, const char *name)
774
{
775 776
	int ret;

777
	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
778
		return 0;
779 780 781 782 783 784 785
	/*
	 * SELinux and Smack integrate the cap call,
	 * so assume that all LSMs supplying this call do so.
	 */
	ret = call_int_hook(inode_removexattr, 1, dentry, name);
	if (ret == 1)
		ret = cap_inode_removexattr(dentry, name);
786 787 788
	if (ret)
		return ret;
	ret = ima_inode_removexattr(dentry, name);
789 790 791
	if (ret)
		return ret;
	return evm_inode_removexattr(dentry, name);
792 793
}

794 795
int security_inode_need_killpriv(struct dentry *dentry)
{
796
	return call_int_hook(inode_need_killpriv, 0, dentry);
797 798 799 800
}

int security_inode_killpriv(struct dentry *dentry)
{
801
	return call_int_hook(inode_killpriv, 0, dentry);
802 803
}

804
int security_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
805
{
806 807 808
	struct security_hook_list *hp;
	int rc;

809
	if (unlikely(IS_PRIVATE(inode)))
810
		return -EOPNOTSUPP;
811 812 813
	/*
	 * Only one module will provide an attribute with a given name.
	 */
814
	hlist_for_each_entry(hp, &security_hook_heads.inode_getsecurity, list) {
815 816 817 818 819
		rc = hp->hook.inode_getsecurity(inode, name, buffer, alloc);
		if (rc != -EOPNOTSUPP)
			return rc;
	}
	return -EOPNOTSUPP;
820 821 822 823
}

int security_inode_setsecurity(struct inode *inode, const char *name, const void *value, size_t size, int flags)
{
824 825 826
	struct security_hook_list *hp;
	int rc;

827
	if (unlikely(IS_PRIVATE(inode)))
828
		return -EOPNOTSUPP;
829 830 831
	/*
	 * Only one module will provide an attribute with a given name.
	 */
832
	hlist_for_each_entry(hp, &security_hook_heads.inode_setsecurity, list) {
833 834 835 836 837 838
		rc = hp->hook.inode_setsecurity(inode, name, value, size,
								flags);
		if (rc != -EOPNOTSUPP)
			return rc;
	}
	return -EOPNOTSUPP;
839 840 841 842 843 844
}

int security_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
{
	if (unlikely(IS_PRIVATE(inode)))
		return 0;
845
	return call_int_hook(inode_listsecurity, 0, inode, buffer, buffer_size);
846
}
847
EXPORT_SYMBOL(security_inode_listsecurity);
848

849
void security_inode_getsecid(struct inode *inode, u32 *secid)
850
{
851
	call_void_hook(inode_getsecid, inode, secid);
852 853
}

854 855 856 857 858 859
int security_inode_copy_up(struct dentry *src, struct cred **new)
{
	return call_int_hook(inode_copy_up, 0, src, new);
}
EXPORT_SYMBOL(security_inode_copy_up);

860 861 862 863 864 865
int security_inode_copy_up_xattr(const char *name)
{
	return call_int_hook(inode_copy_up_xattr, -EOPNOTSUPP, name);
}
EXPORT_SYMBOL(security_inode_copy_up_xattr);

866 867
int security_file_permission(struct file *file, int mask)
{
868 869
	int ret;

870
	ret = call_int_hook(file_permission, 0, file, mask);
871 872 873 874
	if (ret)
		return ret;

	return fsnotify_perm(file, mask);
875 876 877 878
}

int security_file_alloc(struct file *file)
{
879
	return call_int_hook(file_alloc_security, 0, file);
880 881 882 883
}

void security_file_free(struct file *file)
{
884
	call_void_hook(file_free_security, file);
885 886 887 888
}

int security_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
889
	return call_int_hook(file_ioctl, 0, file, cmd, arg);
890 891
}

892
static inline unsigned long mmap_prot(struct file *file, unsigned long prot)
893
{
894
	/*
895 896
	 * Does we have PROT_READ and does the application expect
	 * it to imply PROT_EXEC?  If not, nothing to talk about...
897
	 */
898 899
	if ((prot & (PROT_READ | PROT_EXEC)) != PROT_READ)
		return prot;
900
	if (!(current->personality & READ_IMPLIES_EXEC))
901 902 903 904 905 906 907 908
		return prot;
	/*
	 * if that's an anonymous mapping, let it.
	 */
	if (!file)
		return prot | PROT_EXEC;
	/*
	 * ditto if it's not on noexec mount, except that on !MMU we need
909
	 * NOMMU_MAP_EXEC (== VM_MAYEXEC) in this case
910
	 */
911
	if (!path_noexec(&file->f_path)) {
912
#ifndef CONFIG_MMU
913 914 915 916 917
		if (file->f_op->mmap_capabilities) {
			unsigned caps = file->f_op->mmap_capabilities(file);
			if (!(caps & NOMMU_MAP_EXEC))
				return prot;
		}
918
#endif
919
		return prot | PROT_EXEC;
920
	}
921 922 923 924 925 926 927 928
	/* anything on noexec mount won't get PROT_EXEC */
	return prot;
}

int security_mmap_file(struct file *file, unsigned long prot,
			unsigned long flags)
{
	int ret;
929
	ret = call_int_hook(mmap_file, 0, file, prot,
930
					mmap_prot(file, prot), flags);
931 932 933
	if (ret)
		return ret;
	return ima_file_mmap(file, prot);
934 935
}

936 937
int security_mmap_addr(unsigned long addr)
{
938
	return call_int_hook(mmap_addr, 0, addr);
939