1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
20#include <linux/module.h>
21#include <linux/scatterlist.h>
22#include <linux/slab.h>
23#include <linux/err.h>
24
25#include "ima.h"
26
27
28static const char *boot_aggregate_name = "boot_aggregate";
29struct tpm_chip *ima_tpm_chip;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46static int __init ima_add_boot_aggregate(void)
47{
48 static const char op[] = "add_boot_aggregate";
49 const char *audit_cause = "ENOMEM";
50 struct ima_template_entry *entry;
51 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};
54 int result = -ENOMEM;
55 int violation = 0;
56 struct {
57 struct ima_digest_data hdr;
58 char digest[TPM_DIGEST_SIZE];
59 } hash;
60
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;
66
67 if (ima_tpm_chip) {
68 result = ima_calc_boot_aggregate(&hash.hdr);
69 if (result < 0) {
70 audit_cause = "hashing_error";
71 goto err_out;
72 }
73 }
74
75 result = ima_alloc_init_template(&event_data, &entry);
76 if (result < 0) {
77 audit_cause = "alloc_entry";
78 goto err_out;
79 }
80
81 result = ima_store_template(entry, violation, NULL,
82 boot_aggregate_name,
83 CONFIG_IMA_MEASURE_PCR_IDX);
84 if (result < 0) {
85 ima_free_template_entry(entry);
86 audit_cause = "store_entry";
87 goto err_out;
88 }
89 return 0;
90err_out:
91 integrity_audit_msg(AUDIT_INTEGRITY_PCR, NULL, boot_aggregate_name, op,
92 audit_cause, result, 0);
93 return result;
94}
95
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;
102 integrity_load_x509(INTEGRITY_KEYRING_IMA, CONFIG_IMA_X509_PATH);
103 ima_policy_flag |= unset_flags;
104}
105#endif
106
107int __init ima_init(void)
108{
109 int rc;
110
111 ima_tpm_chip = tpm_default_chip();
112 if (!ima_tpm_chip)
113 pr_info("No TPM chip found, activating TPM-bypass!\n");
114
115 rc = integrity_init_keyring(INTEGRITY_KEYRING_IMA);
116 if (rc)
117 return rc;
118
119 rc = ima_init_crypto();
120 if (rc)
121 return rc;
122 rc = ima_init_template();
123 if (rc != 0)
124 return rc;
125
126 ima_load_kexec_buffer();
127
128 rc = ima_add_boot_aggregate();
129 if (rc != 0)
130 return rc;
131
132 ima_init_policy();
133
134 return ima_fs_init();
135}
136