hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/security/integrity/ima/ima_init.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Copyright (C) 2005,2006,2007,2008 IBM Corporation
34 *
....@@ -6,18 +7,11 @@
67 * Leendert van Doorn <leendert@watson.ibm.com>
78 * Mimi Zohar <zohar@us.ibm.com>
89 *
9
- * This program is free software; you can redistribute it and/or
10
- * modify it under the terms of the GNU General Public License as
11
- * published by the Free Software Foundation, version 2 of the
12
- * License.
13
- *
1410 * File: ima_init.c
1511 * initialization and cleanup functions
1612 */
1713
18
-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
-
20
-#include <linux/module.h>
14
+#include <linux/init.h>
2115 #include <linux/scatterlist.h>
2216 #include <linux/slab.h>
2317 #include <linux/err.h>
....@@ -31,7 +25,7 @@
3125 /* Add the boot aggregate to the IMA measurement list and extend
3226 * the PCR register.
3327 *
34
- * Calculate the boot aggregate, a SHA1 over tpm registers 0-7,
28
+ * Calculate the boot aggregate, a hash over tpm registers 0-7,
3529 * assuming a TPM chip exists, and zeroes if the TPM chip does not
3630 * exist. Add the boot aggregate measurement to the measurement
3731 * list and extend the PCR register.
....@@ -49,21 +43,33 @@
4943 const char *audit_cause = "ENOMEM";
5044 struct ima_template_entry *entry;
5145 struct integrity_iint_cache tmp_iint, *iint = &tmp_iint;
52
- struct ima_event_data event_data = {iint, NULL, boot_aggregate_name,
53
- NULL, 0, NULL};
46
+ struct ima_event_data event_data = { .iint = iint,
47
+ .filename = boot_aggregate_name };
5448 int result = -ENOMEM;
5549 int violation = 0;
5650 struct {
5751 struct ima_digest_data hdr;
58
- char digest[TPM_DIGEST_SIZE];
52
+ char digest[TPM_MAX_DIGEST_SIZE];
5953 } hash;
6054
6155 memset(iint, 0, sizeof(*iint));
6256 memset(&hash, 0, sizeof(hash));
6357 iint->ima_hash = &hash.hdr;
64
- iint->ima_hash->algo = HASH_ALGO_SHA1;
65
- iint->ima_hash->length = SHA1_DIGEST_SIZE;
58
+ iint->ima_hash->algo = ima_hash_algo;
59
+ iint->ima_hash->length = hash_digest_size[ima_hash_algo];
6660
61
+ /*
62
+ * With TPM 2.0 hash agility, TPM chips could support multiple TPM
63
+ * PCR banks, allowing firmware to configure and enable different
64
+ * banks. The SHA1 bank is not necessarily enabled.
65
+ *
66
+ * Use the same hash algorithm for reading the TPM PCRs as for
67
+ * calculating the boot aggregate digest. Preference is given to
68
+ * the configured IMA default hash algorithm. Otherwise, use the
69
+ * TCG required banks - SHA256 for TPM 2.0, SHA1 for TPM 1.2.
70
+ * Ultimately select SHA1 also for TPM 2.0 if the SHA256 PCR bank
71
+ * is not found.
72
+ */
6773 if (ima_tpm_chip) {
6874 result = ima_calc_boot_aggregate(&hash.hdr);
6975 if (result < 0) {
....@@ -72,7 +78,7 @@
7278 }
7379 }
7480
75
- result = ima_alloc_init_template(&event_data, &entry);
81
+ result = ima_alloc_init_template(&event_data, &entry, NULL);
7682 if (result < 0) {
7783 audit_cause = "alloc_entry";
7884 goto err_out;
....@@ -123,13 +129,23 @@
123129 if (rc != 0)
124130 return rc;
125131
132
+ /* It can be called before ima_init_digests(), it does not use TPM. */
126133 ima_load_kexec_buffer();
127134
135
+ rc = ima_init_digests();
136
+ if (rc != 0)
137
+ return rc;
128138 rc = ima_add_boot_aggregate(); /* boot aggregate must be first entry */
129139 if (rc != 0)
130140 return rc;
131141
132142 ima_init_policy();
133143
134
- return ima_fs_init();
144
+ rc = ima_fs_init();
145
+ if (rc != 0)
146
+ return rc;
147
+
148
+ ima_init_key_queue();
149
+
150
+ return rc;
135151 }