ima_main.c 35 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2
/*
3 4
 * Integrity Measurement Architecture
 *
5 6 7 8 9 10 11 12 13
 * Copyright (C) 2005,2006,2007,2008 IBM Corporation
 *
 * Authors:
 * Reiner Sailer <sailer@watson.ibm.com>
 * Serge Hallyn <serue@us.ibm.com>
 * Kylene Hall <kylene@us.ibm.com>
 * Mimi Zohar <zohar@us.ibm.com>
 *
 * File: ima_main.c
14
 *	implements the IMA hooks: ima_bprm_check, ima_file_mmap,
15
 *	and ima_file_check.
16
 */
17

18
#include <linux/module.h>
19 20
#include <linux/file.h>
#include <linux/binfmts.h>
21
#include <linux/kernel_read_file.h>
22 23
#include <linux/mount.h>
#include <linux/mman.h>
24
#include <linux/slab.h>
25
#include <linux/xattr.h>
26
#include <linux/ima.h>
27
#include <linux/fs.h>
28
#include <linux/iversion.h>
29
#include <linux/evm.h>
30 31 32

#include "ima.h"

33 34 35 36 37 38
#ifdef CONFIG_IMA_APPRAISE
int ima_appraise = IMA_APPRAISE_ENFORCE;
#else
int ima_appraise;
#endif

39
int __ro_after_init ima_hash_algo = HASH_ALGO_SHA1;
40
static int hash_setup_done;
41

42 43 44 45
static struct notifier_block ima_lsm_policy_notifier = {
	.notifier_call = ima_lsm_policy_change,
};

46 47
static int __init hash_setup(char *str)
{
48 49 50 51 52 53 54
	struct ima_template_desc *template_desc = ima_template_desc_current();
	int i;

	if (hash_setup_done)
		return 1;

	if (strcmp(template_desc->name, IMA_TEMPLATE_IMA_NAME) == 0) {
55
		if (strncmp(str, "sha1", 4) == 0) {
56
			ima_hash_algo = HASH_ALGO_SHA1;
57
		} else if (strncmp(str, "md5", 3) == 0) {
58
			ima_hash_algo = HASH_ALGO_MD5;
59 60 61
		} else {
			pr_err("invalid hash algorithm \"%s\" for template \"%s\"",
				str, IMA_TEMPLATE_IMA_NAME);
62
			return 1;
63
		}
64 65 66
		goto out;
	}

67
	i = match_string(hash_algo_name, HASH_ALGO__LAST, str);
68 69
	if (i < 0) {
		pr_err("invalid hash algorithm \"%s\"", str);
70
		return 1;
71
	}
72 73

	ima_hash_algo = i;
74 75
out:
	hash_setup_done = 1;
76 77 78 79
	return 1;
}
__setup("ima_hash=", hash_setup);

80 81 82 83 84
enum hash_algo ima_get_current_hash_algo(void)
{
	return ima_hash_algo;
}

85 86 87 88 89 90 91 92
/* Prevent mmap'ing a file execute that is already mmap'ed write */
static int mmap_violation_check(enum ima_hooks func, struct file *file,
				char **pathbuf, const char **pathname,
				char *filename)
{
	struct inode *inode;
	int rc = 0;

93 94
	if ((func == MMAP_CHECK || func == MMAP_CHECK_REQPROT) &&
	    mapping_writably_mapped(file->f_mapping)) {
95 96 97 98 99 100 101 102 103 104 105 106
		rc = -ETXTBSY;
		inode = file_inode(file);

		if (!*pathbuf)	/* ima_rdwr_violation possibly pre-fetched */
			*pathname = ima_d_path(&file->f_path, pathbuf,
					       filename);
		integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, *pathname,
				    "mmap_file", "mmapped_writers", rc, 0);
	}
	return rc;
}

Mimi Zohar's avatar
Mimi Zohar committed
107
/*
108
 * ima_rdwr_violation_check
Mimi Zohar's avatar
Mimi Zohar committed
109
 *
110
 * Only invalidate the PCR for measured files:
111
 *	- Opening a file for write when already open for read,
Mimi Zohar's avatar
Mimi Zohar committed
112 113
 *	  results in a time of measure, time of use (ToMToU) error.
 *	- Opening a file for read when already open for write,
114
 *	  could result in a file measurement error.
Mimi Zohar's avatar
Mimi Zohar committed
115 116
 *
 */
117
static void ima_rdwr_violation_check(struct file *file,
118
				     struct ima_iint_cache *iint,
119
				     int must_measure,
120
				     char **pathbuf,
121 122
				     const char **pathname,
				     char *filename)
Mimi Zohar's avatar
Mimi Zohar committed
123
{
124
	struct inode *inode = file_inode(file);
Mimi Zohar's avatar
Mimi Zohar committed
125
	fmode_t mode = file->f_mode;
126
	bool send_tomtou = false, send_writers = false;
127

Mimi Zohar's avatar
Mimi Zohar committed
128
	if (mode & FMODE_WRITE) {
129
		if (atomic_read(&inode->i_readcount) && IS_IMA(inode)) {
130
			if (!iint)
131
				iint = ima_iint_find(inode);
132
			/* IMA_MEASURE is set from reader side */
133 134
			if (iint && test_bit(IMA_MUST_MEASURE,
						&iint->atomic_flags))
135 136
				send_tomtou = true;
		}
137
	} else {
138 139
		if (must_measure)
			set_bit(IMA_MUST_MEASURE, &iint->atomic_flags);
140
		if (inode_is_open_for_write(inode) && must_measure)
141
			send_writers = true;
Mimi Zohar's avatar
Mimi Zohar committed
142
	}
143

144 145 146
	if (!send_tomtou && !send_writers)
		return;

147
	*pathname = ima_d_path(&file->f_path, pathbuf, filename);
148

149
	if (send_tomtou)
150 151
		ima_add_violation(file, *pathname, iint,
				  "invalid_pcr", "ToMToU");
152
	if (send_writers)
153
		ima_add_violation(file, *pathname, iint,
154
				  "invalid_pcr", "open_writers");
Mimi Zohar's avatar
Mimi Zohar committed
155 156
}

157
static void ima_check_last_writer(struct ima_iint_cache *iint,
158
				  struct inode *inode, struct file *file)
