forked from TrenchBoot/landing-zone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_log.c
308 lines (255 loc) · 7.89 KB
/
event_log.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
* Copyright (C) 2020 3mdeb Embedded Systems Consulting
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <boot.h>
#include <tags.h>
#include "tpmlib/tpm.h"
#include "tpmlib/tpm2_constants.h"
static u8 *evtlog_base;
static u8 *ptr_current;
static u8 *limit;
#define HAS_ENOUGH_SPACE(n) ((limit - ptr_current) > (n))
static int log_write(const void *data, unsigned size)
{
if (size >= limit - ptr_current)
return 1;
memcpy(ptr_current, data, size);
ptr_current += size;
return 0;
}
#define EV_NO_ACTION 0x3
#define EV_TYPE_SLAUNCH 0x502
#define HASH_COUNT 2
/* For compatibility with TXT and easier operations */
#define TPM12_EVTLOG_SIGNATURE "TXT Event Container"
typedef struct __packed {
char signature[20];
char reserved[12];
u8 container_ver_major;
u8 container_ver_minor;
u8 pcr_event_ver_major;
u8 pcr_event_ver_minor;
u32 container_size;
u32 pcr_events_offset;
u32 next_event_offset;
/* PCREvents[] */
} tpm12_event_log_header;
typedef struct __packed {
u64 phys_addr;
u32 allocated_event_container_size;
u32 first_record_offset;
u32 next_record_offset;
} txt_event_log_pointer2_1_element;
/* Event log headers */
typedef struct __packed {
char signature[16];
u32 platform_class;
u8 spec_ver_minor;
u8 spec_ver_major;
u8 errata;
u8 uintn_size; /* reserved (must be 0) for 1.21 */
} common_spec_id_ev_t;
typedef struct __packed {
common_spec_id_ev_t c;
u8 vendor_info_size;
tpm12_event_log_header hdr; /* AKA u8 vendor_info[]; */
} tpm12_spec_id_ev_t;
typedef struct __packed {
u32 number_of_algorithms;
/* Hardcode table size so we can use sizeof */
struct {
u16 id;
u16 size;
} digest_sizes[HASH_COUNT];
} tpm20_digest_sizes_t;
typedef struct __packed {
common_spec_id_ev_t c;
tpm20_digest_sizes_t sizes;
u8 vendor_info_size;
txt_event_log_pointer2_1_element el; /* AKA u8 vendor_info[]; */
} tpm20_spec_id_ev_t;
/* Event log entries */
typedef struct __packed {
u32 pcr;
u32 event_type;
u8 digest[20];
u32 event_size;
/* u8 event[]; */
} tpm12_event_t;
typedef struct __packed {
u32 pcr;
u32 event_type;
ev_log_hash_t digests; /* defined in boot.h */
u32 event_size;
/* u8 event[]; */
} tpm20_event_t;
static tpm12_spec_id_ev_t tpm12_id_struct = {
.c.signature = "Spec ID Event00",
.c.spec_ver_minor = 2,
.c.spec_ver_major = 1,
.c.errata = 1,
.vendor_info_size = sizeof(tpm12_event_log_header),
.hdr.signature = TPM12_EVTLOG_SIGNATURE,
.hdr.container_ver_major = 1,
.hdr.container_ver_minor = 0,
.hdr.pcr_event_ver_major = 1,
.hdr.pcr_event_ver_minor = 0,
/*
* HACK: this offset should be relative to the base of Event Log, but TXT
* creates its log starting with the .hdr.signature, not .c.signature.
* Linux kernel sets its evtlog_base to the address of the former one in
* order to use the same code for both of the supported CPU vendors.
*/
.hdr.pcr_events_offset = sizeof(tpm12_event_log_header),
.hdr.next_event_offset = sizeof(tpm12_event_log_header)
};
static tpm20_spec_id_ev_t tpm20_id_struct = {
.c.signature = "Spec ID Event03",
.c.spec_ver_minor = 0,
.c.spec_ver_major = 2,
.c.errata = 0,
.c.uintn_size = 2,
.sizes.number_of_algorithms = HASH_COUNT,
.sizes.digest_sizes[0].id = TPM_ALG_SHA1,
.sizes.digest_sizes[0].size = 20,
.sizes.digest_sizes[1].id = TPM_ALG_SHA256,
.sizes.digest_sizes[1].size = 32,
.vendor_info_size = sizeof(txt_event_log_pointer2_1_element),
.el.first_record_offset = 0,
.el.next_record_offset = sizeof(tpm20_spec_id_ev_t) + sizeof(tpm12_event_t)
};
int log_event_tpm12(u32 pcr, u8 sha1[20], char *event)
{
tpm12_event_t ev;
tpm12_spec_id_ev_t *base = (tpm12_spec_id_ev_t *)
(evtlog_base + sizeof(tpm12_event_t));
ev.event_size = strlen(event);
if (HAS_ENOUGH_SPACE(sizeof(ev) + ev.event_size)) {
ev.pcr = pcr;
ev.event_type = EV_TYPE_SLAUNCH;
memcpy(ev.digest, sha1, 20);
base->hdr.next_event_offset += sizeof(ev) + ev.event_size;
log_write(&ev, sizeof(ev));
return log_write(event, ev.event_size);
}
return 1;
}
int log_event_tpm20(u32 pcr, u8 sha1[20], u8 sha256[32], char *event)
{
tpm20_event_t ev;
tpm20_spec_id_ev_t *base = (tpm20_spec_id_ev_t *)
(evtlog_base + sizeof(tpm12_event_t));
ev.event_size = strlen(event);
if (HAS_ENOUGH_SPACE(sizeof(ev) + ev.event_size)) {
ev.pcr = pcr;
ev.event_type = EV_TYPE_SLAUNCH;
ev.digests.count = 2;
ev.digests.sha1_id = TPM_ALG_SHA1;
memcpy(ev.digests.sha1_hash, sha1, 20);
ev.digests.sha256_id = TPM_ALG_SHA256;
memcpy(ev.digests.sha256_hash, sha256, 32);
base->el.next_record_offset += sizeof(ev) + ev.event_size;
log_write(&ev, sizeof(ev));
return log_write(event, ev.event_size);
}
return 1;
}
int event_log_init(struct tpm *tpm)
{
unsigned int min_size;
struct lz_tag_evtlog *t = next_of_type(&bootloader_data, LZ_TAG_EVENT_LOG);
if (t == NULL || next_of_type(t, LZ_TAG_EVENT_LOG) != NULL)
goto err;
min_size = sizeof (tpm12_event_t);
if (tpm->family == TPM12) {
min_size += sizeof(tpm12_id_struct);
min_size += 2 * sizeof(tpm12_event_t); /* LZ and kernel hashes */
} else if (tpm->family == TPM20) {
min_size += sizeof(tpm20_id_struct);
min_size += 2 * sizeof(tpm20_event_t); /* LZ and kernel hashes */
} else {
goto err;
}
/* Note that min_size does not include tpmXX_event_t.event[] entries */
if (t->size < min_size)
goto err;
ptr_current = evtlog_base = _p(t->address);
limit = _p(t->address + t->size);
/* Check for overflow */
if (ptr_current > limit)
goto err;
/*
* Bootloader controls location and size, so it could force LZ to overwrite
* its code **after** it was measured. Make sure that the Event Log and LZ
* do not overlap before wiping the memory.
*/
if (!(_p(limit) < _p(_start) || _p(_start + SLB_SIZE) < _p(ptr_current)))
goto err;
tpm12_id_struct.hdr.container_size =
tpm20_id_struct.el.allocated_event_container_size =
t->size;
tpm20_id_struct.el.phys_addr = _u(evtlog_base);
memset(ptr_current, 0, t->size);
/* Write log header */
{
tpm12_event_t ev;
ev.pcr = 0;
ev.event_type = EV_NO_ACTION;
memset(ev.digest, 0, 20);
if (tpm->family == TPM12) {
ev.event_size = sizeof(tpm12_id_struct);
} else {
ev.event_size = sizeof(tpm20_id_struct);
}
log_write(&ev, sizeof(ev));
}
if (tpm->family == TPM12) {
log_write(&tpm12_id_struct, sizeof(tpm12_id_struct));
} else {
log_write(&tpm20_id_struct, sizeof(tpm20_id_struct));
}
/* Log what was done by SKINIT */
if (tpm->family == TPM12) {
struct lz_tag_hash *h = next_of_type(&bootloader_data, LZ_TAG_LZ_HASH);
while (h != NULL) {
if (h->algo_id == TPM_ALG_SHA1)
return log_event_tpm12(17, h->digest, "SKINIT");
h = next_of_type(h, LZ_TAG_LZ_HASH);
}
/* No SHA1 hash was passed by a bootloader? */
return 1;
} else {
struct lz_tag_hash *h = next_of_type(&bootloader_data, LZ_TAG_LZ_HASH);
u8 *sha1 = NULL;
u8 *sha256 = NULL;
while (h != NULL) {
if (h->algo_id == TPM_ALG_SHA1)
sha1 = h->digest;
if (h->algo_id == TPM_ALG_SHA256)
sha256 = h->digest;
if (sha1 != NULL && sha256 != NULL)
return log_event_tpm20(17, sha1, sha256, "SKINIT");
h = next_of_type(h, LZ_TAG_LZ_HASH);
}
/* Either SHA1 or SHA256 hash wasn't passed by a bootloader? */
return 1;
}
err:
/* Make sure that further calls to log_write() will fail */
limit = ptr_current;
return 1;
}