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