159
{
160
	fmode_t mode = file->f_mode;
161
	bool update;
162

163 164 165
	if (!(mode & FMODE_WRITE))
		return;

166
	mutex_lock(&iint->mutex);
167
	if (atomic_read(&inode->i_writecount) == 1) {
168 169
		struct kstat stat;

170 171
		update = test_and_clear_bit(IMA_UPDATE_XATTR,
					    &iint->atomic_flags);
172 173 174 175 176
		if ((iint->flags & IMA_NEW_FILE) ||
		    vfs_getattr_nosec(&file->f_path, &stat,
				      STATX_CHANGE_COOKIE,
				      AT_STATX_SYNC_AS_STAT) ||
		    !(stat.result_mask & STATX_CHANGE_COOKIE) ||
177
		    stat.change_cookie != iint->real_inode.version) {
178
			iint->flags &= ~(IMA_DONE_MASK | IMA_NEW_FILE);
179
			iint->measured_pcrs = 0;
180
			if (update)
181 182
				ima_update_xattr(iint, file);
		}
183
	}
184
	mutex_unlock(&iint->mutex);
185 186
}

187 188 189 190
/**
 * ima_file_free - called on __fput()
 * @file: pointer to file structure being freed
 *
191
 * Flag files that changed, based on i_version
192
 */
193
static void ima_file_free(struct file *file)
194
{
Al Viro's avatar
Al Viro committed
195
	struct inode *inode = file_inode(file);
196
	struct ima_iint_cache *iint;
197

198
	if (!ima_policy_flag || !S_ISREG(inode->i_mode))
199
		return;
200

201
	iint = ima_iint_find(inode);
202 203
	if (!iint)
		return;
204

205
	ima_check_last_writer(iint, inode, file);
206 207
}

208 209
static int process_measurement(struct file *file, const struct cred *cred,
			       u32 secid, char *buf, loff_t size, int mask,
210
			       enum ima_hooks func)
211
{
212
	struct inode *real_inode, *inode = file_inode(file);
213
	struct ima_iint_cache *iint = NULL;
214
	struct ima_template_desc *template_desc = NULL;
215
	struct inode *metadata_inode;
216
	char *pathbuf = NULL;
217
	char filename[NAME_MAX];
218
	const char *pathname = NULL;
219
	int rc = 0, action, must_appraise = 0;
220
	int pcr = CONFIG_IMA_MEASURE_PCR_IDX;
221
	struct evm_ima_xattr_data *xattr_value = NULL;
222
	struct modsig *modsig = NULL;
223
	int xattr_len = 0;
224
	bool violation_check;
225
	enum hash_algo hash_algo;
226
	unsigned int allowed_algos = 0;
227

228
	if (!ima_policy_flag || !S_ISREG(inode->i_mode))
229
		return 0;
230

231 232 233 234
	/* Return an IMA_MEASURE, IMA_APPRAISE, IMA_AUDIT action
	 * bitmask based on the appraise/audit/measurement policy.
	 * Included is the appraise submask.
	 */
235
	action = ima_get_action(file_mnt_idmap(file), inode, cred, secid,
236 237
				mask, func, &pcr, &template_desc, NULL,
				&allowed_algos);
238 239
	violation_check = ((func == FILE_CHECK || func == MMAP_CHECK ||
			    func == MMAP_CHECK_REQPROT) &&
240 241
			   (ima_policy_flag & IMA_MEASURE));
	if (!action && !violation_check)
242 243 244
		return 0;

	must_appraise = action & IMA_APPRAISE;
245

246
	/*  Is the appraise rule hook specific?  */
247
	if (action & IMA_FILE_APPRAISE)
248
		func = FILE_CHECK;
249

Al Viro's avatar
Al Viro committed
250
	inode_lock(inode);
251

252
	if (action) {
253
		iint = ima_inode_get(inode);
254
		if (!iint)
255
			rc = -ENOMEM;
256 257
	}

258
	if (!rc && violation_check)
259
		ima_rdwr_violation_check(file, iint, action & IMA_MEASURE,
260
					 &pathbuf, &pathname, filename);
261 262 263 264 265 266 267 268 269 270 271 272 273 274

	inode_unlock(inode);

	if (rc)
		goto out;
	if (!action)
		goto out;

	mutex_lock(&iint->mutex);

	if (test_and_clear_bit(IMA_CHANGE_ATTR, &iint->atomic_flags))
		/* reset appraisal flags if ima_inode_post_setattr was called */
		iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED |
				 IMA_APPRAISE_SUBMASK | IMA_APPRAISED_SUBMASK |
275
				 IMA_NONACTION_FLAGS);
276

277 278 279 280 281 282 283
	/*
	 * Re-evaulate the file if either the xattr has changed or the
	 * kernel has no way of detecting file change on the filesystem.
	 * (Limited to privileged mounted filesystems.)
	 */
	if (test_and_clear_bit(IMA_CHANGE_XATTR, &iint->atomic_flags) ||
	    ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) &&
284 285
	     !(inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) &&
	     !(action & IMA_FAIL_UNVERIFIABLE_SIGS))) {
286
		iint->flags &= ~IMA_DONE_MASK;
287 288
		iint->measured_pcrs = 0;
	}
289

290
	/*
291 292
	 * On stacked filesystems, detect and re-evaluate file data and
	 * metadata changes.
293 294 295
	 */
	real_inode = d_real_inode(file_dentry(file));
	if (real_inode != inode &&
296
	    (action & IMA_DO_MASK) && (iint->flags & IMA_DONE_MASK)) {
297
		if (!IS_I_VERSION(real_inode) ||
298 299
		    integrity_inode_attrs_changed(&iint->real_inode,
						  real_inode)) {
300 301 302
			iint->flags &= ~IMA_DONE_MASK;
			iint->measured_pcrs = 0;
		}
303 304 305 306 307 308 309 310 311

		/*
		 * Reset the EVM status when metadata changed.
		 */
		metadata_inode = d_inode(d_real(file_dentry(file),
					 D_REAL_METADATA));
		if (evm_metadata_changed(inode, metadata_inode))
			iint->flags &= ~(IMA_APPRAISED |
					 IMA_APPRAISED_SUBMASK);
312 313
	}

