security.c 41.1 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * Security plug functions
 *
 * Copyright (C) 2001 WireX Communications, Inc <[email protected]>
 * Copyright (C) 2001-2002 Greg Kroah-Hartman <[email protected]>
 * Copyright (C) 2001 Networks Associates Technology, Inc <[email protected]>
 *
 *	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.
 */

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

30
#define MAX_LSM_EVM_XATTR	2
Linus Torvalds's avatar
Linus Torvalds committed
31

32 33 34
/* Maximum number of letters for an LSM name string */
#define SECURITY_NAME_MAX	10

35
struct security_hook_heads security_hook_heads __lsm_ro_after_init;
36
char *lsm_names;
37
/* Boot-time LSM user choice */
38 39
static __initdata char chosen_lsm[SECURITY_NAME_MAX + 1] =
	CONFIG_DEFAULT_SECURITY;
Linus Torvalds's avatar
Linus Torvalds committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

static void __init do_security_initcalls(void)
{
	initcall_t *call;
	call = __security_initcall_start;
	while (call < __security_initcall_end) {
		(*call) ();
		call++;
	}
}

/**
 * security_init - initializes the security framework
 *
 * This should be called early in the kernel initialization sequence.
 */
int __init security_init(void)
{
58 59 60 61 62 63
	int i;
	struct list_head *list = (struct list_head *) &security_hook_heads;

	for (i = 0; i < sizeof(security_hook_heads) / sizeof(struct list_head);
	     i++)
		INIT_LIST_HEAD(&list[i]);
64
	pr_info("Security Framework initialized\n");
Linus Torvalds's avatar
Linus Torvalds committed
65

66
	/*
67
	 * Load minor LSMs, with the capability module always first.
68 69 70
	 */
	capability_add_hooks();
	yama_add_hooks();
71
	loadpin_add_hooks();
72

73
	/*
74
	 * Load all the remaining security modules.
75
	 */
Linus Torvalds's avatar
Linus Torvalds committed
76 77 78 79 80
	do_security_initcalls();

	return 0;
}

81 82 83 84 85 86 87 88
/* Save user chosen LSM */
static int __init choose_lsm(char *str)
{
	strncpy(chosen_lsm, str, SECURITY_NAME_MAX);
	return 1;
}
__setup("security=", choose_lsm);

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
static int lsm_append(char *new, char **result)
{
	char *cp;

	if (*result == NULL) {
		*result = kstrdup(new, GFP_KERNEL);
	} else {
		cp = kasprintf(GFP_KERNEL, "%s,%s", *result, new);
		if (cp == NULL)
			return -ENOMEM;
		kfree(*result);
		*result = cp;
	}
	return 0;
}

105 106
/**
 * security_module_enable - Load given security module on boot ?
107
 * @module: the name of the module
108 109 110
 *
 * Each LSM must pass this method before registering its own operations
 * to avoid security registration races. This method may also be used
111
 * to check if your LSM is currently loaded during kernel initialization.
112 113 114
 *
 * Return true if:
 *	-The passed LSM is the one chosen by user at boot time,
115
 *	-or the passed LSM is configured as the default and the user did not
116
 *	 choose an alternate LSM at boot time.
117 118
 * Otherwise, return false.
 */
119
int __init security_module_enable(const char *module)
120
{
121
	return !strcmp(module, chosen_lsm);
122 123
}

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
/**
 * 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;
		list_add_tail_rcu(&hooks[i].list, hooks[i].head);
	}
	if (lsm_append(lsm, &lsm_names) < 0)
		panic("%s - Cannot get early memory.\n", __func__);
}

145
/*
146
 * Hook list operation macros.
Linus Torvalds's avatar
Linus Torvalds committed
147
 *
148 149
 * call_void_hook:
 *	This is a hook that does not return a value.
Linus Torvalds's avatar
Linus Torvalds committed
150
 *
151 152
 * call_int_hook:
 *	This is a hook that returns a value.
Linus Torvalds's avatar
Linus Torvalds committed
153 154
 */

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
#define call_void_hook(FUNC, ...)				\
	do {							\
		struct security_hook_list *P;			\
								\
		list_for_each_entry(P, &security_hook_heads.FUNC, list)	\
			P->hook.FUNC(__VA_ARGS__);		\
	} while (0)

