1 // SPDX-License-Identifier: GPL-2.0-only
3 * Integrity Measurement Architecture
5 * Copyright (C) 2005,2006,2007,2008 IBM Corporation
8 * Reiner Sailer <sailer@watson.ibm.com>
9 * Serge Hallyn <serue@us.ibm.com>
10 * Kylene Hall <kylene@us.ibm.com>
11 * Mimi Zohar <zohar@us.ibm.com>
14 * implements the IMA hooks: ima_bprm_check, ima_file_mmap,
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <linux/module.h>
21 #include <linux/file.h>
22 #include <linux/binfmts.h>
23 #include <linux/mount.h>
24 #include <linux/mman.h>
25 #include <linux/slab.h>
26 #include <linux/xattr.h>
27 #include <linux/ima.h>
28 #include <linux/iversion.h>
33 #ifdef CONFIG_IMA_APPRAISE
34 int ima_appraise = IMA_APPRAISE_ENFORCE;
39 int ima_hash_algo = HASH_ALGO_SHA1;
40 static int hash_setup_done;
42 static struct notifier_block ima_lsm_policy_notifier = {
43 .notifier_call = ima_lsm_policy_change,
46 static int __init hash_setup(char *str)
48 struct ima_template_desc *template_desc = ima_template_desc_current();
54 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;
64 i = match_string(hash_algo_name, HASH_ALGO__LAST, str);
73 __setup("ima_hash=", hash_setup);
75 /* Prevent mmap'ing a file execute that is already mmap'ed write */
76 static int mmap_violation_check(enum ima_hooks func, struct file *file,
77 char **pathbuf, const char **pathname,
83 if ((func == MMAP_CHECK) && mapping_writably_mapped(file->f_mapping)) {
85 inode = file_inode(file);
87 if (!*pathbuf) /* ima_rdwr_violation possibly pre-fetched */
88 *pathname = ima_d_path(&file->f_path, pathbuf,
90 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, *pathname,
91 "mmap_file", "mmapped_writers", rc, 0);
97 * ima_rdwr_violation_check
99 * Only invalidate the PCR for measured files:
100 * - Opening a file for write when already open for read,
101 * results in a time of measure, time of use (ToMToU) error.
102 * - Opening a file for read when already open for write,
103 * could result in a file measurement error.
106 static void ima_rdwr_violation_check(struct file *file,
107 struct integrity_iint_cache *iint,
110 const char **pathname,
113 struct inode *inode = file_inode(file);
114 fmode_t mode = file->f_mode;
115 bool send_tomtou = false, send_writers = false;
117 if (mode & FMODE_WRITE) {
118 if (atomic_read(&inode->i_readcount) && IS_IMA(inode)) {
120 iint = integrity_iint_find(inode);
121 /* IMA_MEASURE is set from reader side */
122 if (iint && test_bit(IMA_MUST_MEASURE,
123 &iint->atomic_flags))
128 set_bit(IMA_MUST_MEASURE, &iint->atomic_flags);
129 if (inode_is_open_for_write(inode) && must_measure)
133 if (!send_tomtou && !send_writers)
136 *pathname = ima_d_path(&file->f_path, pathbuf, filename);
139 ima_add_violation(file, *pathname, iint,
140 "invalid_pcr", "ToMToU");
142 ima_add_violation(file, *pathname, iint,
143 "invalid_pcr", "open_writers");
146 static void ima_check_last_writer(struct integrity_iint_cache *iint,
147 struct inode *inode, struct file *file)
149 fmode_t mode = file->f_mode;
152 if (!(mode & FMODE_WRITE))
155 mutex_lock(&iint->mutex);
156 if (atomic_read(&inode->i_writecount) == 1) {
157 update = test_and_clear_bit(IMA_UPDATE_XATTR,
158 &iint->atomic_flags);
159 if (!IS_I_VERSION(inode) ||
160 !inode_eq_iversion(inode, iint->version) ||
161 (iint->flags & IMA_NEW_FILE)) {
162 iint->flags &= ~(IMA_DONE_MASK | IMA_NEW_FILE);
163 iint->measured_pcrs = 0;
165 ima_update_xattr(iint, file);
168 mutex_unlock(&iint->mutex);
172 * ima_file_free - called on __fput()
173 * @file: pointer to file structure being freed
175 * Flag files that changed, based on i_version
177 void ima_file_free(struct file *file)
179 struct inode *inode = file_inode(file);
180 struct integrity_iint_cache *iint;
182 if (!ima_policy_flag || !S_ISREG(inode->i_mode))
185 iint = integrity_iint_find(inode);
189 ima_check_last_writer(iint, inode, file);
192 static int process_measurement(struct file *file, const struct cred *cred,
193 u32 secid, char *buf, loff_t size, int mask,
196 struct inode *inode = file_inode(file);
197 struct integrity_iint_cache *iint = NULL;
198 struct ima_template_desc *template_desc = NULL;
199 char *pathbuf = NULL;
200 char filename[NAME_MAX];
201 const char *pathname = NULL;
202 int rc = 0, action, must_appraise = 0;
203 int pcr = CONFIG_IMA_MEASURE_PCR_IDX;
204 struct evm_ima_xattr_data *xattr_value = NULL;
206 bool violation_check;
207 enum hash_algo hash_algo;
209 if (!ima_policy_flag || !S_ISREG(inode->i_mode))
212 /* Return an IMA_MEASURE, IMA_APPRAISE, IMA_AUDIT action
213 * bitmask based on the appraise/audit/measurement policy.
214 * Included is the appraise submask.
216 action = ima_get_action(inode, cred, secid, mask, func, &pcr,
218 violation_check = ((func == FILE_CHECK || func == MMAP_CHECK) &&
219 (ima_policy_flag & IMA_MEASURE));
220 if (!action && !violation_check)
223 must_appraise = action & IMA_APPRAISE;
225 /* Is the appraise rule hook specific? */
226 if (action & IMA_FILE_APPRAISE)
232 iint = integrity_inode_get(inode);
237 if (!rc && violation_check)
238 ima_rdwr_violation_check(file, iint, action & IMA_MEASURE,
239 &pathbuf, &pathname, filename);
248 mutex_lock(&iint->mutex);
250 if (test_and_clear_bit(IMA_CHANGE_ATTR, &iint->atomic_flags))
251 /* reset appraisal flags if ima_inode_post_setattr was called */
252 iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED |
253 IMA_APPRAISE_SUBMASK | IMA_APPRAISED_SUBMASK |
257 * Re-evaulate the file if either the xattr has changed or the
258 * kernel has no way of detecting file change on the filesystem.
259 * (Limited to privileged mounted filesystems.)
261 if (test_and_clear_bit(IMA_CHANGE_XATTR, &iint->atomic_flags) ||
262 ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) &&
263 !(inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) &&
264 !(action & IMA_FAIL_UNVERIFIABLE_SIGS))) {
265 iint->flags &= ~IMA_DONE_MASK;
266 iint->measured_pcrs = 0;
269 /* Determine if already appraised/measured based on bitmask
270 * (IMA_MEASURE, IMA_MEASURED, IMA_XXXX_APPRAISE, IMA_XXXX_APPRAISED,
271 * IMA_AUDIT, IMA_AUDITED)
273 iint->flags |= action;
274 action &= IMA_DO_MASK;
275 action &= ~((iint->flags & (IMA_DONE_MASK ^ IMA_MEASURED)) >> 1);
277 /* If target pcr is already measured, unset IMA_MEASURE action */
278 if ((action & IMA_MEASURE) && (iint->measured_pcrs & (0x1 << pcr)))
279 action ^= IMA_MEASURE;
281 /* HASH sets the digital signature and update flags, nothing else */
282 if ((action & IMA_HASH) &&
283 !(test_bit(IMA_DIGSIG, &iint->atomic_flags))) {
284 xattr_len = ima_read_xattr(file_dentry(file), &xattr_value);
285 if ((xattr_value && xattr_len > 2) &&
286 (xattr_value->type == EVM_IMA_XATTR_DIGSIG))
287 set_bit(IMA_DIGSIG, &iint->atomic_flags);
288 iint->flags |= IMA_HASHED;
290 set_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
293 /* Nothing to do, just return existing appraised status */
296 rc = mmap_violation_check(func, file, &pathbuf,
297 &pathname, filename);
299 rc = ima_get_cache_status(iint, func);
304 if ((action & IMA_APPRAISE_SUBMASK) ||
305 strcmp(template_desc->name, IMA_TEMPLATE_IMA_NAME) != 0)
306 /* read 'security.ima' */
307 xattr_len = ima_read_xattr(file_dentry(file), &xattr_value);
309 hash_algo = ima_get_hash_algo(xattr_value, xattr_len);
311 rc = ima_collect_measurement(iint, file, buf, size, hash_algo);
312 if (rc != 0 && rc != -EBADF && rc != -EINVAL)
315 if (!pathbuf) /* ima_rdwr_violation possibly pre-fetched */
316 pathname = ima_d_path(&file->f_path, &pathbuf, filename);
318 if (action & IMA_MEASURE)
319 ima_store_measurement(iint, file, pathname,
320 xattr_value, xattr_len, pcr,
322 if (rc == 0 && (action & IMA_APPRAISE_SUBMASK)) {
324 rc = ima_appraise_measurement(func, iint, file, pathname,
325 xattr_value, xattr_len);
328 rc = mmap_violation_check(func, file, &pathbuf,
329 &pathname, filename);
331 if (action & IMA_AUDIT)
332 ima_audit_measurement(iint, pathname);
334 if ((file->f_flags & O_DIRECT) && (iint->flags & IMA_PERMIT_DIRECTIO))
337 if ((mask & MAY_WRITE) && test_bit(IMA_DIGSIG, &iint->atomic_flags) &&
338 !(iint->flags & IMA_NEW_FILE))
340 mutex_unlock(&iint->mutex);
346 if (rc && (ima_appraise & IMA_APPRAISE_ENFORCE))
348 if (file->f_mode & FMODE_WRITE)
349 set_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
355 * ima_file_mmap - based on policy, collect/store measurement.
356 * @file: pointer to the file to be measured (May be NULL)
357 * @prot: contains the protection that will be applied by the kernel.
359 * Measure files being mmapped executable based on the ima_must_measure()
362 * On success return 0. On integrity appraisal error, assuming the file
363 * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
365 int ima_file_mmap(struct file *file, unsigned long prot)
369 if (file && (prot & PROT_EXEC)) {
370 security_task_getsecid(current, &secid);
371 return process_measurement(file, current_cred(), secid, NULL,
372 0, MAY_EXEC, MMAP_CHECK);
379 * ima_bprm_check - based on policy, collect/store measurement.
380 * @bprm: contains the linux_binprm structure
382 * The OS protects against an executable file, already open for write,
383 * from being executed in deny_write_access() and an executable file,
384 * already open for execute, from being modified in get_write_access().
385 * So we can be certain that what we verify and measure here is actually
386 * what is being executed.
388 * On success return 0. On integrity appraisal error, assuming the file
389 * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
391 int ima_bprm_check(struct linux_binprm *bprm)
396 security_task_getsecid(current, &secid);
397 ret = process_measurement(bprm->file, current_cred(), secid, NULL, 0,
398 MAY_EXEC, BPRM_CHECK);
402 security_cred_getsecid(bprm->cred, &secid);
403 return process_measurement(bprm->file, bprm->cred, secid, NULL, 0,
404 MAY_EXEC, CREDS_CHECK);
408 * ima_path_check - based on policy, collect/store measurement.
409 * @file: pointer to the file to be measured
410 * @mask: contains MAY_READ, MAY_WRITE, MAY_EXEC or MAY_APPEND
412 * Measure files based on the ima_must_measure() policy decision.
414 * On success return 0. On integrity appraisal error, assuming the file
415 * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
417 int ima_file_check(struct file *file, int mask)
421 security_task_getsecid(current, &secid);
422 return process_measurement(file, current_cred(), secid, NULL, 0,
423 mask & (MAY_READ | MAY_WRITE | MAY_EXEC |
424 MAY_APPEND), FILE_CHECK);
426 EXPORT_SYMBOL_GPL(ima_file_check);
429 * ima_post_create_tmpfile - mark newly created tmpfile as new
430 * @file : newly created tmpfile
432 * No measuring, appraising or auditing of newly created tmpfiles is needed.
433 * Skip calling process_measurement(), but indicate which newly, created
434 * tmpfiles are in policy.
436 void ima_post_create_tmpfile(struct inode *inode)
438 struct integrity_iint_cache *iint;
441 must_appraise = ima_must_appraise(inode, MAY_ACCESS, FILE_CHECK);
445 /* Nothing to do if we can't allocate memory */
446 iint = integrity_inode_get(inode);
450 /* needed for writing the security xattrs */
451 set_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
452 iint->ima_file_status = INTEGRITY_PASS;
456 * ima_post_path_mknod - mark as a new inode
457 * @dentry: newly created dentry
459 * Mark files created via the mknodat syscall as new, so that the
460 * file data can be written later.
462 void ima_post_path_mknod(struct dentry *dentry)
464 struct integrity_iint_cache *iint;
465 struct inode *inode = dentry->d_inode;
468 must_appraise = ima_must_appraise(inode, MAY_ACCESS, FILE_CHECK);
472 /* Nothing to do if we can't allocate memory */
473 iint = integrity_inode_get(inode);
477 /* needed for re-opening empty files */
478 iint->flags |= IMA_NEW_FILE;
482 * ima_read_file - pre-measure/appraise hook decision based on policy
483 * @file: pointer to the file to be measured/appraised/audit
484 * @read_id: caller identifier
486 * Permit reading a file based on policy. The policy rules are written
487 * in terms of the policy identifier. Appraising the integrity of
488 * a file requires a file descriptor.
490 * For permission return 0, otherwise return -EACCES.
492 int ima_read_file(struct file *file, enum kernel_read_file_id read_id)
495 * READING_FIRMWARE_PREALLOC_BUFFER
497 * Do devices using pre-allocated memory run the risk of the
498 * firmware being accessible to the device prior to the completion
499 * of IMA's signature verification any more than when using two
505 static const int read_idmap[READING_MAX_ID] = {
506 [READING_FIRMWARE] = FIRMWARE_CHECK,
507 [READING_FIRMWARE_PREALLOC_BUFFER] = FIRMWARE_CHECK,
508 [READING_MODULE] = MODULE_CHECK,
509 [READING_KEXEC_IMAGE] = KEXEC_KERNEL_CHECK,
510 [READING_KEXEC_INITRAMFS] = KEXEC_INITRAMFS_CHECK,
511 [READING_POLICY] = POLICY_CHECK
515 * ima_post_read_file - in memory collect/appraise/audit measurement
516 * @file: pointer to the file to be measured/appraised/audit
517 * @buf: pointer to in memory file contents
518 * @size: size of in memory file contents
519 * @read_id: caller identifier
521 * Measure/appraise/audit in memory file based on policy. Policy rules
522 * are written in terms of a policy identifier.
524 * On success return 0. On integrity appraisal error, assuming the file
525 * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
527 int ima_post_read_file(struct file *file, void *buf, loff_t size,
528 enum kernel_read_file_id read_id)
533 if (!file && read_id == READING_FIRMWARE) {
534 if ((ima_appraise & IMA_APPRAISE_FIRMWARE) &&
535 (ima_appraise & IMA_APPRAISE_ENFORCE)) {
536 pr_err("Prevent firmware loading_store.\n");
537 return -EACCES; /* INTEGRITY_UNKNOWN */
542 /* permit signed certs */
543 if (!file && read_id == READING_X509_CERTIFICATE)
546 if (!file || !buf || size == 0) { /* should never happen */
547 if (ima_appraise & IMA_APPRAISE_ENFORCE)
552 func = read_idmap[read_id] ?: FILE_CHECK;
553 security_task_getsecid(current, &secid);
554 return process_measurement(file, current_cred(), secid, buf, size,
559 * ima_load_data - appraise decision based on policy
560 * @id: kernel load data caller identifier
562 * Callers of this LSM hook can not measure, appraise, or audit the
563 * data provided by userspace. Enforce policy rules requring a file
564 * signature (eg. kexec'ed kernel image).
566 * For permission return 0, otherwise return -EACCES.
568 int ima_load_data(enum kernel_load_data_id id)
570 bool ima_enforce, sig_enforce;
573 (ima_appraise & IMA_APPRAISE_ENFORCE) == IMA_APPRAISE_ENFORCE;
576 case LOADING_KEXEC_IMAGE:
577 if (IS_ENABLED(CONFIG_KEXEC_VERIFY_SIG)
578 && arch_ima_get_secureboot()) {
579 pr_err("impossible to appraise a kernel image without a file descriptor; try using kexec_file_load syscall.\n");
583 if (ima_enforce && (ima_appraise & IMA_APPRAISE_KEXEC)) {
584 pr_err("impossible to appraise a kernel image without a file descriptor; try using kexec_file_load syscall.\n");
585 return -EACCES; /* INTEGRITY_UNKNOWN */
588 case LOADING_FIRMWARE:
589 if (ima_enforce && (ima_appraise & IMA_APPRAISE_FIRMWARE)) {
590 pr_err("Prevent firmware sysfs fallback loading.\n");
591 return -EACCES; /* INTEGRITY_UNKNOWN */
595 sig_enforce = is_module_sig_enforced();
597 if (ima_enforce && (!sig_enforce
598 && (ima_appraise & IMA_APPRAISE_MODULES))) {
599 pr_err("impossible to appraise a module without a file descriptor. sig_enforce kernel parameter might help\n");
600 return -EACCES; /* INTEGRITY_UNKNOWN */
609 * process_buffer_measurement - Measure the buffer to ima log.
610 * @buf: pointer to the buffer that needs to be added to the log.
611 * @size: size of buffer(in bytes).
612 * @eventname: event name to be used for the buffer entry.
613 * @cred: a pointer to a credentials structure for user validation.
614 * @secid: the secid of the task to be validated.
616 * Based on policy, the buffer is measured into the ima log.
618 static void process_buffer_measurement(const void *buf, int size,
619 const char *eventname,
620 const struct cred *cred, u32 secid)
623 struct ima_template_entry *entry = NULL;
624 struct integrity_iint_cache iint = {};
625 struct ima_event_data event_data = {.iint = &iint,
626 .filename = eventname,
629 struct ima_template_desc *template_desc = NULL;
631 struct ima_digest_data hdr;
632 char digest[IMA_MAX_DIGEST_SIZE];
635 int pcr = CONFIG_IMA_MEASURE_PCR_IDX;
638 action = ima_get_action(NULL, cred, secid, 0, KEXEC_CMDLINE, &pcr,
640 if (!(action & IMA_MEASURE))
643 iint.ima_hash = &hash.hdr;
644 iint.ima_hash->algo = ima_hash_algo;
645 iint.ima_hash->length = hash_digest_size[ima_hash_algo];
647 ret = ima_calc_buffer_hash(buf, size, iint.ima_hash);
651 ret = ima_alloc_init_template(&event_data, &entry, template_desc);
655 ret = ima_store_template(entry, violation, NULL, buf, pcr);
658 ima_free_template_entry(entry);
665 * ima_kexec_cmdline - measure kexec cmdline boot args
666 * @buf: pointer to buffer
667 * @size: size of buffer
669 * Buffers can only be measured, not appraised.
671 void ima_kexec_cmdline(const void *buf, int size)
675 if (buf && size != 0) {
676 security_task_getsecid(current, &secid);
677 process_buffer_measurement(buf, size, "kexec-cmdline",
678 current_cred(), secid);
682 static int __init init_ima(void)
686 ima_init_template_list();
687 hash_setup(CONFIG_IMA_DEFAULT_HASH);
690 if (error && strcmp(hash_algo_name[ima_hash_algo],
691 CONFIG_IMA_DEFAULT_HASH) != 0) {
692 pr_info("Allocating %s failed, going to use default hash algorithm %s\n",
693 hash_algo_name[ima_hash_algo], CONFIG_IMA_DEFAULT_HASH);
695 hash_setup(CONFIG_IMA_DEFAULT_HASH);
699 error = register_blocking_lsm_notifier(&ima_lsm_policy_notifier);
701 pr_warn("Couldn't register LSM notifier, error %d\n", error);
704 ima_update_policy_flag();
709 late_initcall(init_ima); /* Start IMA after the TPM is available */