314
	/* Determine if already appraised/measured based on bitmask
315 316 317
	 * (IMA_MEASURE, IMA_MEASURED, IMA_XXXX_APPRAISE, IMA_XXXX_APPRAISED,
	 *  IMA_AUDIT, IMA_AUDITED)
	 */
318
	iint->flags |= action;
319
	action &= IMA_DO_MASK;
320 321 322 323 324
	action &= ~((iint->flags & (IMA_DONE_MASK ^ IMA_MEASURED)) >> 1);

	/* If target pcr is already measured, unset IMA_MEASURE action */
	if ((action & IMA_MEASURE) && (iint->measured_pcrs & (0x1 << pcr)))
		action ^= IMA_MEASURE;
325

326 327 328
	/* HASH sets the digital signature and update flags, nothing else */
	if ((action & IMA_HASH) &&
	    !(test_bit(IMA_DIGSIG, &iint->atomic_flags))) {
329 330
		xattr_len = ima_read_xattr(file_dentry(file),
					   &xattr_value, xattr_len);
331 332 333 334 335 336 337 338
		if ((xattr_value && xattr_len > 2) &&
		    (xattr_value->type == EVM_IMA_XATTR_DIGSIG))
			set_bit(IMA_DIGSIG, &iint->atomic_flags);
		iint->flags |= IMA_HASHED;
		action ^= IMA_HASH;
		set_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
	}

339 340
	/* Nothing to do, just return existing appraised status */
	if (!action) {
341 342 343 344 345 346
		if (must_appraise) {
			rc = mmap_violation_check(func, file, &pathbuf,
						  &pathname, filename);
			if (!rc)
				rc = ima_get_cache_status(iint, func);
		}
347
		goto out_locked;
348
	}
349

350
	if ((action & IMA_APPRAISE_SUBMASK) ||
351
	    strcmp(template_desc->name, IMA_TEMPLATE_IMA_NAME) != 0) {
352
		/* read 'security.ima' */
353 354
		xattr_len = ima_read_xattr(file_dentry(file),
					   &xattr_value, xattr_len);
355

356 357 358 359 360 361 362 363 364 365 366 367
		/*
		 * Read the appended modsig if allowed by the policy, and allow
		 * an additional measurement list entry, if needed, based on the
		 * template format and whether the file was already measured.
		 */
		if (iint->flags & IMA_MODSIG_ALLOWED) {
			rc = ima_read_modsig(func, buf, size, &modsig);

			if (!rc && ima_template_has_modsig(template_desc) &&
			    iint->flags & IMA_MEASURED)
				action |= IMA_MEASURE;
		}
368 369
	}

370 371
	hash_algo = ima_get_hash_algo(xattr_value, xattr_len);

372
	rc = ima_collect_measurement(iint, file, buf, size, hash_algo, modsig);
373
	if (rc != 0 && rc != -EBADF && rc != -EINVAL)
374
		goto out_locked;
375

376 377
	if (!pathbuf)	/* ima_rdwr_violation possibly pre-fetched */
		pathname = ima_d_path(&file->f_path, &pathbuf, filename);
378

379
	if (action & IMA_MEASURE)
380
		ima_store_measurement(iint, file, pathname,
381
				      xattr_value, xattr_len, modsig, pcr,
382
				      template_desc);
383
	if (rc == 0 && (action & IMA_APPRAISE_SUBMASK)) {
384 385 386 387 388 389 390 391
		rc = ima_check_blacklist(iint, modsig, pcr);
		if (rc != -EPERM) {
			inode_lock(inode);
			rc = ima_appraise_measurement(func, iint, file,
						      pathname, xattr_value,
						      xattr_len, modsig);
			inode_unlock(inode);
		}
392 393 394
		if (!rc)
			rc = mmap_violation_check(func, file, &pathbuf,
						  &pathname, filename);
395
	}
Peter Moody's avatar
Peter Moody committed
396
	if (action & IMA_AUDIT)
397
		ima_audit_measurement(iint, pathname);
398

399 400
	if ((file->f_flags & O_DIRECT) && (iint->flags & IMA_PERMIT_DIRECTIO))
		rc = 0;
401 402 403 404 405 406 407 408 409 410

	/* Ensure the digest was generated using an allowed algorithm */
	if (rc == 0 && must_appraise && allowed_algos != 0 &&
	    (allowed_algos & (1U << hash_algo)) == 0) {
		rc = -EACCES;

		integrity_audit_msg(AUDIT_INTEGRITY_DATA, file_inode(file),
				    pathname, "collect_data",
				    "denied-hash-algorithm", rc, 0);
	}
411 412
out_locked:
	if ((mask & MAY_WRITE) && test_bit(IMA_DIGSIG, &iint->atomic_flags) &&
413
	     !(iint->flags & IMA_NEW_FILE))
414
		rc = -EACCES;
415
	mutex_unlock(&iint->mutex);
416
	kfree(xattr_value);
417
	ima_free_modsig(modsig);
418
out:
Dmitry Kasatkin's avatar
Dmitry Kasatkin committed
419 420
	if (pathbuf)
		__putname(pathbuf);
421 422 423 424 425 426
	if (must_appraise) {
		if (rc && (ima_appraise & IMA_APPRAISE_ENFORCE))
			return -EACCES;
		if (file->f_mode & FMODE_WRITE)
			set_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
	}
427
	return 0;
428 429 430 431 432
}

/**
 * ima_file_mmap - based on policy, collect/store measurement.
 * @file: pointer to the file to be measured (May be NULL)
433 434 435
 * @reqprot: protection requested by the application
 * @prot: protection that will be applied by the kernel
 * @flags: operational flags
436 437 438 439
 *
 * Measure files being mmapped executable based on the ima_must_measure()
 * policy decision.
 *
440 441
 * On success return 0.  On integrity appraisal error, assuming the file
 * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
442
 */
443 444
static int ima_file_mmap(struct file *file, unsigned long reqprot,
			 unsigned long prot, unsigned long flags)