#define call_int_hook(FUNC, IRC, ...) ({			\
	int RC = IRC;						\
	do {							\
		struct security_hook_list *P;			\
								\
		list_for_each_entry(P, &security_hook_heads.FUNC, list) { \
			RC = P->hook.FUNC(__VA_ARGS__);		\
			if (RC != 0)				\
				break;				\
		}						\
	} while (0);						\
	RC;							\
})
Linus Torvalds's avatar
Linus Torvalds committed
176

177 178
/* Security operations */

179 180
int security_binder_set_context_mgr(struct task_struct *mgr)
{
181
	return call_int_hook(binder_set_context_mgr, 0, mgr);
182 183 184 185 186
}

int security_binder_transaction(struct task_struct *from,
				struct task_struct *to)
{
187
	return call_int_hook(binder_transaction, 0, from, to);
188 189 190 191 192
}

int security_binder_transfer_binder(struct task_struct *from,
				    struct task_struct *to)
{
193
	return call_int_hook(binder_transfer_binder, 0, from, to);
194 195 196 197 198
}

int security_binder_transfer_file(struct task_struct *from,
				  struct task_struct *to, struct file *file)
{
199
	return call_int_hook(binder_transfer_file, 0, from, to, file);
200 201
}

202
int security_ptrace_access_check(struct task_struct *child, unsigned int mode)
203
{
204
	return call_int_hook(ptrace_access_check, 0, child, mode);
205 206 207 208
}

int security_ptrace_traceme(struct task_struct *parent)
{
209
	return call_int_hook(ptrace_traceme, 0, parent);
210 211 212 213 214 215 216
}

int security_capget(struct task_struct *target,
		     kernel_cap_t *effective,
		     kernel_cap_t *inheritable,
		     kernel_cap_t *permitted)
{
217 218
	return call_int_hook(capget, 0, target,
				effective, inheritable, permitted);
219 220
}

221 222 223 224
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)
225
{
226 227
	return call_int_hook(capset, 0, new, old,
				effective, inheritable, permitted);
228 229
}

230
int security_capable(const struct cred *cred, struct user_namespace *ns,
231
		     int cap)
232
{
233
	return call_int_hook(capable, 0, cred, ns, cap, SECURITY_CAP_AUDIT);
234 235
}

236 237
int security_capable_noaudit(const struct cred *cred, struct user_namespace *ns,
			     int cap)
238
{
239
	return call_int_hook(capable, 0, cred, ns, cap, SECURITY_CAP_NOAUDIT);
240 241 242 243
}

int security_quotactl(int cmds, int type, int id, struct super_block *sb)
{
244
	return call_int_hook(quotactl, 0, cmds, type, id, sb);
245 246 247 248
}

int security_quota_on(struct dentry *dentry)
{
249
	return call_int_hook(quota_on, 0, dentry);
250 251
}

252
int security_syslog(int type)
253
{
254
	return call_int_hook(syslog, 0, type);
255 256
}

257
int security_settime64(const struct timespec64 *ts, const struct timezone *tz)
258
{
259
	return call_int_hook(settime, 0, ts, tz);
260 261 262 263
}

int security_vm_enough_memory_mm(struct mm_struct *mm, long pages)
{
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
	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.
	 */
	list_for_each_entry(hp, &security_hook_heads.vm_enough_memory, list) {
		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);
283 284
}

285
int security_bprm_set_creds(struct linux_binprm *bprm)
286
{
287
	return call_int_hook(bprm_set_creds, 0, bprm);
288 289
}

290
int security_bprm_check(struct linux_binprm *bprm)
291
{
292 293
	int ret;

294
	ret = call_int_hook(bprm_check_security, 0, bprm);
295 296 297
	if (ret)
		return ret;
	return ima_bprm_check(bprm);
298 299
}

