security.c 55.5 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
char *lsm_names;
36
/* Boot-time LSM user choice */
37 38
static __initdata char chosen_lsm[SECURITY_NAME_MAX + 1] =
	CONFIG_DEFAULT_SECURITY;
Linus Torvalds's avatar
Linus Torvalds committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

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)
{
57
	pr_info("Security Framework initialized\n");
Linus Torvalds's avatar
Linus Torvalds committed
58

59
	/*
60
	 * Load minor LSMs, with the capability module always first.
61 62 63
	 */
	capability_add_hooks();
	yama_add_hooks();
64
	loadpin_add_hooks();
65

66
	/*
67
	 * Load all the remaining security modules.
68
	 */
Linus Torvalds's avatar
Linus Torvalds committed
69 70 71 72 73
	do_security_initcalls();

	return 0;
}

74 75 76 77 78 79 80 81
/* Save user chosen LSM */
static int __init choose_lsm(char *str)
{
	strncpy(chosen_lsm, str, SECURITY_NAME_MAX);
	return 1;
}
__setup("security=", choose_lsm);

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
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;
}

98 99
/**
 * security_module_enable - Load given security module on boot ?
100
 * @module: the name of the module
101 102 103
 *
 * Each LSM must pass this method before registering its own operations
 * to avoid security registration races. This method may also be used
104
 * to check if your LSM is currently loaded during kernel initialization.
105
 *
106 107 108 109 110 111 112 113
 * 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.
 *
114 115
 * Otherwise, return false.
 */
116
int __init security_module_enable(const char *module)
117
{
118
	return !strcmp(module, chosen_lsm);
119 120
}

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
/**
 * 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__);
}

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

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
#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
173

174 175
/* Security operations */

176 177
int security_binder_set_context_mgr(struct task_struct *mgr)
{
178
	return call_int_hook(binder_set_context_mgr, 0, mgr);
179 180 181 182 183
}

int security_binder_transaction(struct task_struct *from,
				struct task_struct *to)
{
184
	return call_int_hook(binder_transaction, 0, from, to);
185 186 187 188 189
}

int security_binder_transfer_binder(struct task_struct *from,
				    struct task_struct *to)
{
190
	return call_int_hook(binder_transfer_binder, 0, from, to);
191 192 193 194 195
}

int security_binder_transfer_file(struct task_struct *from,
				  struct task_struct *to, struct file *file)
{
196
	return call_int_hook(binder_transfer_file, 0, from, to, file);
197 198
}

199
int security_ptrace_access_check(struct task_struct *child, unsigned int mode)
200
{
201
	return call_int_hook(ptrace_access_check, 0, child, mode);
202 203 204 205
}

int security_ptrace_traceme(struct task_struct *parent)
{
206
	return call_int_hook(ptrace_traceme, 0, parent);
207 208 209 210 211 212 213
}

int security_capget(struct task_struct *target,
		     kernel_cap_t *effective,
		     kernel_cap_t *inheritable,
		     kernel_cap_t *permitted)
{
214 215
	return call_int_hook(capget, 0, target,
				effective, inheritable, permitted);
216 217
}

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

227
int security_capable(const struct cred *cred, struct user_namespace *ns,
228
		     int cap)
229
{
230
	return call_int_hook(capable, 0, cred, ns, cap, SECURITY_CAP_AUDIT);
231 232
}

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

int security_quotactl(int cmds, int type, int id, struct super_block *sb)
{
241
	return call_int_hook(quotactl, 0, cmds, type, id, sb);
242 243 244 245
}

int security_quota_on(struct dentry *dentry)
{
246
	return call_int_hook(quota_on, 0, dentry);
247 248
}

249
int security_syslog(int type)
250
{
251
	return call_int_hook(syslog, 0, type);
252 253
}

254
int security_settime64(const struct timespec64 *ts, const struct timezone *tz)
255
{
256
	return call_int_hook(settime, 0, ts, tz);
257 258 259 260
}

int security_vm_enough_memory_mm(struct mm_struct *mm, long pages)
{
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
	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);
280 281
}

282
int security_bprm_set_creds(struct linux_binprm *bprm)
283
{
284
	return call_int_hook(bprm_set_creds, 0, bprm);
285 286
}

287
int security_bprm_check(struct linux_binprm *bprm)
288
{
289 290
	int ret;

291
	ret = call_int_hook(bprm_check_security, 0, bprm);
292 293 294
	if (ret)
		return ret;
	return ima_bprm_check(bprm);
295 296
}

297
void security_bprm_committing_creds(struct linux_binprm *bprm)
298
{
299
	call_void_hook(bprm_committing_creds, bprm);
300 301
}

302
void security_bprm_committed_creds(struct linux_binprm *bprm)
303
{
304
	call_void_hook(bprm_committed_creds, bprm);
305 306 307 308
}

int security_bprm_secureexec(struct linux_binprm *bprm)
{
309
	return call_int_hook(bprm_secureexec, 0, bprm);
310 311 312 313
}

int security_sb_alloc(struct super_block *sb)
{
314
	return call_int_hook(sb_alloc_security, 0, sb);
315 316 317 318
}

void security_sb_free(struct super_block *sb)
{
319
	call_void_hook(sb_free_security, sb);
320 321
}

322
int security_sb_copy_data(char *orig, char *copy)
323
{
324
	return call_int_hook(sb_copy_data, 0, orig, copy);
325
}
326
EXPORT_SYMBOL(security_sb_copy_data);
327

328 329
int security_sb_remount(struct super_block *sb, void *data)
{
330
	return call_int_hook(sb_remount, 0, sb, data);
331 332
}