445
{
446
	u32 secid;
447
	int ret;
448

449 450 451 452 453 454 455 456 457 458 459 460 461
	if (!file)
		return 0;

	security_current_getsecid_subj(&secid);

	if (reqprot & PROT_EXEC) {
		ret = process_measurement(file, current_cred(), secid, NULL,
					  0, MAY_EXEC, MMAP_CHECK_REQPROT);
		if (ret)
			return ret;
	}

	if (prot & PROT_EXEC)
462
		return process_measurement(file, current_cred(), secid, NULL,
463
					   0, MAY_EXEC, MMAP_CHECK);
464

465
	return 0;
466 467
}

468 469
/**
 * ima_file_mprotect - based on policy, limit mprotect change
470
 * @vma: vm_area_struct protection is set to
471 472
 * @reqprot: protection requested by the application
 * @prot: protection that will be applied by the kernel
473 474 475 476 477 478 479 480 481
 *
 * Files can be mmap'ed read/write and later changed to execute to circumvent
 * IMA's mmap appraisal policy rules.  Due to locking issues (mmap semaphore
 * would be taken before i_mutex), files can not be measured or appraised at
 * this point.  Eliminate this integrity gap by denying the mprotect
 * PROT_EXECUTE change, if an mmap appraise policy rule exists.
 *
 * On mprotect change success, return 0.  On failure, return -EACESS.
 */
482 483
static int ima_file_mprotect(struct vm_area_struct *vma, unsigned long reqprot,
			     unsigned long prot)
484
{
485
	struct ima_template_desc *template = NULL;
486
	struct file *file;
487 488 489 490 491 492 493 494 495 496
	char filename[NAME_MAX];
	char *pathbuf = NULL;
	const char *pathname = NULL;
	struct inode *inode;
	int result = 0;
	int action;
	u32 secid;
	int pcr;

	/* Is mprotect making an mmap'ed file executable? */
Mimi Zohar's avatar
Mimi Zohar committed
497 498
	if (!(ima_policy_flag & IMA_APPRAISE) || !vma->vm_file ||
	    !(prot & PROT_EXEC) || (vma->vm_flags & VM_EXEC))
499 500
		return 0;

501
	security_current_getsecid_subj(&secid);
502
	inode = file_inode(vma->vm_file);
503
	action = ima_get_action(file_mnt_idmap(vma->vm_file), inode,
504
				current_cred(), secid, MAY_EXEC, MMAP_CHECK,
505
				&pcr, &template, NULL, NULL);
506
	action |= ima_get_action(file_mnt_idmap(vma->vm_file), inode,
507 508 509
				 current_cred(), secid, MAY_EXEC,
				 MMAP_CHECK_REQPROT, &pcr, &template, NULL,
				 NULL);
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527

	/* Is the mmap'ed file in policy? */
	if (!(action & (IMA_MEASURE | IMA_APPRAISE_SUBMASK)))
		return 0;

	if (action & IMA_APPRAISE_SUBMASK)
		result = -EPERM;

	file = vma->vm_file;
	pathname = ima_d_path(&file->f_path, &pathbuf, filename);
	integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, pathname,
			    "collect_data", "failed-mprotect", result, 0);
	if (pathbuf)
		__putname(pathbuf);

	return result;
}

528 529 530 531 532 533 534 535 536 537
/**
 * ima_bprm_check - based on policy, collect/store measurement.
 * @bprm: contains the linux_binprm structure
 *
 * The OS protects against an executable file, already open for write,
 * from being executed in deny_write_access() and an executable file,
 * already open for execute, from being modified in get_write_access().
 * So we can be certain that what we verify and measure here is actually
 * what is being executed.
 *
538 539
 * On success return 0.  On integrity appraisal error, assuming the file
 * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
540
 */
541
static int ima_bprm_check(struct linux_binprm *bprm)
542
{
543 544 545
	int ret;
	u32 secid;

546
	security_current_getsecid_subj(&secid);
547
	ret = process_measurement(bprm->file, current_cred(), secid, NULL, 0,
548
				  MAY_EXEC, BPRM_CHECK);
549 550 551 552 553
	if (ret)
		return ret;

	security_cred_getsecid(bprm->cred, &secid);
	return process_measurement(bprm->file, bprm->cred, secid, NULL, 0,
554
				   MAY_EXEC, CREDS_CHECK);
555 556
}

Mimi Zohar's avatar
Mimi Zohar committed
557
/**
558
 * ima_file_check - based on policy, collect/store measurement.
Mimi Zohar's avatar
Mimi Zohar committed
559
 * @file: pointer to the file to be measured
560
 * @mask: contains MAY_READ, MAY_WRITE, MAY_EXEC or MAY_APPEND
Mimi Zohar's avatar
Mimi Zohar committed
561 562 563
 *
 * Measure files based on the ima_must_measure() policy decision.
 *
564 565
 * On success return 0.  On integrity appraisal error, assuming the file
 * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
Mimi Zohar's avatar
Mimi Zohar committed
566
 */
567
static int ima_file_check(struct file *file, int mask)
Mimi Zohar's avatar
Mimi Zohar committed
568
{
569 570
	u32 secid;

571
	security_current_getsecid_subj(&secid);
572
	return process_measurement(file, current_cred(), secid, NULL, 0,
573
				   mask & (MAY_READ | MAY_WRITE | MAY_EXEC |
574
					   MAY_APPEND), FILE_CHECK);
Mimi Zohar's avatar
Mimi Zohar committed
575 576
}

577 578
static int __ima_inode_hash(struct inode *inode, struct file *file, char *buf,
			    size_t buf_size)