300
void security_bprm_committing_creds(struct linux_binprm *bprm)
301
{
302
	call_void_hook(bprm_committing_creds, bprm);
303 304
}

305
void security_bprm_committed_creds(struct linux_binprm *bprm)
306
{
307
	call_void_hook(bprm_committed_creds, bprm);
308 309 310 311
}

int security_bprm_secureexec(struct linux_binprm *bprm)
{
312
	return call_int_hook(bprm_secureexec, 0, bprm);
313 314 315 316
}

int security_sb_alloc(struct super_block *sb)
{
317
	return call_int_hook(sb_alloc_security, 0, sb);
318 319 320 321
}

void security_sb_free(struct super_block *sb)
{
322
	call_void_hook(sb_free_security, sb);
323 324
}

325
int security_sb_copy_data(char *orig, char *copy)
326
{
327
	return call_int_hook(sb_copy_data, 0, orig, copy);
328
}
329
EXPORT_SYMBOL(security_sb_copy_data);
330

331 332
int security_sb_remount(struct super_block *sb, void *data)
{
333
	return call_int_hook(sb_remount, 0, sb, data);
334 335
}

336
int security_sb_kern_mount(struct super_block *sb, int flags, void *data)
337
{
338
	return call_int_hook(sb_kern_mount, 0, sb, flags, data);
339 340
}

341 342
int security_sb_show_options(struct seq_file *m, struct super_block *sb)
{
343
	return call_int_hook(sb_show_options, 0, m, sb);
344 345
}

346 347
int security_sb_statfs(struct dentry *dentry)
{
348
	return call_int_hook(sb_statfs, 0, dentry);
349 350
}

Al Viro's avatar
Al Viro committed
351
int security_sb_mount(const char *dev_name, const struct path *path,
Al Viro's avatar
Al Viro committed
352
                       const char *type, unsigned long flags, void *data)
353
{
354
	return call_int_hook(sb_mount, 0, dev_name, path, type, flags, data);
355 356 357 358
}

int security_sb_umount(struct vfsmount *mnt, int flags)
{
359
	return call_int_hook(sb_umount, 0, mnt, flags);
360 361
}

Al Viro's avatar
Al Viro committed
362
int security_sb_pivotroot(const struct path *old_path, const struct path *new_path)
363
{
364
	return call_int_hook(sb_pivotroot, 0, old_path, new_path);
365 366
}

367
int security_sb_set_mnt_opts(struct super_block *sb,
368 369 370
				struct security_mnt_opts *opts,
				unsigned long kern_flags,
				unsigned long *set_kern_flags)
371
{
372 373 374
	return call_int_hook(sb_set_mnt_opts,
				opts->num_mnt_opts ? -EOPNOTSUPP : 0, sb,
				opts, kern_flags, set_kern_flags);
375
}
376
EXPORT_SYMBOL(security_sb_set_mnt_opts);
377

378
int security_sb_clone_mnt_opts(const struct super_block *oldsb,
379 380
				struct super_block *newsb)
{
381
	return call_int_hook(sb_clone_mnt_opts, 0, oldsb, newsb);
382
}
383 384 385 386
EXPORT_SYMBOL(security_sb_clone_mnt_opts);

int security_sb_parse_opts_str(char *options, struct security_mnt_opts *opts)
{
387
	return call_int_hook(sb_parse_opts_str, 0, options, opts);
388 389
}
EXPORT_SYMBOL(security_sb_parse_opts_str);
390

391 392 393
int security_inode_alloc(struct inode *inode)
{
	inode->i_security = NULL;
394
	return call_int_hook(inode_alloc_security, 0, inode);
395 396 397 398
}

void security_inode_free(struct inode *inode)
{
399
	integrity_inode_free(inode);
400
	call_void_hook(inode_free_security, inode);
401 402
}

