Debian Codegraph @

src/boot/measure-smbios.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "efi-efivars.h"
4 #include "efi-log.h"
5 #include "measure.h"
6 #include "measure-smbios.h"
7 #include "smbios.h"
8 #include "tpm2-pcr.h"
9 #include "util.h"
10
11 static void measure_smbios_raw(
12 const void *p,
13 size_t size,
14 uint32_t event_id,
15 const char16_t *description,
16 bool *measured) {
17
18 EFI_STATUS err;
19 bool m = false;
20
21 assert(p);
22 assert(description);
23 assert(measured);
24
25 err = tpm_log_tagged_event(
26 TPM2_PCR_PLATFORM_CONFIG,
27 POINTER_TO_PHYSICAL_ADDRESS(p),
28 size,
29 event_id,
30 description,
31 &m);
32 if (err != EFI_SUCCESS)
33 log_error_status(err, "Unable to measure SMBIOS structure (%ls), ignoring: %m", description);
34
35 *measured = *measured || m;
36 }
37
38 static void measure_smbios_type1(const SmbiosHeader *header, size_t size, bool *measured) {
39 assert(header);
40 assert(measured);
41
42 /* The wake-up type field varies depending on how the machine was powered on (cold boot, resume
43 * from sleep, AC restore, …), which would make the measurement non-reproducible. Hence measure a
44 * copy with that field zeroed out. */
45
46 assert(size >= sizeof(SmbiosTableType1));
47
48 _cleanup_free_ SmbiosTableType1 *copy = xmemdup(header, size);
49 copy->wake_up_type = 0;
50
51 measure_smbios_raw(copy, size, SMBIOS_TYPE1_EVENT_TAG_ID, u"smbios:type1", measured);
52 }
53
54 static bool measure_smbios_object(const SmbiosHeader *header, size_t size, void *userdata) {
55 bool *measured = ASSERT_PTR(userdata);
56
57 switch (header->type) {
58
59 case 1: /* System Information */
60 measure_smbios_type1(header, size, measured);
61 break;
62
63 case 2: /* Baseboard Information */
64 measure_smbios_raw(header, size, SMBIOS_TYPE2_EVENT_TAG_ID, u"smbios:type2", measured);
65 break;
66
67 case 11: /* OEM Strings */
68 measure_smbios_raw(header, size, SMBIOS_TYPE11_EVENT_TAG_ID, u"smbios:type11", measured);
69 break;
70
71 default:
72 break;
73 }
74
75 return true; /* Keep iterating: there may be more than one matching structure (e.g. type 11). */
76 }
77
78 void measure_smbios(void) {
79 bool measured = false;
80
81 if (!tpm_present())
82 return;
83
84 /* If the measurement was already done this boot (e.g. by sd-boot before it chainloaded us), don't
85 * do it again — re-extending PCR 1 would invalidate the value. */
86 if (efivar_get_raw(MAKE_GUID_PTR(LOADER), u"LoaderPcrSMBIOS", /* ret_data= */ NULL, /* ret_size= */ NULL) == EFI_SUCCESS)
87 return;
88
89 /* Measure SMBIOS type 1 (system information), type 2 (baseboard information) and type 11 (OEM
90 * strings) into PCR 1, in a single pass over the SMBIOS table. */
91 smbios_foreach(measure_smbios_object, &measured);
92
93 /* If we measured something, tell the OS which PCR we used (and suppress a second pass). */
94 if (measured)
95 (void) efivar_set_uint64_str16(MAKE_GUID_PTR(LOADER), u"LoaderPcrSMBIOS", TPM2_PCR_PLATFORM_CONFIG, /* flags= */ 0);
96 }