579
{
580
	struct ima_iint_cache *iint = NULL, tmp_iint;
581
	int rc, hash_algo;
582

583
	if (ima_policy_flag) {
584
		iint = ima_iint_find(inode);
585 586 587 588 589 590 591 592 593 594 595 596 597
		if (iint)
			mutex_lock(&iint->mutex);
	}

	if ((!iint || !(iint->flags & IMA_COLLECTED)) && file) {
		if (iint)
			mutex_unlock(&iint->mutex);

		memset(&tmp_iint, 0, sizeof(tmp_iint));
		mutex_init(&tmp_iint.mutex);

		rc = ima_collect_measurement(&tmp_iint, file, NULL, 0,
					     ima_hash_algo, NULL);
598 599 600 601 602
		if (rc < 0) {
			/* ima_hash could be allocated in case of failure. */
			if (rc != -ENOMEM)
				kfree(tmp_iint.ima_hash);

603
			return -EOPNOTSUPP;
604
		}
605 606 607 608

		iint = &tmp_iint;
		mutex_lock(&iint->mutex);
	}
609 610 611 612

	if (!iint)
		return -EOPNOTSUPP;

613 614 615 616
	/*
	 * ima_file_hash can be called when ima_collect_measurement has still
	 * not been called, we might not always have a hash.
	 */
617
	if (!iint->ima_hash || !(iint->flags & IMA_COLLECTED)) {
618 619 620 621
		mutex_unlock(&iint->mutex);
		return -EOPNOTSUPP;
	}

622 623 624 625 626 627 628 629 630
	if (buf) {
		size_t copied_size;

		copied_size = min_t(size_t, iint->ima_hash->length, buf_size);
		memcpy(buf, iint->ima_hash->digest, copied_size);
	}
	hash_algo = iint->ima_hash->algo;
	mutex_unlock(&iint->mutex);

631 632 633
	if (iint == &tmp_iint)
		kfree(iint->ima_hash);

634 635
	return hash_algo;
}
KP Singh's avatar
KP Singh committed
636 637

/**
638
 * ima_file_hash - return a measurement of the file
KP Singh's avatar
KP Singh committed
639 640 641 642 643 644 645 646 647 648 649 650
 * @file: pointer to the file
 * @buf: buffer in which to store the hash
 * @buf_size: length of the buffer
 *
 * On success, return the hash algorithm (as defined in the enum hash_algo).
 * If buf is not NULL, this function also outputs the hash into buf.
 * If the hash is larger than buf_size, then only buf_size bytes will be copied.
 * It generally just makes sense to pass a buffer capable of holding the largest
 * possible hash: IMA_MAX_DIGEST_SIZE.
 * The file hash returned is based on the entire file, including the appended
 * signature.
 *
651
 * If the measurement cannot be performed, return -EOPNOTSUPP.
KP Singh's avatar
KP Singh committed
652 653 654 655 656 657 658
 * If the parameters are incorrect, return -EINVAL.
 */
int ima_file_hash(struct file *file, char *buf, size_t buf_size)
{
	if (!file)
		return -EINVAL;

659
	return __ima_inode_hash(file_inode(file), file, buf, buf_size);
KP Singh's avatar
KP Singh committed
660
}
661 662
EXPORT_SYMBOL_GPL(ima_file_hash);

KP Singh's avatar
KP Singh committed
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
/**
 * ima_inode_hash - return the stored measurement if the inode has been hashed
 * and is in the iint cache.
 * @inode: pointer to the inode
 * @buf: buffer in which to store the hash
 * @buf_size: length of the buffer
 *
 * On success, return the hash algorithm (as defined in the enum hash_algo).
 * If buf is not NULL, this function also outputs the hash into buf.
 * If the hash is larger than buf_size, then only buf_size bytes will be copied.
 * It generally just makes sense to pass a buffer capable of holding the largest
 * possible hash: IMA_MAX_DIGEST_SIZE.
 * The hash returned is based on the entire contents, including the appended
 * signature.
 *
 * If IMA is disabled or if no measurement is available, return -EOPNOTSUPP.
 * If the parameters are incorrect, return -EINVAL.
 */
int ima_inode_hash(struct inode *inode, char *buf, size_t buf_size)
{
	if (!inode)
		return -EINVAL;

686
	return __ima_inode_hash(inode, NULL, buf, buf_size);
KP Singh's avatar
KP Singh committed
687 688 689
}
EXPORT_SYMBOL_GPL(ima_inode_hash);

690 691
/**
 * ima_post_create_tmpfile - mark newly created tmpfile as new
692
 * @idmap: idmap of the mount the inode was found from
693
 * @inode: inode of the newly created tmpfile
694 695 696 697 698
 *
 * No measuring, appraising or auditing of newly created tmpfiles is needed.
 * Skip calling process_measurement(), but indicate which newly, created
 * tmpfiles are in policy.
 */
699 700 701
static void ima_post_create_tmpfile(struct mnt_idmap *idmap,
				    struct inode *inode)

702
{
703
	struct ima_iint_cache *iint;
704 705
	int must_appraise;

706 707 708
	if (!ima_policy_flag || !S_ISREG(inode->i_mode))
		return;

709
	must_appraise = ima_must_appraise(idmap, inode, MAY_ACCESS,
710
					  FILE_CHECK);
711 712 713 714
	if (!must_appraise)
		return;

	/* Nothing to do if we can't allocate memory */
715
	iint = ima_inode_get(inode);
716 717 718 719 720 721 722 723
	if (!iint)
		return;

	/* needed for writing the security xattrs */
	set_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
	iint->ima_file_status = INTEGRITY_PASS;
}

724 725
/**
 * ima_post_path_mknod - mark as a new inode
726
 * @idmap: idmap of the mount the inode was found from
727 728 729 730 731
 * @dentry: newly created dentry
 *
 * Mark files created via the mknodat syscall as new, so that the
 * file data can be written later.
 */
732
static void ima_post_path_mknod(struct mnt_idmap *idmap, struct dentry *dentry)
733
{
734
	struct ima_iint_cache *iint;
735 736 737
	struct inode *inode = dentry->d_inode;
	int must_appraise;

738 739 740
	if (!ima_policy_flag || !S_ISREG(inode->i_mode))
		return;

741
	must_appraise = ima_must_appraise(idmap, inode, MAY_ACCESS,
742
					  FILE_CHECK);
743 744 745
	if (!must_appraise)
		return;

746
	/* Nothing to do if we can't allocate memory */
747
	iint = ima_inode_get(inode);
748 749 750 751 752
	if (!iint)
		return;

	/* needed for re-opening empty files */
	iint->flags |= IMA_NEW_FILE;
753 754
}

755 756 757 758
/**
 * ima_read_file - pre-measure/appraise hook decision based on policy
 * @file: pointer to the file to be measured/appraised/audit
 * @read_id: caller identifier
759
 * @contents: whether a subsequent call will be made to ima_post_read_file()
760 761 762 763 764 765 766
 *
 * Permit reading a file based on policy. The policy rules are written
 * in terms of the policy identifier.  Appraising the integrity of
 * a file requires a file descriptor.
 *
 * For permission return 0, otherwise return -EACCES.
 */