403
int security_dentry_init_security(struct dentry *dentry, int mode,
404
					const struct qstr *name, void **ctx,
405 406
					u32 *ctxlen)
{
407 408
	return call_int_hook(dentry_init_security, -EOPNOTSUPP, dentry, mode,
				name, ctx, ctxlen);
409 410 411
}
EXPORT_SYMBOL(security_dentry_init_security);

412 413 414 415 416 417 418 419 420
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);

421
int security_inode_init_security(struct inode *inode, struct inode *dir,
422 423
				 const struct qstr *qstr,
				 const initxattrs initxattrs, void *fs_data)
424
{
425 426
	struct xattr new_xattrs[MAX_LSM_EVM_XATTR + 1];
	struct xattr *lsm_xattr, *evm_xattr, *xattr;
427 428
	int ret;

429
	if (unlikely(IS_PRIVATE(inode)))
430
		return 0;
431 432

	if (!initxattrs)
433 434
		return call_int_hook(inode_init_security, -EOPNOTSUPP, inode,
				     dir, qstr, NULL, NULL, NULL);
435
	memset(new_xattrs, 0, sizeof(new_xattrs));
436
	lsm_xattr = new_xattrs;
437
	ret = call_int_hook(inode_init_security, -EOPNOTSUPP, inode, dir, qstr,
438 439 440 441 442
						&lsm_xattr->name,
						&lsm_xattr->value,
						&lsm_xattr->value_len);
	if (ret)
		goto out;
443 444 445 446 447

	evm_xattr = lsm_xattr + 1;
	ret = evm_inode_init_security(inode, lsm_xattr, evm_xattr);
	if (ret)
		goto out;
448 449
	ret = initxattrs(inode, new_xattrs, fs_data);
out:
450
	for (xattr = new_xattrs; xattr->value != NULL; xattr++)
451
		kfree(xattr->value);
452 453 454 455 456
	return (ret == -EOPNOTSUPP) ? 0 : ret;
}
EXPORT_SYMBOL(security_inode_init_security);

int security_old_inode_init_security(struct inode *inode, struct inode *dir,
457
				     const struct qstr *qstr, const char **name,
458
				     void **value, size_t *len)
459 460
{
	if (unlikely(IS_PRIVATE(inode)))
461
		return -EOPNOTSUPP;
462 463
	return call_int_hook(inode_init_security, -EOPNOTSUPP, inode, dir,
			     qstr, name, value, len);
464
}
465
EXPORT_SYMBOL(security_old_inode_init_security);
466

467
#ifdef CONFIG_SECURITY_PATH
468
int security_path_mknod(const struct path *dir, struct dentry *dentry, umode_t mode,
469 470
			unsigned int dev)
{
471
	if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
472
		return 0;
473
	return call_int_hook(path_mknod, 0, dir, dentry, mode, dev);
474 475 476
}
EXPORT_SYMBOL(security_path_mknod);

477
int security_path_mkdir(const struct path *dir, struct dentry *dentry, umode_t mode)
478
{
479
	if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
480
		return 0;
481
	return call_int_hook(path_mkdir, 0, dir, dentry, mode);
482
}
483
EXPORT_SYMBOL(security_path_mkdir);
484

485
int security_path_rmdir(const struct path *dir, struct dentry *dentry)
486
{
487
	if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
488
		return 0;
489
	return call_int_hook(path_rmdir, 0, dir, dentry);
490 491
}

492
int security_path_unlink(const struct path *dir, struct dentry *dentry)
493
{
494
	if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
495
		return 0;
496
	return call_int_hook(path_unlink, 0, dir, dentry);
497
}
498
EXPORT_SYMBOL(security_path_unlink);
499

500
int security_path_symlink(const struct path *dir, struct dentry *dentry,
501 502
			  const char *old_name)
{
503
	if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry))))
504
		return 0;
505
	return call_int_hook(path_symlink, 0, dir, dentry, old_name);
506 507
}

508
int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
509 510
		       struct dentry *new_dentry)
{
511
	if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry))))
512
		return 0;
513
	return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
514 515
}

516 517
int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
			 const struct path *new_dir, struct dentry *new_dentry,
