treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 441
[linux-block.git] / security / integrity / ima / ima_init.c
CommitLineData
b886d83c 1// SPDX-License-Identifier: GPL-2.0-only
3323eec9
MZ
2/*
3 * Copyright (C) 2005,2006,2007,2008 IBM Corporation
4 *
5 * Authors:
6 * Reiner Sailer <sailer@watson.ibm.com>
7 * Leendert van Doorn <leendert@watson.ibm.com>
8 * Mimi Zohar <zohar@us.ibm.com>
9 *
3323eec9
MZ
10 * File: ima_init.c
11 * initialization and cleanup functions
12 */
20ee451f
JP
13
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
876979c9 16#include <linux/init.h>
3323eec9 17#include <linux/scatterlist.h>
5a0e3ad6 18#include <linux/slab.h>
3323eec9 19#include <linux/err.h>
1525b06d 20
3323eec9
MZ
21#include "ima.h"
22
23/* name for boot aggregate entry */
b2724d58 24static const char boot_aggregate_name[] = "boot_aggregate";
5c2a640a 25struct tpm_chip *ima_tpm_chip;
3323eec9
MZ
26
27/* Add the boot aggregate to the IMA measurement list and extend
28 * the PCR register.
29 *
30 * Calculate the boot aggregate, a SHA1 over tpm registers 0-7,
31 * assuming a TPM chip exists, and zeroes if the TPM chip does not
32 * exist. Add the boot aggregate measurement to the measurement
33 * list and extend the PCR register.
34 *
35 * If a tpm chip does not exist, indicate the core root of trust is
36 * not hardware based by invalidating the aggregate PCR value.
37 * (The aggregate PCR value is invalidated by adding one value to
38 * the measurement list and extending the aggregate PCR value with
39 * a different value.) Violations add a zero entry to the measurement
40 * list and extend the aggregate PCR value with ff...ff's.
41 */
be39ffc2 42static int __init ima_add_boot_aggregate(void)
3323eec9 43{
52a13284
MZ
44 static const char op[] = "add_boot_aggregate";
45 const char *audit_cause = "ENOMEM";
3323eec9 46 struct ima_template_entry *entry;
7bc5f447 47 struct integrity_iint_cache tmp_iint, *iint = &tmp_iint;
23b57419 48 struct ima_event_data event_data = {iint, NULL, boot_aggregate_name,
8d94eb9b 49 NULL, 0, NULL};
3323eec9 50 int result = -ENOMEM;
7bc5f447 51 int violation = 0;
09ef5435
DK
52 struct {
53 struct ima_digest_data hdr;
54 char digest[TPM_DIGEST_SIZE];
55 } hash;
3323eec9 56
7bc5f447
RS
57 memset(iint, 0, sizeof(*iint));
58 memset(&hash, 0, sizeof(hash));
59 iint->ima_hash = &hash.hdr;
60 iint->ima_hash->algo = HASH_ALGO_SHA1;
61 iint->ima_hash->length = SHA1_DIGEST_SIZE;
3323eec9 62
ec403d8e 63 if (ima_tpm_chip) {
09ef5435 64 result = ima_calc_boot_aggregate(&hash.hdr);
3323eec9
MZ
65 if (result < 0) {
66 audit_cause = "hashing_error";
3323eec9
MZ
67 goto err_out;
68 }
69 }
7bc5f447 70
23b57419 71 result = ima_alloc_init_template(&event_data, &entry);
be39ffc2
RS
72 if (result < 0) {
73 audit_cause = "alloc_entry";
74 goto err_out;
75 }
7bc5f447 76
9803d413 77 result = ima_store_template(entry, violation, NULL,
14b1da85
ER
78 boot_aggregate_name,
79 CONFIG_IMA_MEASURE_PCR_IDX);
be39ffc2 80 if (result < 0) {
a7ed7c60 81 ima_free_template_entry(entry);
be39ffc2
RS
82 audit_cause = "store_entry";
83 goto err_out;
84 }
85 return 0;
3323eec9
MZ
86err_out:
87 integrity_audit_msg(AUDIT_INTEGRITY_PCR, NULL, boot_aggregate_name, op,
88 audit_cause, result, 0);
be39ffc2 89 return result;
3323eec9
MZ
90}
91
fd5f4e90
DK
92#ifdef CONFIG_IMA_LOAD_X509
93void __init ima_load_x509(void)
94{
95 int unset_flags = ima_policy_flag & IMA_APPRAISE;
96
97 ima_policy_flag &= ~unset_flags;
a18d0cbf 98 integrity_load_x509(INTEGRITY_KEYRING_IMA, CONFIG_IMA_X509_PATH);
fd5f4e90
DK
99 ima_policy_flag |= unset_flags;
100}
101#endif
102
932995f0 103int __init ima_init(void)
3323eec9 104{
3323eec9
MZ
105 int rc;
106
5c2a640a 107 ima_tpm_chip = tpm_default_chip();
ec403d8e 108 if (!ima_tpm_chip)
5c2a640a 109 pr_info("No TPM chip found, activating TPM-bypass!\n");
3323eec9 110
f4dc3778 111 rc = integrity_init_keyring(INTEGRITY_KEYRING_IMA);
31b70f66
DK
112 if (rc)
113 return rc;
114
76bb28f6
DK
115 rc = ima_init_crypto();
116 if (rc)
117 return rc;
adf53a77
RS
118 rc = ima_init_template();
119 if (rc != 0)
120 return rc;
121
0b6cf6b9 122 /* It can be called before ima_init_digests(), it does not use TPM. */
94c3aac5
MZ
123 ima_load_kexec_buffer();
124
0b6cf6b9
RS
125 rc = ima_init_digests();
126 if (rc != 0)
127 return rc;
be39ffc2
RS
128 rc = ima_add_boot_aggregate(); /* boot aggregate must be first entry */
129 if (rc != 0)
130 return rc;
131
3323eec9 132 ima_init_policy();
bab73937
MZ
133
134 return ima_fs_init();
135}