767 768
static int ima_read_file(struct file *file, enum kernel_read_file_id read_id,
			 bool contents)
769
{
770 771
	enum ima_hooks func;
	u32 secid;
772

773 774 775 776
	/*
	 * Do devices using pre-allocated memory run the risk of the
	 * firmware being accessible to the device prior to the completion
	 * of IMA's signature verification any more than when using two
777 778
	 * buffers? It may be desirable to include the buffer address
	 * in this API and walk all the dma_map_single() mappings to check.
779
	 */
780 781 782 783 784 785 786 787 788 789 790

	/*
	 * There will be a call made to ima_post_read_file() with
	 * a filled buffer, so we don't need to perform an extra
	 * read early here.
	 */
	if (contents)
		return 0;

	/* Read entire file for all partial reads. */
	func = read_idmap[read_id] ?: FILE_CHECK;
791
	security_current_getsecid_subj(&secid);
792 793
	return process_measurement(file, current_cred(), secid, NULL,
				   0, MAY_READ, func);
794 795
}

796
const int read_idmap[READING_MAX_ID] = {
797 798 799 800
	[READING_FIRMWARE] = FIRMWARE_CHECK,
	[READING_MODULE] = MODULE_CHECK,
	[READING_KEXEC_IMAGE] = KEXEC_KERNEL_CHECK,
	[READING_KEXEC_INITRAMFS] = KEXEC_INITRAMFS_CHECK,
801
	[READING_POLICY] = POLICY_CHECK
802 803
};

804 805 806 807 808 809 810 811 812 813 814 815 816
/**
 * ima_post_read_file - in memory collect/appraise/audit measurement
 * @file: pointer to the file to be measured/appraised/audit
 * @buf: pointer to in memory file contents
 * @size: size of in memory file contents
 * @read_id: caller identifier
 *
 * Measure/appraise/audit in memory file based on policy.  Policy rules
 * are written in terms of a policy identifier.
 *
 * On success return 0.  On integrity appraisal error, assuming the file
 * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
 */
817 818
static int ima_post_read_file(struct file *file, char *buf, loff_t size,
			      enum kernel_read_file_id read_id)
819
{
820
	enum ima_hooks func;
821
	u32 secid;
822

823 824 825 826
	/* permit signed certs */
	if (!file && read_id == READING_X509_CERTIFICATE)
		return 0;

827 828 829 830 831 832
	if (!file || !buf || size == 0) { /* should never happen */
		if (ima_appraise & IMA_APPRAISE_ENFORCE)
			return -EACCES;
		return 0;
	}

833
	func = read_idmap[read_id] ?: FILE_CHECK;
834
	security_current_getsecid_subj(&secid);
835
	return process_measurement(file, current_cred(), secid, buf, size,
836
				   MAY_READ, func);
837 838
}

839 840 841
/**
 * ima_load_data - appraise decision based on policy
 * @id: kernel load data caller identifier
842 843
 * @contents: whether the full contents will be available in a later
 *	      call to ima_post_load_data().
844 845
 *
 * Callers of this LSM hook can not measure, appraise, or audit the
846
 * data provided by userspace.  Enforce policy rules requiring a file
847 848 849 850
 * signature (eg. kexec'ed kernel image).
 *
 * For permission return 0, otherwise return -EACCES.
 */
851
static int ima_load_data(enum kernel_load_data_id id, bool contents)
852
{
853
	bool ima_enforce, sig_enforce;
854

855 856
	ima_enforce =
		(ima_appraise & IMA_APPRAISE_ENFORCE) == IMA_APPRAISE_ENFORCE;
857 858 859

	switch (id) {
	case LOADING_KEXEC_IMAGE:
860
		if (IS_ENABLED(CONFIG_KEXEC_SIG)
861 862 863 864 865 866
		    && arch_ima_get_secureboot()) {
			pr_err("impossible to appraise a kernel image without a file descriptor; try using kexec_file_load syscall.\n");
			return -EACCES;
		}

		if (ima_enforce && (ima_appraise & IMA_APPRAISE_KEXEC)) {
867 868 869
			pr_err("impossible to appraise a kernel image without a file descriptor; try using kexec_file_load syscall.\n");
			return -EACCES;	/* INTEGRITY_UNKNOWN */
		}
870 871
		break;
	case LOADING_FIRMWARE:
872
		if (ima_enforce && (ima_appraise & IMA_APPRAISE_FIRMWARE) && !contents) {
873 874 875
			pr_err("Prevent firmware sysfs fallback loading.\n");
			return -EACCES;	/* INTEGRITY_UNKNOWN */
		}
876 877 878 879
		break;
	case LOADING_MODULE:
		sig_enforce = is_module_sig_enforced();

880 881
		if (ima_enforce && (!sig_enforce
				    && (ima_appraise & IMA_APPRAISE_MODULES))) {
882 883 884
			pr_err("impossible to appraise a module without a file descriptor. sig_enforce kernel parameter might help\n");
			return -EACCES;	/* INTEGRITY_UNKNOWN */
		}
885
		break;
886 887 888 889 890 891
	default:
		break;
	}
	return 0;
}

892 893 894 895
/**
 * ima_post_load_data - appraise decision based on policy
 * @buf: pointer to in memory file contents
 * @size: size of in memory file contents
896 897
 * @load_id: kernel load data caller identifier
 * @description: @load_id-specific description of contents
898 899 900 901 902 903 904
 *
 * Measure/appraise/audit in memory buffer based on policy.  Policy rules
 * are written in terms of a policy identifier.
 *
 * On success return 0.  On integrity appraisal error, assuming the file
 * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
 */
905 906 907
static int ima_post_load_data(char *buf, loff_t size,
			      enum kernel_load_data_id load_id,
			      char *description)
908
{
909 910 911 912 913 914 915 916 917
	if (load_id == LOADING_FIRMWARE) {
		if ((ima_appraise & IMA_APPRAISE_FIRMWARE) &&
		    (ima_appraise & IMA_APPRAISE_ENFORCE)) {
			pr_err("Prevent firmware loading_store.\n");
			return -EACCES; /* INTEGRITY_UNKNOWN */
		}
		return 0;
	}

918 919 920 921 922 923 924
	/*
	 * Measure the init_module syscall buffer containing the ELF image.
	 */
	if (load_id == LOADING_MODULE)
		ima_measure_critical_data("modules", "init_module",
					  buf, size, true, NULL, 0);

925 926 927
	return 0;
}