518
			 unsigned int flags)
519
{
520 521
	if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry)) ||
		     (d_is_positive(new_dentry) && IS_PRIVATE(d_backing_inode(new_dentry)))))
522
		return 0;
Miklos Szeredi's avatar
Miklos Szeredi committed
523 524

	if (flags & RENAME_EXCHANGE) {
525 526
		int err = call_int_hook(path_rename, 0, new_dir, new_dentry,
					old_dir, old_dentry);
Miklos Szeredi's avatar
Miklos Szeredi committed
527 528 529 530
		if (err)
			return err;
	}

531 532
	return call_int_hook(path_rename, 0, old_dir, old_dentry, new_dir,
				new_dentry);
533
}
534
EXPORT_SYMBOL(security_path_rename);
535

Al Viro's avatar
Al Viro committed
536
int security_path_truncate(const struct path *path)
537
{
538
	if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
539
		return 0;
540
	return call_int_hook(path_truncate, 0, path);
541
}
542

543
int security_path_chmod(const struct path *path, umode_t mode)
544
{
545
	if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
546
		return 0;
547
	return call_int_hook(path_chmod, 0, path, mode);
548 549
}

550
int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
551
{
552
	if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
553
		return 0;
554
	return call_int_hook(path_chown, 0, path, uid, gid);
555
}
556

Al Viro's avatar
Al Viro committed
557
int security_path_chroot(const struct path *path)
558
{
559
	return call_int_hook(path_chroot, 0, path);
560
}
561 562
#endif

Al Viro's avatar
Al Viro committed
563
int security_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
564 565 566
{
	if (unlikely(IS_PRIVATE(dir)))
		return 0;
567
	return call_int_hook(inode_create, 0, dir, dentry, mode);
568
}
569
EXPORT_SYMBOL_GPL(security_inode_create);
570 571 572 573

int security_inode_link(struct dentry *old_dentry, struct inode *dir,
			 struct dentry *new_dentry)
{
574
	if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry))))
575
		return 0;
576
	return call_int_hook(inode_link, 0, old_dentry, dir, new_dentry);
577 578 579 580
}

int security_inode_unlink(struct inode *dir, struct dentry *dentry)
{
581
	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
582
		return 0;
583
	return call_int_hook(inode_unlink, 0, dir, dentry);
584 585 586 587 588 589 590
}

int security_inode_symlink(struct inode *dir, struct dentry *dentry,
			    const char *old_name)
{
	if (unlikely(IS_PRIVATE(dir)))
		return 0;
591
	return call_int_hook(inode_symlink, 0, dir, dentry, old_name);
592 593
}

594
int security_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
595 596 597
{
	if (unlikely(IS_PRIVATE(dir)))
		return 0;
598
	return call_int_hook(inode_mkdir, 0, dir, dentry, mode);
599
}
600
EXPORT_SYMBOL_GPL(security_inode_mkdir);
601 602 603

int security_inode_rmdir(struct inode *dir, struct dentry *dentry)
{
604
	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
605
		return 0;
606
	return call_int_hook(inode_rmdir, 0, dir, dentry);
607 608
}

Al Viro's avatar
Al Viro committed
609
int security_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
610 611 612
{
	if (unlikely(IS_PRIVATE(dir)))
		return 0;
613
	return call_int_hook(inode_mknod, 0, dir, dentry, mode, dev);
614 615 616
}

int security_inode_rename(struct inode *old_dir, struct dentry *old_dentry,
617 618
			   struct inode *new_dir, struct dentry *new_dentry,
			   unsigned int flags)
619
{
620 621
        if (unlikely(IS_PRIVATE(d_backing_inode(old_dentry)) ||
            (d_is_positive(new_dentry) && IS_PRIVATE(d_backing_inode(new_dentry)))))
622
		return 0;
Miklos Szeredi's avatar
Miklos Szeredi committed
623 624

	if (flags & RENAME_EXCHANGE) {
625
		int err = call_int_hook(inode_rename, 0, new_dir, new_dentry,
Miklos Szeredi's avatar
Miklos Szeredi committed
626 627 628 629 630
						     old_dir, old_dentry);
		if (err)
			return err;
	}

631
	return call_int_hook(inode_rename, 0, old_dir, old_dentry,
632 633 634 635 636
					   new_dir, new_dentry);
}