333
int security_sb_kern_mount(struct super_block *sb, int flags, void *data)
334
{
335
	return call_int_hook(sb_kern_mount, 0, sb, flags, data);
336 337
}

338 339
int security_sb_show_options(struct seq_file *m, struct super_block *sb)
{
340
	return call_int_hook(sb_show_options, 0, m, sb);
341 342
}

343 344
int security_sb_statfs(struct dentry *dentry)
{
345
	return call_int_hook(sb_statfs, 0, dentry);
346 347
}

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

int security_sb_umount(struct vfsmount *mnt, int flags)
{
356
	return call_int_hook(sb_umount, 0, mnt, flags);
357 358
}

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

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

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

int security_sb_parse_opts_str(char *options, struct security_mnt_opts *opts)
{
384
	return call_int_hook(sb_parse_opts_str, 0, options, opts);
385 386
}
EXPORT_SYMBOL(security_sb_parse_opts_str);
387

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

void security_inode_free(struct inode *inode)
{
396
	integrity_inode_free(inode);
397
	call_void_hook(inode_free_security, inode);
398 399
}

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

409 410 411 412 413 414 415 416 417
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);

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

426
	if (unlikely(IS_PRIVATE(inode)))
427
		return 0;
428 429

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

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

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

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

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

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

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

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

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

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

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

528 529
	return call_int_hook(path_rename, 0, old_dir, old_dentry, new_dir,
				new_dentry);
530
}
531
EXPORT_SYMBOL(security_path_rename);
532

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

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

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

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

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

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

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

int security_inode_symlink(struct inode *dir, struct dentry *dentry,
			    const char *old_name)
{
	if (unlikely(IS_PRIVATE(dir)))
		return 0;
588
	return call_int_hook(inode_symlink, 0, dir, dentry, old_name);
589 590
}

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

int security_inode_rmdir(struct inode *dir, struct dentry *dentry)
{
601
	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
602
		return 0;
603
	return call_int_hook(inode_rmdir, 0, dir, dentry);
604 605
}

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

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

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

628
	return call_int_hook(inode_rename, 0, old_dir, old_dentry,
629 630 631 632 633
					   new_dir, new_dentry);
}

int security_inode_readlink(struct dentry *dentry)
{
634
	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
635
		return 0;
636
	return call_int_hook(inode_readlink, 0, dentry);
637 638
}

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

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

int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
{
656 657
	int ret;

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

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

674 675
int security_inode_setxattr(struct dentry *dentry, const char *name,
			    const void *value, size_t size, int flags)
676
{
677 678
	int ret;

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

	if (ret == 1)
		ret = cap_inode_setxattr(dentry, name, value, size, flags);
690 691 692
	if (ret)
		return ret;
	ret = ima_inode_setxattr(dentry, name, value, size);
693 694 695
	if (ret)
		return ret;
	return evm_inode_setxattr(dentry, name, value, size);
696 697
}

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

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

int security_inode_listxattr(struct dentry *dentry)
{
716
	if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
717
		return 0;
718
	return call_int_hook(inode_listxattr, 0, dentry);
719 720
}

721
int security_inode_removexattr(struct dentry *dentry, const char *name)
722
{
723 724
	int ret;

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

742 743
int security_inode_need_killpriv(struct dentry *dentry)
{
744
	return call_int_hook(inode_need_killpriv, 0, dentry);
745 746 747 748
}

int security_inode_killpriv(struct dentry *dentry)
{
749
	return call_int_hook(inode_killpriv, 0, dentry);
750 751
}

752
int security_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
753
{
754 755 756
	struct security_hook_list *hp;
	int rc;

757
	if (unlikely(IS_PRIVATE(inode)))
758
		return -EOPNOTSUPP;
759 760 761 762 763 764 765 766 767
	/*
	 * 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;
768 769 770 771
}

int security_inode_setsecurity(struct inode *inode, const char *name, const void *value, size_t size, int flags)
{
772 773 774
	struct security_hook_list *hp;
	int rc;

775
	if (unlikely(IS_PRIVATE(inode)))
776
		return -EOPNOTSUPP;
777 778 779 780 781 782 783 784 785 786
	/*
	 * 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;
787 788 789 790 791 792
}

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

797
void security_inode_getsecid(struct inode *inode, u32 *secid)
798
{
799
	call_void_hook(inode_getsecid, inode, secid);
800 801
}

802 803 804 805 806 807
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);

808 809 810 811 812 813
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);

814 815
int security_file_permission(struct file *file, int mask)
{
816 817
	int ret;

818
	ret = call_int_hook(file_permission, 0, file, mask);
819 820 821 822
	if (ret)
		return ret;

	return fsnotify_perm(file, mask);
823 824 825 826
}

int security_file_alloc(struct file *file)
{
827
	return call_int_hook(file_alloc_security, 0, file);
828 829 830 831
}

void security_file_free(struct file *file)
{
832
	call_void_hook(file_free_security, file);
833 834 835 836
}

int security_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
837
	return call_int_hook(file_ioctl, 0, file, cmd, arg);
838 839
}

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

884 885
int security_mmap_addr(unsigned long addr)
{
886
	return call_int_hook(mmap_addr, 0, addr);
887 888
}

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

int security_file_lock(struct file *file, unsigned int cmd)
{
897
	return call_int_hook(file_lock, 0, file, cmd);
898 899 900 901
}

int security_file_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
{
902
	return call_int_hook(file_fcntl, 0, file, cmd, arg);
903 904
}

905
void security_file_set_fowner(struct file *file)
906
{
907
	call_void_hook(file_set_fowner, file);
908 909 910 911 912
}