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;
205 struct modsig *modsig = NULL;
207 bool violation_check;
208 enum hash_algo hash_algo;
210 if (!ima_policy_flag || !S_ISREG(inode->i_mode))
213 /* Return an IMA_MEASURE, IMA_APPRAISE, IMA_AUDIT action
214 * bitmask based on the appraise/audit/measurement policy.
215 * Included is the appraise submask.
217 action = ima_get_action(inode, cred, secid, mask, func, &pcr,
218 &template_desc, NULL);
219 violation_check = ((func == FILE_CHECK || func == MMAP_CHECK) &&
220 (ima_policy_flag & IMA_MEASURE));
221 if (!action && !violation_check)
224 must_appraise = action & IMA_APPRAISE;
226 /* Is the appraise rule hook specific? */
227 if (action & IMA_FILE_APPRAISE)
233 iint = integrity_inode_get(inode);
238 if (!rc && violation_check)
239 ima_rdwr_violation_check(file, iint, action & IMA_MEASURE,
240 &pathbuf, &pathname, filename);
249 mutex_lock(&iint->mutex);
251 if (test_and_clear_bit(IMA_CHANGE_ATTR, &iint->atomic_flags))
252 /* reset appraisal flags if ima_inode_post_setattr was called */
253 iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED |
254 IMA_APPRAISE_SUBMASK | IMA_APPRAISED_SUBMASK |
258 * Re-evaulate the file if either the xattr has changed or the
259 * kernel has no way of detecting file change on the filesystem.
260 * (Limited to privileged mounted filesystems.)
262 if (test_and_clear_bit(IMA_CHANGE_XATTR, &iint->atomic_flags) ||
263 ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) &&
264 !(inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) &&
265 !(action & IMA_FAIL_UNVERIFIABLE_SIGS))) {
266 iint->flags &= ~IMA_DONE_MASK;
267 iint->measured_pcrs = 0;
270 /* Determine if already appraised/measured based on bitmask
271 * (IMA_MEASURE, IMA_MEASURED, IMA_XXXX_APPRAISE, IMA_XXXX_APPRAISED,
272 * IMA_AUDIT, IMA_AUDITED)
274 iint->flags |= action;
275 action &= IMA_DO_MASK;
276 action &= ~((iint->flags & (IMA_DONE_MASK ^ IMA_MEASURED)) >> 1);
278 /* If target pcr is already measured, unset IMA_MEASURE action */
279 if ((action & IMA_MEASURE) && (iint->measured_pcrs & (0x1 << pcr)))
280 action ^= IMA_MEASURE;
282 /* HASH sets the digital signature and update flags, nothing else */
283 if ((action & IMA_HASH) &&
284 !(test_bit(IMA_DIGSIG, &iint->atomic_flags))) {
285 xattr_len = ima_read_xattr(file_dentry(file), &xattr_value);
286 if ((xattr_value && xattr_len > 2) &&
287 (xattr_value->type == EVM_IMA_XATTR_DIGSIG))
288 set_bit(IMA_DIGSIG, &iint->atomic_flags);
289 iint->flags |= IMA_HASHED;
291 set_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
294 /* Nothing to do, just return existing appraised status */
297 rc = mmap_violation_check(func, file, &pathbuf,
298 &pathname, filename);
300 rc = ima_get_cache_status(iint, func);
305 if ((action & IMA_APPRAISE_SUBMASK) ||
306 strcmp(template_desc->name, IMA_TEMPLATE_IMA_NAME) != 0) {
307 /* read 'security.ima' */
308 xattr_len = ima_read_xattr(file_dentry(file), &xattr_value);
311 * Read the appended modsig if allowed by the policy, and allow
312 * an additional measurement list entry, if needed, based on the
313 * template format and whether the file was already measured.
315 if (iint->flags & IMA_MODSIG_ALLOWED) {
316 rc = ima_read_modsig(func, buf, size, &modsig);
318 if (!rc && ima_template_has_modsig(template_desc) &&
319 iint->flags & IMA_MEASURED)
320 action |= IMA_MEASURE;
324 hash_algo = ima_get_hash_algo(xattr_value, xattr_len);
326 rc = ima_collect_measurement(iint, file, buf, size, hash_algo, modsig);
327 if (rc != 0 && rc != -EBADF && rc != -EINVAL)
330 if (!pathbuf) /* ima_rdwr_violation possibly pre-fetched */
331 pathname = ima_d_path(&file->f_path, &pathbuf, filename);
333 if (action & IMA_MEASURE)
334 ima_store_measurement(iint, file, pathname,
335 xattr_value, xattr_len, modsig, pcr,
337 if (rc == 0 && (action & IMA_APPRAISE_SUBMASK)) {
338 rc = ima_check_blacklist(iint, modsig, pcr);
341 rc = ima_appraise_measurement(func, iint, file,
342 pathname, xattr_value,
347 rc = mmap_violation_check(func, file, &pathbuf,
348 &pathname, filename);
350 if (action & IMA_AUDIT)
351 ima_audit_measurement(iint, pathname);
353 if ((file->f_flags & O_DIRECT) && (iint->flags & IMA_PERMIT_DIRECTIO))
356 if ((mask & MAY_WRITE) && test_bit(IMA_DIGSIG, &iint->atomic_flags) &&
357 !(iint->flags & IMA_NEW_FILE))
359 mutex_unlock(&iint->mutex);
361 ima_free_modsig(modsig);
366 if (rc && (ima_appraise & IMA_APPRAISE_ENFORCE))
368 if (file->f_mode & FMODE_WRITE)
369 set_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
375 * ima_file_mmap - based on policy, collect/store measurement.
376 * @file: pointer to the file to be measured (May be NULL)
377 * @prot: contains the protection that will be applied by the kernel.
379 * Measure files being mmapped executable based on the ima_must_measure()
382 * On success return 0. On integrity appraisal error, assuming the file
383 * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
385 int ima_file_mmap(struct file *file, unsigned long prot)
389 if (file && (prot & PROT_EXEC)) {
390 security_task_getsecid(current, &secid);
391 return process_measurement(file, current_cred(), secid, NULL,
392 0, MAY_EXEC, MMAP_CHECK);
399 * ima_bprm_check - based on policy, collect/store measurement.
400 * @bprm: contains the linux_binprm structure
402 * The OS protects against an executable file, already open for write,
403 * from being executed in deny_write_access() and an executable file,
404 * already open for execute, from being modified in get_write_access().
405 * So we can be certain that what we verify and measure here is actually
406 * what is being executed.
408 * On success return 0. On integrity appraisal error, assuming the file
409 * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
411 int ima_bprm_check(struct linux_binprm *bprm)
416 security_task_getsecid(current, &secid);
417 ret = process_measurement(bprm->file, current_cred(), secid, NULL, 0,
418 MAY_EXEC, BPRM_CHECK);
422 security_cred_getsecid(bprm->cred, &secid);
423 return process_measurement(bprm->file, bprm->cred, secid, NULL, 0,
424 MAY_EXEC, CREDS_CHECK);
428 * ima_path_check - based on policy, collect/store measurement.
429 * @file: pointer to the file to be measured
430 * @mask: contains MAY_READ, MAY_WRITE, MAY_EXEC or MAY_APPEND
432 * Measure files based on the ima_must_measure() policy decision.
434 * On success return 0. On integrity appraisal error, assuming the file
435 * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
437 int ima_file_check(struct file *file, int mask)
441 security_task_getsecid(current, &secid);
442 return process_measurement(file, current_cred(), secid, NULL, 0,
443 mask & (MAY_READ | MAY_WRITE | MAY_EXEC |
444 MAY_APPEND), FILE_CHECK);
446 EXPORT_SYMBOL_GPL(ima_file_check);
449 * ima_file_hash - return the stored measurement if a file has been hashed and
450 * is in the iint cache.
451 * @file: pointer to the file
452 * @buf: buffer in which to store the hash
453 * @buf_size: length of the buffer
455 * On success, return the hash algorithm (as defined in the enum hash_algo).
456 * If buf is not NULL, this function also outputs the hash into buf.
457 * If the hash is larger than buf_size, then only buf_size bytes will be copied.
458 * It generally just makes sense to pass a buffer capable of holding the largest
459 * possible hash: IMA_MAX_DIGEST_SIZE.
460 * The file hash returned is based on the entire file, including the appended
463 * If IMA is disabled or if no measurement is available, return -EOPNOTSUPP.
464 * If the parameters are incorrect, return -EINVAL.
466 int ima_file_hash(struct file *file, char *buf, size_t buf_size)
469 struct integrity_iint_cache *iint;
475 if (!ima_policy_flag)
478 inode = file_inode(file);
479 iint = integrity_iint_find(inode);
483 mutex_lock(&iint->mutex);
487 copied_size = min_t(size_t, iint->ima_hash->length, buf_size);
488 memcpy(buf, iint->ima_hash->digest, copied_size);
490 hash_algo = iint->ima_hash->algo;
491 mutex_unlock(&iint->mutex);
495 EXPORT_SYMBOL_GPL(ima_file_hash);
498 * ima_post_create_tmpfile - mark newly created tmpfile as new
499 * @file : newly created tmpfile
501 * No measuring, appraising or auditing of newly created tmpfiles is needed.
502 * Skip calling process_measurement(), but indicate which newly, created
503 * tmpfiles are in policy.
505 void ima_post_create_tmpfile(struct inode *inode)
507 struct integrity_iint_cache *iint;
510 must_appraise = ima_must_appraise(inode, MAY_ACCESS, FILE_CHECK);
514 /* Nothing to do if we can't allocate memory */
515 iint = integrity_inode_get(inode);
519 /* needed for writing the security xattrs */
520 set_bit(IMA_UPDATE_XATTR, &iint->atomic_flags);
521 iint->ima_file_status = INTEGRITY_PASS;
525 * ima_post_path_mknod - mark as a new inode
526 * @dentry: newly created dentry
528 * Mark files created via the mknodat syscall as new, so that the
529 * file data can be written later.
531 void ima_post_path_mknod(struct dentry *dentry)
533 struct integrity_iint_cache *iint;
534 struct inode *inode = dentry->d_inode;
537 must_appraise = ima_must_appraise(inode, MAY_ACCESS, FILE_CHECK);
541 /* Nothing to do if we can't allocate memory */
542 iint = integrity_inode_get(inode);
546 /* needed for re-opening empty files */
547 iint->flags |= IMA_NEW_FILE;
551 * ima_read_file - pre-measure/appraise hook decision based on policy
552 * @file: pointer to the file to be measured/appraised/audit
553 * @read_id: caller identifier
555 * Permit reading a file based on policy. The policy rules are written
556 * in terms of the policy identifier. Appraising the integrity of
557 * a file requires a file descriptor.
559 * For permission return 0, otherwise return -EACCES.
561 int ima_read_file(struct file *file, enum kernel_read_file_id read_id)
564 * READING_FIRMWARE_PREALLOC_BUFFER
566 * Do devices using pre-allocated memory run the risk of the
567 * firmware being accessible to the device prior to the completion
568 * of IMA's signature verification any more than when using two
574 const int read_idmap[READING_MAX_ID] = {
575 [READING_FIRMWARE] = FIRMWARE_CHECK,
576 [READING_FIRMWARE_PREALLOC_BUFFER] = FIRMWARE_CHECK,
577 [READING_MODULE] = MODULE_CHECK,
578 [READING_KEXEC_IMAGE] = KEXEC_KERNEL_CHECK,
579 [READING_KEXEC_INITRAMFS] = KEXEC_INITRAMFS_CHECK,
580 [READING_POLICY] = POLICY_CHECK
584 * ima_post_read_file - in memory collect/appraise/audit measurement
585 * @file: pointer to the file to be measured/appraised/audit
586 * @buf: pointer to in memory file contents
587 * @size: size of in memory file contents
588 * @read_id: caller identifier
590 * Measure/appraise/audit in memory file based on policy. Policy rules
591 * are written in terms of a policy identifier.
593 * On success return 0. On integrity appraisal error, assuming the file
594 * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
596 int ima_post_read_file(struct file *file, void *buf, loff_t size,
597 enum kernel_read_file_id read_id)
602 if (!file && read_id == READING_FIRMWARE) {
603 if ((ima_appraise & IMA_APPRAISE_FIRMWARE) &&
604 (ima_appraise & IMA_APPRAISE_ENFORCE)) {
605 pr_err("Prevent firmware loading_store.\n");
606 return -EACCES; /* INTEGRITY_UNKNOWN */
611 /* permit signed certs */
612 if (!file && read_id == READING_X509_CERTIFICATE)
615 if (!file || !buf || size == 0) { /* should never happen */
616 if (ima_appraise & IMA_APPRAISE_ENFORCE)
621 func = read_idmap[read_id] ?: FILE_CHECK;
622 security_task_getsecid(current, &secid);
623 return process_measurement(file, current_cred(), secid, buf, size,
628 * ima_load_data - appraise decision based on policy
629 * @id: kernel load data caller identifier
631 * Callers of this LSM hook can not measure, appraise, or audit the
632 * data provided by userspace. Enforce policy rules requring a file
633 * signature (eg. kexec'ed kernel image).
635 * For permission return 0, otherwise return -EACCES.
637 int ima_load_data(enum kernel_load_data_id id)
639 bool ima_enforce, sig_enforce;
642 (ima_appraise & IMA_APPRAISE_ENFORCE) == IMA_APPRAISE_ENFORCE;
645 case LOADING_KEXEC_IMAGE:
646 if (IS_ENABLED(CONFIG_KEXEC_SIG)
647 && arch_ima_get_secureboot()) {
648 pr_err("impossible to appraise a kernel image without a file descriptor; try using kexec_file_load syscall.\n");
652 if (ima_enforce && (ima_appraise & IMA_APPRAISE_KEXEC)) {
653 pr_err("impossible to appraise a kernel image without a file descriptor; try using kexec_file_load syscall.\n");
654 return -EACCES; /* INTEGRITY_UNKNOWN */
657 case LOADING_FIRMWARE:
658 if (ima_enforce && (ima_appraise & IMA_APPRAISE_FIRMWARE)) {
659 pr_err("Prevent firmware sysfs fallback loading.\n");
660 return -EACCES; /* INTEGRITY_UNKNOWN */
664 sig_enforce = is_module_sig_enforced();
666 if (ima_enforce && (!sig_enforce
667 && (ima_appraise & IMA_APPRAISE_MODULES))) {
668 pr_err("impossible to appraise a module without a file descriptor. sig_enforce kernel parameter might help\n");
669 return -EACCES; /* INTEGRITY_UNKNOWN */
678 * process_buffer_measurement - Measure the buffer to ima log.
679 * @buf: pointer to the buffer that needs to be added to the log.
680 * @size: size of buffer(in bytes).
681 * @eventname: event name to be used for the buffer entry.
683 * @pcr: pcr to extend the measurement
684 * @keyring: keyring name to determine the action to be performed
686 * Based on policy, the buffer is measured into the ima log.
688 void process_buffer_measurement(const void *buf, int size,
689 const char *eventname, enum ima_hooks func,
690 int pcr, const char *keyring)
693 struct ima_template_entry *entry = NULL;
694 struct integrity_iint_cache iint = {};
695 struct ima_event_data event_data = {.iint = &iint,
696 .filename = eventname,
699 struct ima_template_desc *template = NULL;
701 struct ima_digest_data hdr;
702 char digest[IMA_MAX_DIGEST_SIZE];
708 if (!ima_policy_flag)
712 * Both LSM hooks and auxilary based buffer measurements are
713 * based on policy. To avoid code duplication, differentiate
714 * between the LSM hooks and auxilary buffer measurements,
715 * retrieving the policy rule information only for the LSM hook
716 * buffer measurements.
719 security_task_getsecid(current, &secid);
720 action = ima_get_action(NULL, current_cred(), secid, 0, func,
721 &pcr, &template, keyring);
722 if (!(action & IMA_MEASURE))
727 pcr = CONFIG_IMA_MEASURE_PCR_IDX;
730 template = lookup_template_desc("ima-buf");
731 ret = template_desc_init_fields(template->fmt,
733 &(template->num_fields));
735 pr_err("template %s init failed, result: %d\n",
736 (strlen(template->name) ?
737 template->name : template->fmt), ret);
742 iint.ima_hash = &hash.hdr;
743 iint.ima_hash->algo = ima_hash_algo;
744 iint.ima_hash->length = hash_digest_size[ima_hash_algo];
746 ret = ima_calc_buffer_hash(buf, size, iint.ima_hash);
750 ret = ima_alloc_init_template(&event_data, &entry, template);
754 ret = ima_store_template(entry, violation, NULL, buf, pcr);
757 ima_free_template_entry(entry);
764 * ima_kexec_cmdline - measure kexec cmdline boot args
765 * @buf: pointer to buffer
766 * @size: size of buffer
768 * Buffers can only be measured, not appraised.
770 void ima_kexec_cmdline(const void *buf, int size)
772 if (buf && size != 0)
773 process_buffer_measurement(buf, size, "kexec-cmdline",
774 KEXEC_CMDLINE, 0, NULL);
777 static int __init init_ima(void)
781 ima_init_template_list();
782 hash_setup(CONFIG_IMA_DEFAULT_HASH);
785 if (error && strcmp(hash_algo_name[ima_hash_algo],
786 CONFIG_IMA_DEFAULT_HASH) != 0) {
787 pr_info("Allocating %s failed, going to use default hash algorithm %s\n",
788 hash_algo_name[ima_hash_algo], CONFIG_IMA_DEFAULT_HASH);
790 hash_setup(CONFIG_IMA_DEFAULT_HASH);
794 error = register_blocking_lsm_notifier(&ima_lsm_policy_notifier);
796 pr_warn("Couldn't register LSM notifier, error %d\n", error);
799 ima_update_policy_flag();
804 late_initcall(init_ima); /* Start IMA after the TPM is available */