int security_inode_readlink(struct dentry *dentry)
{
637
	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
638
		return 0;
639
	return call_int_hook(inode_readlink, 0, dentry);
640 641
}

642 643
int security_inode_follow_link(struct dentry *dentry, struct inode *inode,
			       bool rcu)
644
{
645
	if (unlikely(IS_PRIVATE(inode)))
646
		return 0;
647
	return call_int_hook(inode_follow_link, 0, dentry, inode, rcu);
648 649
}

650
int security_inode_permission(struct inode *inode, int mask)
651 652 653
{
	if (unlikely(IS_PRIVATE(inode)))
		return 0;
654
	return call_int_hook(inode_permission, 0, inode, mask);
655 656 657 658
}

int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
{
659 660
	int ret;

661
	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
662
		return 0;
663
	ret = call_int_hook(inode_setattr, 0, dentry, attr);
664 665 666
	if (ret)
		return ret;
	return evm_inode_setattr(dentry, attr);
667
}
668
EXPORT_SYMBOL_GPL(security_inode_setattr);
669

670
int security_inode_getattr(const struct path *path)
671
{
672
	if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry))))
673
		return 0;
674
	return call_int_hook(inode_getattr, 0, path);
675 676
}

677 678
int security_inode_setxattr(struct dentry *dentry, const char *name,
			    const void *value, size_t size, int flags)
679
{
680 681
	int ret;

682
	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
683
		return 0;
684 685 686 687 688
	/*
	 * 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,
689
				flags);
690 691 692

	if (ret == 1)
		ret = cap_inode_setxattr(dentry, name, value, size, flags);
693 694 695
	if (ret)
		return ret;
	ret = ima_inode_setxattr(dentry, name, value, size);
696 697 698
	if (ret)
		return ret;
	return evm_inode_setxattr(dentry, name, value, size);
699 700
}

701 702
void security_inode_post_setxattr(struct dentry *dentry, const char *name,
				  const void *value, size_t size, int flags)
703
{
704
	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
705
		return;
706
	call_void_hook(inode_post_setxattr, dentry, name, value, size, flags);
707
	evm_inode_post_setxattr(dentry, name, value, size);
708 709
}

710
int security_inode_getxattr(struct dentry *dentry, const char *name)
711
{
712
	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
713
		return 0;
714
	return call_int_hook(inode_getxattr, 0, dentry, name);
715 716 717 718
}

int security_inode_listxattr(struct dentry *dentry)
{
719
	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
720
		return 0;
721
	return call_int_hook(inode_listxattr, 0, dentry);
722 723
}

724
int security_inode_removexattr(struct dentry *dentry, const char *name)
725
{
726 727
	int ret;

728
	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
729
		return 0;
730 731 732 733 734 735 736
	/*
	 * 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);
737 738 739
	if (ret)
		return ret;
	ret = ima_inode_removexattr(dentry, name);
740 741 742
	if (ret)
		return ret;
	return evm_inode_removexattr(dentry, name);
743 744
}

745 746
int security_inode_need_killpriv(struct dentry *dentry)
{
747
	return call_int_hook(inode_need_killpriv, 0, dentry);
748 749 750 751
}

int security_inode_killpriv(struct dentry *dentry)
{
752
	return call_int_hook(inode_killpriv, 0, dentry);
753 754
}

755
int security_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
756
{
757 758 759
	struct security_hook_list *hp;
	int rc;

760
	if (unlikely(IS_PRIVATE(inode)))
761
		return -EOPNOTSUPP;
762 763 764 765 766 767 768 769 770
	/*
	 * Only one module will provide an attribute with a given name.
	 */
	list_for_each_entry(hp, &security_hook_heads.inode_getsecurity, list) {
		rc = hp->hook.inode_getsecurity(inode, name, buffer, alloc);
		if (rc != -EOPNOTSUPP)
			return rc;
	}
	return -EOPNOTSUPP;
771 772 773 774
}

