blob: a94177042eaa0fc615d2e2a1761bf192f54aa070 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
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 *
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000010 * File: ima_init.c
11 * initialization and cleanup functions
12 */
13
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
David Brazdil0f672f62019-12-10 10:32:29 +000016#include <linux/init.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000017#include <linux/scatterlist.h>
18#include <linux/slab.h>
19#include <linux/err.h>
20
21#include "ima.h"
22
23/* name for boot aggregate entry */
Olivier Deprez0e641232021-09-23 10:07:05 +020024const char boot_aggregate_name[] = "boot_aggregate";
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000025struct tpm_chip *ima_tpm_chip;
26
27/* Add the boot aggregate to the IMA measurement list and extend
28 * the PCR register.
29 *
Olivier Deprez0e641232021-09-23 10:07:05 +020030 * Calculate the boot aggregate, a hash over tpm registers 0-7,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000031 * 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 */
42static int __init ima_add_boot_aggregate(void)
43{
44 static const char op[] = "add_boot_aggregate";
45 const char *audit_cause = "ENOMEM";
46 struct ima_template_entry *entry;
47 struct integrity_iint_cache tmp_iint, *iint = &tmp_iint;
David Brazdil0f672f62019-12-10 10:32:29 +000048 struct ima_event_data event_data = { .iint = iint,
49 .filename = boot_aggregate_name };
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000050 int result = -ENOMEM;
51 int violation = 0;
52 struct {
53 struct ima_digest_data hdr;
Olivier Deprez0e641232021-09-23 10:07:05 +020054 char digest[TPM_MAX_DIGEST_SIZE];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000055 } hash;
56
57 memset(iint, 0, sizeof(*iint));
58 memset(&hash, 0, sizeof(hash));
59 iint->ima_hash = &hash.hdr;
Olivier Deprez0e641232021-09-23 10:07:05 +020060 iint->ima_hash->algo = ima_hash_algo;
61 iint->ima_hash->length = hash_digest_size[ima_hash_algo];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000062
Olivier Deprez0e641232021-09-23 10:07:05 +020063 /*
64 * With TPM 2.0 hash agility, TPM chips could support multiple TPM
65 * PCR banks, allowing firmware to configure and enable different
66 * banks. The SHA1 bank is not necessarily enabled.
67 *
68 * Use the same hash algorithm for reading the TPM PCRs as for
69 * calculating the boot aggregate digest. Preference is given to
70 * the configured IMA default hash algorithm. Otherwise, use the
71 * TCG required banks - SHA256 for TPM 2.0, SHA1 for TPM 1.2.
72 * Ultimately select SHA1 also for TPM 2.0 if the SHA256 PCR bank
73 * is not found.
74 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000075 if (ima_tpm_chip) {
76 result = ima_calc_boot_aggregate(&hash.hdr);
77 if (result < 0) {
78 audit_cause = "hashing_error";
79 goto err_out;
80 }
81 }
82
David Brazdil0f672f62019-12-10 10:32:29 +000083 result = ima_alloc_init_template(&event_data, &entry, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000084 if (result < 0) {
85 audit_cause = "alloc_entry";
86 goto err_out;
87 }
88
89 result = ima_store_template(entry, violation, NULL,
90 boot_aggregate_name,
91 CONFIG_IMA_MEASURE_PCR_IDX);
92 if (result < 0) {
93 ima_free_template_entry(entry);
94 audit_cause = "store_entry";
95 goto err_out;
96 }
97 return 0;
98err_out:
99 integrity_audit_msg(AUDIT_INTEGRITY_PCR, NULL, boot_aggregate_name, op,
100 audit_cause, result, 0);
101 return result;
102}
103
104#ifdef CONFIG_IMA_LOAD_X509
105void __init ima_load_x509(void)
106{
107 int unset_flags = ima_policy_flag & IMA_APPRAISE;
108
109 ima_policy_flag &= ~unset_flags;
110 integrity_load_x509(INTEGRITY_KEYRING_IMA, CONFIG_IMA_X509_PATH);
111 ima_policy_flag |= unset_flags;
112}
113#endif
114
115int __init ima_init(void)
116{
117 int rc;
118
119 ima_tpm_chip = tpm_default_chip();
120 if (!ima_tpm_chip)
121 pr_info("No TPM chip found, activating TPM-bypass!\n");
122
123 rc = integrity_init_keyring(INTEGRITY_KEYRING_IMA);
124 if (rc)
125 return rc;
126
127 rc = ima_init_crypto();
128 if (rc)
129 return rc;
130 rc = ima_init_template();
131 if (rc != 0)
132 return rc;
133
David Brazdil0f672f62019-12-10 10:32:29 +0000134 /* It can be called before ima_init_digests(), it does not use TPM. */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000135 ima_load_kexec_buffer();
136
David Brazdil0f672f62019-12-10 10:32:29 +0000137 rc = ima_init_digests();
138 if (rc != 0)
139 return rc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000140 rc = ima_add_boot_aggregate(); /* boot aggregate must be first entry */
141 if (rc != 0)
142 return rc;
143
144 ima_init_policy();
145
146 return ima_fs_init();
147}