928
/**
929
 * process_buffer_measurement - Measure the buffer or the buffer data hash
930
 * @idmap: idmap of the mount the inode was found from
931
 * @inode: inode associated with the object being measured (NULL for KEY_CHECK)
932 933 934
 * @buf: pointer to the buffer that needs to be added to the log.
 * @size: size of buffer(in bytes).
 * @eventname: event name to be used for the buffer entry.
935 936
 * @func: IMA hook
 * @pcr: pcr to extend the measurement
937
 * @func_data: func specific data, may be NULL
938
 * @buf_hash: measure buffer data hash
939 940
 * @digest: buffer digest will be written to
 * @digest_len: buffer length
941
 *
942
 * Based on policy, either the buffer data or buffer data hash is measured
943
 *
944 945 946
 * Return: 0 if the buffer has been successfully measured, 1 if the digest
 * has been written to the passed location but not added to a measurement entry,
 * a negative value otherwise.
947
 */
948
int process_buffer_measurement(struct mnt_idmap *idmap,
949 950 951
			       struct inode *inode, const void *buf, int size,
			       const char *eventname, enum ima_hooks func,
			       int pcr, const char *func_data,
952
			       bool buf_hash, u8 *digest, size_t digest_len)
953 954
{
	int ret = 0;
955
	const char *audit_cause = "ENOMEM";
956
	struct ima_template_entry *entry = NULL;
957
	struct ima_iint_cache iint = {};
958
	struct ima_event_data event_data = {.iint = &iint,
959 960 961
					    .filename = eventname,
					    .buf = buf,
					    .buf_len = size};
962
	struct ima_template_desc *template;
963
	struct ima_max_digest_data hash;
964 965
	struct ima_digest_data *hash_hdr = container_of(&hash.hdr,
						struct ima_digest_data, hdr);
966 967
	char digest_hash[IMA_MAX_DIGEST_SIZE];
	int digest_hash_len = hash_digest_size[ima_hash_algo];
968 969
	int violation = 0;
	int action = 0;
970
	u32 secid;
971

972 973 974 975
	if (digest && digest_len < digest_hash_len)
		return -EINVAL;

	if (!ima_policy_flag && !digest)
976
		return -ENOENT;
977

978 979 980 981 982 983 984
	template = ima_template_desc_buf();
	if (!template) {
		ret = -EINVAL;
		audit_cause = "ima_template_desc_buf";
		goto out;
	}

985 986 987 988 989 990 991 992
	/*
	 * Both LSM hooks and auxilary based buffer measurements are
	 * based on policy.  To avoid code duplication, differentiate
	 * between the LSM hooks and auxilary buffer measurements,
	 * retrieving the policy rule information only for the LSM hook
	 * buffer measurements.
	 */
	if (func) {
993
		security_current_getsecid_subj(&secid);
994
		action = ima_get_action(idmap, inode, current_cred(),
995
					secid, 0, func, &pcr, &template,
996
					func_data, NULL);
997
		if (!(action & IMA_MEASURE) && !digest)
998
			return -ENOENT;
999 1000 1001 1002 1003
	}

	if (!pcr)
		pcr = CONFIG_IMA_MEASURE_PCR_IDX;

1004
	iint.ima_hash = hash_hdr;
1005 1006 1007 1008
	iint.ima_hash->algo = ima_hash_algo;
	iint.ima_hash->length = hash_digest_size[ima_hash_algo];

	ret = ima_calc_buffer_hash(buf, size, iint.ima_hash);
1009 1010
	if (ret < 0) {
		audit_cause = "hashing_error";
1011
		goto out;
1012
	}
1013

1014
	if (buf_hash) {
1015
		memcpy(digest_hash, hash_hdr->digest, digest_hash_len);
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027

		ret = ima_calc_buffer_hash(digest_hash, digest_hash_len,
					   iint.ima_hash);
		if (ret < 0) {
			audit_cause = "hashing_error";
			goto out;
		}

		event_data.buf = digest_hash;
		event_data.buf_len = digest_hash_len;
	}

1028 1029 1030 1031 1032 1033
	if (digest)
		memcpy(digest, iint.ima_hash->digest, digest_hash_len);

	if (!ima_policy_flag || (func && !(action & IMA_MEASURE)))
		return 1;

1034
	ret = ima_alloc_init_template(&event_data, &entry, template);
1035 1036
	if (ret < 0) {
		audit_cause = "alloc_entry";
1037
		goto out;
1038
	}
1039

1040
	ret = ima_store_template(entry, violation, NULL, event_data.buf, pcr);
1041 1042
	if (ret < 0) {
		audit_cause = "store_entry";
1043
		ima_free_template_entry(entry);
1044
	}
1045 1046

out:
1047
	if (ret < 0)
1048 1049 1050
		integrity_audit_message(AUDIT_INTEGRITY_PCR, NULL, eventname,
					func_measure_str(func),
					audit_cause, ret, 0, ret);
1051

1052
	return ret;
1053 1054 1055 1056
}

/**
 * ima_kexec_cmdline - measure kexec cmdline boot args
1057
 * @kernel_fd: file descriptor of the kexec kernel being loaded
1058 1059 1060 1061 1062
 * @buf: pointer to buffer
 * @size: size of buffer
 *
 * Buffers can only be measured, not appraised.
 */
1063
void ima_kexec_cmdline(int kernel_fd, const void *buf, int size)
1064
{
1065 1066 1067 1068 1069 1070 1071 1072 1073
	struct fd f;

	if (!buf || !size)
		return;

	f = fdget(kernel_fd);
	if (!f.file)
		return;

1074
	process_buffer_measurement(file_mnt_idmap(f.file), file_inode(f.file),
1075
				   buf, size, "kexec-cmdline", KEXEC_CMDLINE, 0,
1076
				   NULL, false, NULL, 0);
1077
	fdput(f);
1078 1079
}