int security_inode_setsecurity(struct inode *inode, const char *name, const void *value, size_t size, int flags)
{
775 776 777
	struct security_hook_list *hp;
	int rc;

778
	if (unlikely(IS_PRIVATE(inode)))
779
		return -EOPNOTSUPP;
780 781 782 783 784 785 786 787 788 789
	/*
	 * Only one module will provide an attribute with a given name.
	 */
	list_for_each_entry(hp, &security_hook_heads.inode_setsecurity, list) {
		rc = hp->hook.inode_setsecurity(inode, name, value, size,
								flags);
		if (rc != -EOPNOTSUPP)
			return rc;
	}
	return -EOPNOTSUPP;
790 791 792 793 794 795
}

int security_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
{
	if (unlikely(IS_PRIVATE(inode)))
		return 0;
796
	return call_int_hook(inode_listsecurity, 0, inode, buffer, buffer_size);
797
}
798
EXPORT_SYMBOL(security_inode_listsecurity);
799

800
void security_inode_getsecid(struct inode *inode, u32 *secid)
801
{
802
	call_void_hook(inode_getsecid, inode, secid);
803 804
}

805 806 807 808 809 810
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);

811 812 813 814 815 816
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);

817 818
int security_file_permission(struct file *file, int mask)
{
819 820
	int ret;

821
	ret = call_int_hook(file_permission, 0, file, mask);
822 823 824 825
	if (ret)
		return ret;

	return fsnotify_perm(file, mask);
826 827 828 829
}

int security_file_alloc(struct file *file)
{
830
	return call_int_hook(file_alloc_security, 0, file);
831 832 833 834
}

void security_file_free(struct file *file)
{
835
	call_void_hook(file_free_security, file);
836 837 838 839
}

int security_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
840
	return call_int_hook(file_ioctl, 0, file, cmd, arg);
841 842
}

843
static inline unsigned long mmap_prot(struct file *file, unsigned long prot)
844
{
845
	/*
846 847
	 * Does we have PROT_READ and does the application expect
	 * it to imply PROT_EXEC?  If not, nothing to talk about...
848
	 */
849 850
	if ((prot & (PROT_READ | PROT_EXEC)) != PROT_READ)
		return prot;
851
	if (!(current->personality & READ_IMPLIES_EXEC))
852 853 854 855 856 857 858 859
		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
860
	 * NOMMU_MAP_EXEC (== VM_MAYEXEC) in this case
861
	 */
862
	if (!path_noexec(&file->f_path)) {
863
#ifndef CONFIG_MMU
864 865 866 867 868
		if (file->f_op->mmap_capabilities) {
			unsigned caps = file->f_op->mmap_capabilities(file);
			if (!(caps & NOMMU_MAP_EXEC))
				return prot;
		}
869
#endif
870
		return prot | PROT_EXEC;
871
	}
872 873 874 875 876 877 878 879
	/* 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;
880
	ret = call_int_hook(mmap_file, 0, file, prot,
881
					mmap_prot(file, prot), flags);
882 883 884
	if (ret)
		return ret;
	return ima_file_mmap(file, prot);
885 886
}

887 888
int security_mmap_addr(unsigned long addr)
{
889
	return call_int_hook(mmap_addr, 0, addr);
890 891
}

892 893 894
int security_file_mprotect(struct vm_area_struct *vma, unsigned long reqprot,
			    unsigned long prot)
{
895
	return call_int_hook(file_mprotect, 0, vma, reqprot, prot);
896 897 898 899
}

int security_file_lock(struct file *file, unsigned int cmd)
{
900
	return call_int_hook(file_lock, 0, file, cmd);
901 902 903 904
}

int security_file_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
{
905
	return call_int_hook(file_fcntl, 0, file, cmd, arg);
906 907
}