Debian Codegraph @

src/boot/measure.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #if ENABLE_TPM
4
5 #include "efi-log.h"
6 #include "measure.h"
7 #include "proto/cc-measurement.h"
8 #include "proto/tcg.h"
9 #include "tpm2-pcr.h"
10 #include "util.h"
11
12 static EFI_STATUS tpm2_measure_to_pcr_and_tagged_event_log(
13 EFI_TCG2_PROTOCOL *tcg,
14 uint32_t pcrindex,
15 EFI_PHYSICAL_ADDRESS buffer,
16 uint64_t buffer_size,
17 uint32_t event_id,
18 const char16_t *description) {
19
20 _cleanup_free_ union event {
21 EFI_TCG2_EVENT tcg_event;
22 EFI_TCG2_TAGGED_EVENT tcg_tagged_event;
23 } *event = NULL;
24 size_t desc_len, event_size;
25
26 assert(tcg);
27 assert(description);
28
29 /* New style stuff we log as EV_EVENT_TAG with a recognizable event tag. */
30
31 desc_len = strsize16(description);
32 event_size = offsetof(EFI_TCG2_TAGGED_EVENT, Event) + desc_len;
33
34 event = xmalloc(event_size);
35 event->tcg_tagged_event = (EFI_TCG2_TAGGED_EVENT) {
36 .Size = event_size,
37 .Header.HeaderSize = sizeof(EFI_TCG2_EVENT_HEADER),
38 .Header.HeaderVersion = EFI_TCG2_EVENT_HEADER_VERSION,
39 .Header.PCRIndex = pcrindex,
40 .Header.EventType = EV_EVENT_TAG,
41 .EventId = event_id,
42 .EventSize = desc_len,
43 };
44 memcpy(event->tcg_tagged_event.Event, description, desc_len);
45
46 return tcg->HashLogExtendEvent(
47 tcg,
48 0,
49 buffer, buffer_size,
50 &event->tcg_event);
51 }
52
53 static EFI_STATUS tpm2_measure_to_pcr_and_ipl_event_log(
54 EFI_TCG2_PROTOCOL *tcg,
55 uint32_t pcrindex,
56 EFI_PHYSICAL_ADDRESS buffer,
57 uint64_t buffer_size,
58 const char16_t *description) {
59
60 _cleanup_free_ EFI_TCG2_EVENT *tcg_event = NULL;
61 size_t desc_len;
62
63 assert(tcg);
64 assert(description);
65
66 /* We record older stuff as EV_IPL. Which sucks, because it makes it hard to recognize from the event
67 * log which of the events are ours. Measurement logs are kinda API hence this is hard to change for
68 * existing, established events. But for future additions, let's use EV_EVENT_TAG instead, with a tag
69 * of our choosing that makes clear what precisely we are measuring here. See above. */
70
71 desc_len = strsize16(description);
72 tcg_event = xmalloc(offsetof(EFI_TCG2_EVENT, Event) + desc_len);
73 *tcg_event = (EFI_TCG2_EVENT) {
74 .Size = offsetof(EFI_TCG2_EVENT, Event) + desc_len,
75 .Header.HeaderSize = sizeof(EFI_TCG2_EVENT_HEADER),
76 .Header.HeaderVersion = EFI_TCG2_EVENT_HEADER_VERSION,
77 .Header.PCRIndex = pcrindex,
78 .Header.EventType = EV_IPL,
79 };
80
81 memcpy(tcg_event->Event, description, desc_len);
82
83 return tcg->HashLogExtendEvent(
84 tcg,
85 0,
86 buffer, buffer_size,
87 tcg_event);
88 }
89
90 static EFI_STATUS cc_measure_to_mr_and_ipl_event_log(
91 EFI_CC_MEASUREMENT_PROTOCOL *cc,
92 uint32_t pcrindex,
93 EFI_PHYSICAL_ADDRESS buffer,
94 uint64_t buffer_size,
95 const char16_t *description) {
96
97 _cleanup_free_ EFI_CC_EVENT *event = NULL;
98 uint32_t mr;
99 EFI_STATUS err;
100 size_t desc_len;
101
102 assert(cc);
103 assert(description);
104
105 /* MapPcrToMrIndex service provides callers information on
106 * how the TPM PCR registers are mapped to the CC measurement
107 * registers (MR) in the vendor implementation. */
108 err = cc->MapPcrToMrIndex(cc, pcrindex, &mr);
109 if (err != EFI_SUCCESS)
110 return EFI_NOT_FOUND;
111
112 desc_len = strsize16(description);
113 event = xmalloc(offsetof(EFI_CC_EVENT, Event) + desc_len);
114 *event = (EFI_CC_EVENT) {
115 .Size = offsetof(EFI_CC_EVENT, Event) + desc_len,
116 .Header.HeaderSize = sizeof(EFI_CC_EVENT_HEADER),
117 .Header.HeaderVersion = EFI_CC_EVENT_HEADER_VERSION,
118 .Header.MrIndex = mr,
119 .Header.EventType = EV_IPL,
120 };
121
122 memcpy(event->Event, description, desc_len);
123
124 return cc->HashLogExtendEvent(
125 cc,
126 0,
127 buffer,
128 buffer_size,
129 event);
130 }
131
132 static EFI_CC_MEASUREMENT_PROTOCOL *cc_interface_check(void) {
133 EFI_CC_BOOT_SERVICE_CAPABILITY capability = {
134 .Size = sizeof(capability),
135 };
136 EFI_STATUS err;
137 EFI_CC_MEASUREMENT_PROTOCOL *cc;
138
139 err = BS->LocateProtocol(MAKE_GUID_PTR(EFI_CC_MEASUREMENT_PROTOCOL), NULL, (void **) &cc);
140 if (err != EFI_SUCCESS)
141 return NULL;
142
143 err = cc->GetCapability(cc, &capability);
144 if (err != EFI_SUCCESS)
145 return NULL;
146
147 if (!(capability.SupportedEventLogs & EFI_CC_EVENT_LOG_FORMAT_TCG_2))
148 return NULL;
149
150 return cc;
151 }
152
153 static EFI_TCG2_PROTOCOL* tcg2_interface_check(EFI_TCG2_VERSION *ret_version) {
154 EFI_STATUS err;
155
156 EFI_TCG2_PROTOCOL *tcg;
157 err = BS->LocateProtocol(MAKE_GUID_PTR(EFI_TCG2_PROTOCOL), NULL, (void **) &tcg);
158 if (err != EFI_SUCCESS)
159 return NULL;
160
161 EFI_TCG2_BOOT_SERVICE_CAPABILITY capability = {
162 .Size = sizeof(capability),
163 };
164 err = tcg->GetCapability(tcg, &capability);
165 if (err != EFI_SUCCESS)
166 return NULL;
167
168 assert(capability.Size >= endoffsetof_field(EFI_TCG2_BOOT_SERVICE_CAPABILITY, Size));
169
170 /* This is a paranoia check, given these fields existed from day one of the spec. But the spec also
171 * suggests checking the structure size before accessing any fields, hence let's do so, for extra
172 * paranoia. */
173 if (capability.Size < CONST_MAX(endoffsetof_field(EFI_TCG2_BOOT_SERVICE_CAPABILITY, TPMPresentFlag),
174 endoffsetof_field(EFI_TCG2_BOOT_SERVICE_CAPABILITY, ProtocolVersion)))
175 return NULL;
176
177 if (!capability.TPMPresentFlag)
178 return NULL;
179
180 if (ret_version)
181 *ret_version = capability.ProtocolVersion;
182
183 return tcg;
184 }
185
186 bool tpm_present(void) {
187 return tcg2_interface_check(/* ret_version= */ NULL);
188 }
189
190 uint32_t tpm_get_active_pcr_banks(void) {
191 EFI_STATUS err;
192
193 EFI_TCG2_PROTOCOL *tpm2;
194 EFI_TCG2_VERSION version;
195 tpm2 = tcg2_interface_check(&version);
196 if (!tpm2)
197 return 0;
198
199 /* GetActivePcrBanks() was added only in version 1.1 of the spec */
200 if (version.Major < 1 || (version.Major == 1 && version.Minor < 1)) {
201 log_debug("TCG protocol too old for GetActivePcrBanks(), returning wildcard bank information.");
202 return UINT32_MAX;
203 }
204
205 uint32_t active_pcr_banks = 0;
206 err = tpm2->GetActivePcrBanks(tpm2, &active_pcr_banks);
207 if (err != EFI_SUCCESS) {
208 log_warning_status(err, "Failed to get TPM2 active PCR banks, assuming none: %m");
209 return 0;
210 }
211
212 return active_pcr_banks;
213 }
214
215 static EFI_STATUS tcg2_log_ipl_event(uint32_t pcrindex, EFI_PHYSICAL_ADDRESS buffer, size_t buffer_size, const char16_t *description, bool *ret_measured) {
216 EFI_TCG2_PROTOCOL *tpm2;
217 EFI_STATUS err = EFI_SUCCESS;
218
219 assert(ret_measured);
220
221 tpm2 = tcg2_interface_check(/* ret_version= */ NULL);
222 if (!tpm2) {
223 *ret_measured = false;
224 return EFI_SUCCESS;
225 }
226
227 err = tpm2_measure_to_pcr_and_ipl_event_log(tpm2, pcrindex, buffer, buffer_size, description);
228 if (err != EFI_SUCCESS)
229 return err;
230
231 *ret_measured = true;
232 return EFI_SUCCESS;
233 }
234
235 static EFI_STATUS tcg2_log_tagged_event(
236 uint32_t pcrindex,
237 EFI_PHYSICAL_ADDRESS buffer,
238 size_t buffer_size,
239 uint32_t event_id,
240 const char16_t *description,
241 bool *ret_measured) {
242
243 EFI_TCG2_PROTOCOL *tpm2;
244 EFI_STATUS err = EFI_SUCCESS;
245
246 assert(ret_measured);
247
248 tpm2 = tcg2_interface_check(/* ret_version= */ NULL);
249 if (!tpm2) {
250 *ret_measured = false;
251 return EFI_SUCCESS;
252 }
253
254 err = tpm2_measure_to_pcr_and_tagged_event_log(tpm2, pcrindex, buffer, buffer_size, event_id, description);
255 if (err != EFI_SUCCESS)
256 return err;
257
258 *ret_measured = true;
259 return EFI_SUCCESS;
260 }
261
262 static EFI_STATUS cc_log_event(uint32_t pcrindex, EFI_PHYSICAL_ADDRESS buffer, size_t buffer_size, const char16_t *description, bool *ret_measured) {
263 EFI_CC_MEASUREMENT_PROTOCOL *cc;
264 EFI_STATUS err = EFI_SUCCESS;
265
266 assert(ret_measured);
267
268 cc = cc_interface_check();
269 if (!cc) {
270 *ret_measured = false;
271 return EFI_SUCCESS;
272 }
273
274 err = cc_measure_to_mr_and_ipl_event_log(cc, pcrindex, buffer, buffer_size, description);
275 if (err != EFI_SUCCESS)
276 return err;
277
278 *ret_measured = true;
279 return EFI_SUCCESS;
280 }
281
282 EFI_STATUS tpm_log_ipl_event(uint32_t pcrindex, EFI_PHYSICAL_ADDRESS buffer, size_t buffer_size, const char16_t *description, bool *ret_measured) {
283 EFI_STATUS err;
284 bool tpm_ret_measured, cc_ret_measured;
285
286 assert(description || pcrindex == UINT32_MAX);
287
288 /* If EFI_SUCCESS is returned, will initialize ret_measured to true if we actually measured
289 * something, or false if measurement was turned off. */
290
291 if (pcrindex == UINT32_MAX) { /* PCR disabled? */
292 if (ret_measured)
293 *ret_measured = false;
294
295 return EFI_SUCCESS;
296 }
297
298 /* Measure into both CC and TPM if both are available to avoid a problem like CVE-2021-42299 */
299 err = cc_log_event(pcrindex, buffer, buffer_size, description, &cc_ret_measured);
300 if (err != EFI_SUCCESS)
301 return err;
302
303 err = tcg2_log_ipl_event(pcrindex, buffer, buffer_size, description, &tpm_ret_measured);
304 if (err != EFI_SUCCESS)
305 return err;
306
307 if (ret_measured)
308 *ret_measured = tpm_ret_measured || cc_ret_measured;
309
310 return EFI_SUCCESS;
311 }
312
313 EFI_STATUS tpm_log_tagged_event(
314 uint32_t pcrindex,
315 EFI_PHYSICAL_ADDRESS buffer,
316 size_t buffer_size,
317 uint32_t event_id,
318 const char16_t *description,
319 bool *ret_measured) {
320
321 EFI_STATUS err;
322 bool tpm_ret_measured, cc_ret_measured;
323
324 assert(description || pcrindex == UINT32_MAX);
325 assert(event_id > 0);
326
327 /* If EFI_SUCCESS is returned, will initialize ret_measured to true if we actually measured
328 * something, or false if measurement was turned off. */
329
330 if (pcrindex == UINT32_MAX) { /* PCR disabled? */
331 if (ret_measured)
332 *ret_measured = false;
333
334 return EFI_SUCCESS;
335 }
336
337 /* Measure into both CC and TPM if both are available to avoid a problem like CVE-2021-42299.
338 * The CC protocol has no tagged-event concept, hence use the IPL event log there. */
339 err = cc_log_event(pcrindex, buffer, buffer_size, description, &cc_ret_measured);
340 if (err != EFI_SUCCESS)
341 return err;
342
343 err = tcg2_log_tagged_event(pcrindex, buffer, buffer_size, event_id, description, &tpm_ret_measured);
344 if (err != EFI_SUCCESS)
345 return err;
346
347 if (ret_measured)
348 *ret_measured = tpm_ret_measured || cc_ret_measured;
349
350 return EFI_SUCCESS;
351 }
352
353 EFI_STATUS tpm_log_ipl_event_ascii(uint32_t pcrindex, EFI_PHYSICAL_ADDRESS buffer, size_t buffer_size, const char *description, bool *ret_measured) {
354 _cleanup_free_ char16_t *c = NULL;
355
356 if (description)
357 c = xstr8_to_16(description);
358
359 return tpm_log_ipl_event(pcrindex, buffer, buffer_size, c, ret_measured);
360 }
361
362 EFI_STATUS tpm_log_load_options(const char16_t *load_options, bool *ret_measured) {
363 EFI_STATUS err;
364
365 /* Measures a load options string into the TPM2, i.e. the kernel command line */
366
367 err = tpm_log_ipl_event(
368 TPM2_PCR_KERNEL_CONFIG,
369 POINTER_TO_PHYSICAL_ADDRESS(load_options),
370 strsize16(load_options),
371 load_options,
372 ret_measured);
373 if (err != EFI_SUCCESS)
374 return log_error_status(
375 err,
376 "Unable to add load options (i.e. kernel command) line measurement to PCR %i: %m",
377 TPM2_PCR_KERNEL_CONFIG);
378
379 return EFI_SUCCESS;
380 }
381
382 #endif