1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2005,2006,2007,2008 IBM Corporation
4  *
5  * Authors:
6  * Serge Hallyn <serue@us.ibm.com>
7  * Reiner Sailer <sailer@watson.ibm.com>
8  * Mimi Zohar <zohar@us.ibm.com>
9  *
10  * File: ima_queue.c
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.
16  */
17 
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 
20 #include <linux/rculist.h>
21 #include <linux/slab.h>
22 #include "ima.h"
23 
24 #define AUDIT_CAUSE_LEN_MAX 32
25 
26 /* pre-allocated array of tpm_digest structures to extend a PCR */
27 static struct tpm_digest *digests;
28 
29 LIST_HEAD(ima_measurements);	/* list of all measurements */
30 #ifdef CONFIG_IMA_KEXEC
31 static unsigned long binary_runtime_size;
32 #else
33 static unsigned long binary_runtime_size = ULONG_MAX;
34 #endif
35 
36 /* key: inode (before secure-hashing a file) */
37 struct ima_h_table ima_htable = {
38 	.len = ATOMIC_LONG_INIT(0),
39 	.violations = ATOMIC_LONG_INIT(0),
40 	.queue[0 ... IMA_MEASURE_HTABLE_SIZE - 1] = HLIST_HEAD_INIT
41 };
42 
43 /* mutex protects atomicity of extending measurement list
44  * and extending the TPM PCR aggregate. Since tpm_extend can take
45  * long (and the tpm driver uses a mutex), we can't use the spinlock.
46  */
47 static DEFINE_MUTEX(ima_extend_list_mutex);
48 
49 /* lookup up the digest value in the hash table, and return the entry */
50 static struct ima_queue_entry *ima_lookup_digest_entry(u8 *digest_value,
51 						       int pcr)
52 {
53 	struct ima_queue_entry *qe, *ret = NULL;
54 	unsigned int key;
55 	int rc;
56 
57 	key = ima_hash_key(digest_value);
58 	rcu_read_lock();
59 	hlist_for_each_entry_rcu(qe, &ima_htable.queue[key], hnext) {
60 		rc = memcmp(qe->entry->digest, digest_value, TPM_DIGEST_SIZE);
61 		if ((rc == 0) && (qe->entry->pcr == pcr)) {
62 			ret = qe;
63 			break;
64 		}
65 	}
66 	rcu_read_unlock();
67 	return ret;
68 }
69 
70 /*
71  * Calculate the memory required for serializing a single
72  * binary_runtime_measurement list entry, which contains a
73  * couple of variable length fields (e.g template name and data).
74  */
75 static int get_binary_runtime_size(struct ima_template_entry *entry)
76 {
77 	int size = 0;
78 
79 	size += sizeof(u32);	/* pcr */
80 	size += sizeof(entry->digest);
81 	size += sizeof(int);	/* template name size field */
82 	size += strlen(entry->template_desc->name);
83 	size += sizeof(entry->template_data_len);
84 	size += entry->template_data_len;
85 	return size;
86 }
87 
88 /* ima_add_template_entry helper function:
89  * - Add template entry to the measurement list and hash table, for
90  *   all entries except those carried across kexec.
91  *
92  * (Called with ima_extend_list_mutex held.)
93  */
94 static int ima_add_digest_entry(struct ima_template_entry *entry,
95 				bool update_htable)
96 {
97 	struct ima_queue_entry *qe;
98 	unsigned int key;
99 
100 	qe = kmalloc(sizeof(*qe), GFP_KERNEL);
101 	if (qe == NULL) {
102 		pr_err("OUT OF MEMORY ERROR creating queue entry\n");
103 		return -ENOMEM;
104 	}
105 	qe->entry = entry;
106 
107 	INIT_LIST_HEAD(&qe->later);
108 	list_add_tail_rcu(&qe->later, &ima_measurements);
109 
110 	atomic_long_inc(&ima_htable.len);
111 	if (update_htable) {
112 		key = ima_hash_key(entry->digest);
113 		hlist_add_head_rcu(&qe->hnext, &ima_htable.queue[key]);
114 	}
115 
116 	if (binary_runtime_size != ULONG_MAX) {
117 		int size;
118 
119 		size = get_binary_runtime_size(entry);
120 		binary_runtime_size = (binary_runtime_size < ULONG_MAX - size) ?
121 		     binary_runtime_size + size : ULONG_MAX;
122 	}
123 	return 0;
124 }
125 
126 /*
127  * Return the amount of memory required for serializing the
128  * entire binary_runtime_measurement list, including the ima_kexec_hdr
129  * structure.
130  */
131 unsigned long ima_get_binary_runtime_size(void)
132 {
133 	if (binary_runtime_size >= (ULONG_MAX - sizeof(struct ima_kexec_hdr)))
134 		return ULONG_MAX;
135 	else
136 		return binary_runtime_size + sizeof(struct ima_kexec_hdr);
137 };
138 
139 static int ima_pcr_extend(const u8 *hash, int pcr)
140 {
141 	int result = 0;
142 	int i;
143 
144 	if (!ima_tpm_chip)
145 		return result;
146 
147 	for (i = 0; i < ima_tpm_chip->nr_allocated_banks; i++)
148 		memcpy(digests[i].digest, hash, TPM_DIGEST_SIZE);
149 
150 	result = tpm_pcr_extend(ima_tpm_chip, pcr, digests);
151 	if (result != 0)
152 		pr_err("Error Communicating to TPM chip, result: %d\n", result);
153 	return result;
154 }
155 
156 /*
157  * Add template entry to the measurement list and hash table, and
158  * extend the pcr.
159  *
160  * On systems which support carrying the IMA measurement list across
161  * kexec, maintain the total memory size required for serializing the
162  * binary_runtime_measurements.
163  */
164 int ima_add_template_entry(struct ima_template_entry *entry, int violation,
165 			   const char *op, struct inode *inode,
166 			   const unsigned char *filename)
167 {
168 	u8 digest[TPM_DIGEST_SIZE];
169 	const char *audit_cause = "hash_added";
170 	char tpm_audit_cause[AUDIT_CAUSE_LEN_MAX];
171 	int audit_info = 1;
172 	int result = 0, tpmresult = 0;
173 
174 	mutex_lock(&ima_extend_list_mutex);
175 	if (!violation) {
176 		memcpy(digest, entry->digest, sizeof(digest));
177 		if (ima_lookup_digest_entry(digest, entry->pcr)) {
178 			audit_cause = "hash_exists";
179 			result = -EEXIST;
180 			goto out;
181 		}
182 	}
183 
184 	result = ima_add_digest_entry(entry, 1);
185 	if (result < 0) {
186 		audit_cause = "ENOMEM";
187 		audit_info = 0;
188 		goto out;
189 	}
190 
191 	if (violation)		/* invalidate pcr */
192 		memset(digest, 0xff, sizeof(digest));
193 
194 	tpmresult = ima_pcr_extend(digest, entry->pcr);
195 	if (tpmresult != 0) {
196 		snprintf(tpm_audit_cause, AUDIT_CAUSE_LEN_MAX, "TPM_error(%d)",
197 			 tpmresult);
198 		audit_cause = tpm_audit_cause;
199 		audit_info = 0;
200 	}
201 out:
202 	mutex_unlock(&ima_extend_list_mutex);
203 	integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
204 			    op, audit_cause, result, audit_info);
205 	return result;
206 }
207 
208 int ima_restore_measurement_entry(struct ima_template_entry *entry)
209 {
210 	int result = 0;
211 
212 	mutex_lock(&ima_extend_list_mutex);
213 	result = ima_add_digest_entry(entry, 0);
214 	mutex_unlock(&ima_extend_list_mutex);
215 	return result;
216 }
217 
218 int __init ima_init_digests(void)
219 {
220 	int i;
221 
222 	if (!ima_tpm_chip)
223 		return 0;
224 
225 	digests = kcalloc(ima_tpm_chip->nr_allocated_banks, sizeof(*digests),
226 			  GFP_NOFS);
227 	if (!digests)
228 		return -ENOMEM;
229 
230 	for (i = 0; i < ima_tpm_chip->nr_allocated_banks; i++)
231 		digests[i].alg_id = ima_tpm_chip->allocated_banks[i].alg_id;
232 
233 	return 0;
234 }
235