1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2005,2006,2007,2008 IBM Corporation
11 * Implements queues that store template measurements and
12 * maintains aggregate over the stored measurements
13 * in the pre-configured TPM PCR (if available).
14 * The measurement list is append-only. No entry is
15 * ever removed or changed during the boot-cycle.
18 #include <linux/rculist.h>
19 #include <linux/reboot.h>
20 #include <linux/slab.h>
23 #define AUDIT_CAUSE_LEN_MAX 32
25 /* pre-allocated array of tpm_digest structures to extend a PCR */
26 static struct tpm_digest *digests;
28 LIST_HEAD(ima_measurements); /* list of all measurements */
29 #ifdef CONFIG_IMA_KEXEC
30 static unsigned long binary_runtime_size;
32 static unsigned long binary_runtime_size = ULONG_MAX;
35 /* key: inode (before secure-hashing a file) */
36 struct ima_h_table ima_htable = {
37 .len = ATOMIC_LONG_INIT(0),
38 .violations = ATOMIC_LONG_INIT(0),
39 .queue[0 ... IMA_MEASURE_HTABLE_SIZE - 1] = HLIST_HEAD_INIT
42 /* mutex protects atomicity of extending measurement list
43 * and extending the TPM PCR aggregate. Since tpm_extend can take
44 * long (and the tpm driver uses a mutex), we can't use the spinlock.
46 static DEFINE_MUTEX(ima_extend_list_mutex);
49 * Used internally by the kernel to suspend measurements.
50 * Protected by ima_extend_list_mutex.
52 static bool ima_measurements_suspended;
54 /* lookup up the digest value in the hash table, and return the entry */
55 static struct ima_queue_entry *ima_lookup_digest_entry(u8 *digest_value,
58 struct ima_queue_entry *qe, *ret = NULL;
62 key = ima_hash_key(digest_value);
64 hlist_for_each_entry_rcu(qe, &ima_htable.queue[key], hnext) {
65 rc = memcmp(qe->entry->digests[ima_hash_algo_idx].digest,
66 digest_value, hash_digest_size[ima_hash_algo]);
67 if ((rc == 0) && (qe->entry->pcr == pcr)) {
77 * Calculate the memory required for serializing a single
78 * binary_runtime_measurement list entry, which contains a
79 * couple of variable length fields (e.g template name and data).
81 static int get_binary_runtime_size(struct ima_template_entry *entry)
85 size += sizeof(u32); /* pcr */
86 size += TPM_DIGEST_SIZE;
87 size += sizeof(int); /* template name size field */
88 size += strlen(entry->template_desc->name);
89 size += sizeof(entry->template_data_len);
90 size += entry->template_data_len;
94 /* ima_add_template_entry helper function:
95 * - Add template entry to the measurement list and hash table, for
96 * all entries except those carried across kexec.
98 * (Called with ima_extend_list_mutex held.)
100 static int ima_add_digest_entry(struct ima_template_entry *entry,
103 struct ima_queue_entry *qe;
106 qe = kmalloc(sizeof(*qe), GFP_KERNEL);
108 pr_err("OUT OF MEMORY ERROR creating queue entry\n");
113 INIT_LIST_HEAD(&qe->later);
114 list_add_tail_rcu(&qe->later, &ima_measurements);
116 atomic_long_inc(&ima_htable.len);
118 key = ima_hash_key(entry->digests[ima_hash_algo_idx].digest);
119 hlist_add_head_rcu(&qe->hnext, &ima_htable.queue[key]);
122 if (binary_runtime_size != ULONG_MAX) {
125 size = get_binary_runtime_size(entry);
126 binary_runtime_size = (binary_runtime_size < ULONG_MAX - size) ?
127 binary_runtime_size + size : ULONG_MAX;
133 * Return the amount of memory required for serializing the
134 * entire binary_runtime_measurement list, including the ima_kexec_hdr
137 unsigned long ima_get_binary_runtime_size(void)
139 if (binary_runtime_size >= (ULONG_MAX - sizeof(struct ima_kexec_hdr)))
142 return binary_runtime_size + sizeof(struct ima_kexec_hdr);
145 static int ima_pcr_extend(struct tpm_digest *digests_arg, int pcr)
152 result = tpm_pcr_extend(ima_tpm_chip, pcr, digests_arg);
154 pr_err("Error Communicating to TPM chip, result: %d\n", result);
159 * Add template entry to the measurement list and hash table, and
162 * On systems which support carrying the IMA measurement list across
163 * kexec, maintain the total memory size required for serializing the
164 * binary_runtime_measurements.
166 int ima_add_template_entry(struct ima_template_entry *entry, int violation,
167 const char *op, struct inode *inode,
168 const unsigned char *filename)
170 u8 *digest = entry->digests[ima_hash_algo_idx].digest;
171 struct tpm_digest *digests_arg = entry->digests;
172 const char *audit_cause = "hash_added";
173 char tpm_audit_cause[AUDIT_CAUSE_LEN_MAX];
175 int result = 0, tpmresult = 0;
177 mutex_lock(&ima_extend_list_mutex);
180 * Avoid appending to the measurement log when the TPM subsystem has
181 * been shut down while preparing for system reboot.
183 if (ima_measurements_suspended) {
184 audit_cause = "measurements_suspended";
190 if (!violation && !IS_ENABLED(CONFIG_IMA_DISABLE_HTABLE)) {
191 if (ima_lookup_digest_entry(digest, entry->pcr)) {
192 audit_cause = "hash_exists";
198 result = ima_add_digest_entry(entry,
199 !IS_ENABLED(CONFIG_IMA_DISABLE_HTABLE));
201 audit_cause = "ENOMEM";
206 if (violation) /* invalidate pcr */
207 digests_arg = digests;
209 tpmresult = ima_pcr_extend(digests_arg, entry->pcr);
210 if (tpmresult != 0) {
211 snprintf(tpm_audit_cause, AUDIT_CAUSE_LEN_MAX, "TPM_error(%d)",
213 audit_cause = tpm_audit_cause;
217 mutex_unlock(&ima_extend_list_mutex);
218 integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
219 op, audit_cause, result, audit_info);
223 int ima_restore_measurement_entry(struct ima_template_entry *entry)
227 mutex_lock(&ima_extend_list_mutex);
228 result = ima_add_digest_entry(entry, 0);
229 mutex_unlock(&ima_extend_list_mutex);
233 static void ima_measurements_suspend(void)
235 mutex_lock(&ima_extend_list_mutex);
236 ima_measurements_suspended = true;
237 mutex_unlock(&ima_extend_list_mutex);
240 static int ima_reboot_notifier(struct notifier_block *nb,
241 unsigned long action,
244 ima_measurements_suspend();
249 static struct notifier_block ima_reboot_nb = {
250 .notifier_call = ima_reboot_notifier,
253 void __init ima_init_reboot_notifier(void)
255 register_reboot_notifier(&ima_reboot_nb);
258 int __init ima_init_digests(void)
267 digests = kcalloc(ima_tpm_chip->nr_allocated_banks, sizeof(*digests),
272 for (i = 0; i < ima_tpm_chip->nr_allocated_banks; i++) {
273 digests[i].alg_id = ima_tpm_chip->allocated_banks[i].alg_id;
274 digest_size = ima_tpm_chip->allocated_banks[i].digest_size;
275 crypto_id = ima_tpm_chip->allocated_banks[i].crypto_id;
277 /* for unmapped TPM algorithms digest is still a padded SHA1 */
278 if (crypto_id == HASH_ALGO__LAST)
279 digest_size = SHA1_DIGEST_SIZE;
281 memset(digests[i].digest, 0xff, digest_size);