1080 1081
/**
 * ima_measure_critical_data - measure kernel integrity critical data
1082
 * @event_label: unique event label for grouping and limiting critical data
1083 1084 1085 1086
 * @event_name: event name for the record in the IMA measurement list
 * @buf: pointer to buffer data
 * @buf_len: length of buffer data (in bytes)
 * @hash: measure buffer data hash
1087 1088
 * @digest: buffer digest will be written to
 * @digest_len: buffer length
1089 1090 1091 1092 1093
 *
 * Measure data critical to the integrity of the kernel into the IMA log
 * and extend the pcr.  Examples of critical data could be various data
 * structures, policies, and states stored in kernel memory that can
 * impact the integrity of the system.
1094
 *
1095 1096 1097
 * Return: 0 if the buffer has been successfully measured, 1 if the digest
 * has been written to the passed location but not added to a measurement entry,
 * a negative value otherwise.
1098
 */
1099 1100 1101
int ima_measure_critical_data(const char *event_label,
			      const char *event_name,
			      const void *buf, size_t buf_len,
1102
			      bool hash, u8 *digest, size_t digest_len)
1103
{
1104
	if (!event_name || !event_label || !buf || !buf_len)
1105
		return -ENOPARAM;
1106

1107
	return process_buffer_measurement(&nop_mnt_idmap, NULL, buf, buf_len,
1108
					  event_name, CRITICAL_DATA, 0,
1109 1110
					  event_label, hash, digest,
					  digest_len);
1111
}
1112
EXPORT_SYMBOL_GPL(ima_measure_critical_data);
1113

1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
#ifdef CONFIG_INTEGRITY_ASYMMETRIC_KEYS

/**
 * ima_kernel_module_request - Prevent crypto-pkcs1pad(rsa,*) requests
 * @kmod_name: kernel module name
 *
 * Avoid a verification loop where verifying the signature of the modprobe
 * binary requires executing modprobe itself. Since the modprobe iint->mutex
 * is already held when the signature verification is performed, a deadlock
 * occurs as soon as modprobe is executed within the critical region, since
 * the same lock cannot be taken again.
 *
 * This happens when public_key_verify_signature(), in case of RSA algorithm,
 * use alg_name to store internal information in order to construct an
 * algorithm on the fly, but crypto_larval_lookup() will try to use alg_name
 * in order to load a kernel module with same name.
 *
 * Since we don't have any real "crypto-pkcs1pad(rsa,*)" kernel modules,
 * we are safe to fail such module request from crypto_larval_lookup(), and
 * avoid the verification loop.
 *
 * Return: Zero if it is safe to load the kernel module, -EINVAL otherwise.
 */
1137
static int ima_kernel_module_request(char *kmod_name)
1138 1139 1140 1141 1142 1143 1144 1145 1146
{
	if (strncmp(kmod_name, "crypto-pkcs1pad(rsa,", 20) == 0)
		return -EINVAL;

	return 0;
}

#endif /* CONFIG_INTEGRITY_ASYMMETRIC_KEYS */

1147 1148 1149 1150
static int __init init_ima(void)
{
	int error;

1151
	ima_appraise_parse_cmdline();
1152
	ima_init_template_list();
1153
	hash_setup(CONFIG_IMA_DEFAULT_HASH);
1154
	error = ima_init();
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164

	if (error && strcmp(hash_algo_name[ima_hash_algo],
			    CONFIG_IMA_DEFAULT_HASH) != 0) {
		pr_info("Allocating %s failed, going to use default hash algorithm %s\n",
			hash_algo_name[ima_hash_algo], CONFIG_IMA_DEFAULT_HASH);
		hash_setup_done = 0;
		hash_setup(CONFIG_IMA_DEFAULT_HASH);
		error = ima_init();
	}

1165 1166 1167
	if (error)
		return error;

1168 1169 1170 1171
	error = register_blocking_lsm_notifier(&ima_lsm_policy_notifier);
	if (error)
		pr_warn("Couldn't register LSM notifier, error %d\n", error);

1172
	if (!error)
1173
		ima_update_policy_flags();
1174

1175 1176 1177
	return error;
}

1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
static struct security_hook_list ima_hooks[] __ro_after_init = {
	LSM_HOOK_INIT(bprm_check_security, ima_bprm_check),
	LSM_HOOK_INIT(file_post_open, ima_file_check),
	LSM_HOOK_INIT(inode_post_create_tmpfile, ima_post_create_tmpfile),
	LSM_HOOK_INIT(file_release, ima_file_free),
	LSM_HOOK_INIT(mmap_file, ima_file_mmap),
	LSM_HOOK_INIT(file_mprotect, ima_file_mprotect),
	LSM_HOOK_INIT(kernel_load_data, ima_load_data),
	LSM_HOOK_INIT(kernel_post_load_data, ima_post_load_data),
	LSM_HOOK_INIT(kernel_read_file, ima_read_file),
	LSM_HOOK_INIT(kernel_post_read_file, ima_post_read_file),
	LSM_HOOK_INIT(path_post_mknod, ima_post_path_mknod),
#ifdef CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS
	LSM_HOOK_INIT(key_post_create_or_update, ima_post_key_create_or_update),
#endif
#ifdef CONFIG_INTEGRITY_ASYMMETRIC_KEYS
	LSM_HOOK_INIT(kernel_module_request, ima_kernel_module_request),
#endif
1196
	LSM_HOOK_INIT(inode_free_security, ima_inode_free),
1197 1198 1199 1200 1201 1202 1203 1204 1205
};

static const struct lsm_id ima_lsmid = {
	.name = "ima",
	.id = LSM_ID_IMA,
};

static int __init init_ima_lsm(void)
{
1206
	ima_iintcache_init();
1207
	security_add_hooks(ima_hooks, ARRAY_SIZE(ima_hooks), &ima_lsmid);
1208
	init_ima_appraise_lsm(&ima_lsmid);
1209 1210 1211
	return 0;
}

1212 1213 1214 1215
struct lsm_blob_sizes ima_blob_sizes __ro_after_init = {
	.lbs_inode = sizeof(struct ima_iint_cache *),
};

1216 1217 1218 1219
DEFINE_LSM(ima) = {
	.name = "ima",
	.init = init_ima_lsm,
	.order = LSM_ORDER_LAST,
1220
	.blobs = &ima_blob_sizes,
1221 1222
};

1223
late_initcall(init_ima);	/* Start IMA after the TPM is available */