]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/boot/efi/measure.c
sd-boot: put hashed kernel command line in a PCR of the TPM
[thirdparty/systemd.git] / src / boot / efi / measure.c
1 /*
2 * This program is free software; you can redistribute it and/or modify it
3 * under the terms of the GNU Lesser General Public License as published by
4 * the Free Software Foundation; either version 2.1 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 */
13
14 #ifdef SD_BOOT_LOG_TPM
15
16 #include <efi.h>
17 #include <efilib.h>
18 #include "measure.h"
19
20 #define EFI_TCG_PROTOCOL_GUID { 0xf541796d, 0xa62e, 0x4954, {0xa7, 0x75, 0x95, 0x84, 0xf6, 0x1b, 0x9c, 0xdd} }
21
22 typedef struct _TCG_VERSION {
23 UINT8 Major;
24 UINT8 Minor;
25 UINT8 RevMajor;
26 UINT8 RevMinor;
27 } TCG_VERSION;
28
29 typedef struct _TCG_BOOT_SERVICE_CAPABILITY {
30 UINT8 Size;
31 struct _TCG_VERSION StructureVersion;
32 struct _TCG_VERSION ProtocolSpecVersion;
33 UINT8 HashAlgorithmBitmap;
34 BOOLEAN TPMPresentFlag;
35 BOOLEAN TPMDeactivatedFlag;
36 } TCG_BOOT_SERVICE_CAPABILITY;
37
38 typedef UINT32 TCG_ALGORITHM_ID;
39 #define TCG_ALG_SHA 0x00000004 // The SHA1 algorithm
40
41 #define SHA1_DIGEST_SIZE 20
42
43 typedef struct _TCG_DIGEST {
44 UINT8 Digest[SHA1_DIGEST_SIZE];
45 } TCG_DIGEST;
46
47 #define EV_IPL 13
48
49 typedef struct _TCG_PCR_EVENT {
50 UINT32 PCRIndex;
51 UINT32 EventType;
52 struct _TCG_DIGEST digest;
53 UINT32 EventSize;
54 UINT8 Event[1];
55 } TCG_PCR_EVENT;
56
57 INTERFACE_DECL(_EFI_TCG);
58
59 typedef EFI_STATUS(EFIAPI * EFI_TCG_STATUS_CHECK) (IN struct _EFI_TCG * This,
60 OUT struct _TCG_BOOT_SERVICE_CAPABILITY * ProtocolCapability,
61 OUT UINT32 * TCGFeatureFlags,
62 OUT EFI_PHYSICAL_ADDRESS * EventLogLocation,
63 OUT EFI_PHYSICAL_ADDRESS * EventLogLastEntry);
64
65 typedef EFI_STATUS(EFIAPI * EFI_TCG_HASH_ALL) (IN struct _EFI_TCG * This,
66 IN UINT8 * HashData,
67 IN UINT64 HashDataLen,
68 IN TCG_ALGORITHM_ID AlgorithmId,
69 IN OUT UINT64 * HashedDataLen, IN OUT UINT8 ** HashedDataResult);
70
71 typedef EFI_STATUS(EFIAPI * EFI_TCG_LOG_EVENT) (IN struct _EFI_TCG * This,
72 IN struct _TCG_PCR_EVENT * TCGLogData,
73 IN OUT UINT32 * EventNumber, IN UINT32 Flags);
74
75 typedef EFI_STATUS(EFIAPI * EFI_TCG_PASS_THROUGH_TO_TPM) (IN struct _EFI_TCG * This,
76 IN UINT32 TpmInputParameterBlockSize,
77 IN UINT8 * TpmInputParameterBlock,
78 IN UINT32 TpmOutputParameterBlockSize,
79 IN UINT8 * TpmOutputParameterBlock);
80
81 typedef EFI_STATUS(EFIAPI * EFI_TCG_HASH_LOG_EXTEND_EVENT) (IN struct _EFI_TCG * This,
82 IN EFI_PHYSICAL_ADDRESS HashData,
83 IN UINT64 HashDataLen,
84 IN TCG_ALGORITHM_ID AlgorithmId,
85 IN struct _TCG_PCR_EVENT * TCGLogData,
86 IN OUT UINT32 * EventNumber,
87 OUT EFI_PHYSICAL_ADDRESS * EventLogLastEntry);
88
89 typedef struct _EFI_TCG {
90 EFI_TCG_STATUS_CHECK StatusCheck;
91 EFI_TCG_HASH_ALL HashAll;
92 EFI_TCG_LOG_EVENT LogEvent;
93 EFI_TCG_PASS_THROUGH_TO_TPM PassThroughToTPM;
94 EFI_TCG_HASH_LOG_EXTEND_EVENT HashLogExtendEvent;
95 } EFI_TCG;
96
97 #define EFI_TCG2_PROTOCOL_GUID {0x607f766c, 0x7455, 0x42be, { 0x93, 0x0b, 0xe4, 0xd7, 0x6d, 0xb2, 0x72, 0x0f }}
98
99 typedef struct tdEFI_TCG2_PROTOCOL EFI_TCG2_PROTOCOL;
100
101 typedef struct tdEFI_TCG2_VERSION {
102 UINT8 Major;
103 UINT8 Minor;
104 } EFI_TCG2_VERSION;
105
106 typedef UINT32 EFI_TCG2_EVENT_LOG_BITMAP;
107 typedef UINT32 EFI_TCG2_EVENT_LOG_FORMAT;
108 typedef UINT32 EFI_TCG2_EVENT_ALGORITHM_BITMAP;
109
110 #define EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2 0x00000001
111 #define EFI_TCG2_EVENT_LOG_FORMAT_TCG_2 0x00000002
112
113 typedef struct tdEFI_TCG2_BOOT_SERVICE_CAPABILITY {
114 UINT8 Size;
115 EFI_TCG2_VERSION StructureVersion;
116 EFI_TCG2_VERSION ProtocolVersion;
117 EFI_TCG2_EVENT_ALGORITHM_BITMAP HashAlgorithmBitmap;
118 EFI_TCG2_EVENT_LOG_BITMAP SupportedEventLogs;
119 BOOLEAN TPMPresentFlag;
120 UINT16 MaxCommandSize;
121 UINT16 MaxResponseSize;
122 UINT32 ManufacturerID;
123 UINT32 NumberOfPCRBanks;
124 EFI_TCG2_EVENT_ALGORITHM_BITMAP ActivePcrBanks;
125 } EFI_TCG2_BOOT_SERVICE_CAPABILITY;
126
127 #define EFI_TCG2_EVENT_HEADER_VERSION 1
128
129 typedef struct {
130 UINT32 HeaderSize;
131 UINT16 HeaderVersion;
132 UINT32 PCRIndex;
133 UINT32 EventType;
134 } EFI_TCG2_EVENT_HEADER;
135
136 typedef struct tdEFI_TCG2_EVENT {
137 UINT32 Size;
138 EFI_TCG2_EVENT_HEADER Header;
139 UINT8 Event[1];
140 } EFI_TCG2_EVENT;
141
142 typedef EFI_STATUS(EFIAPI * EFI_TCG2_GET_CAPABILITY) (IN EFI_TCG2_PROTOCOL * This,
143 IN OUT EFI_TCG2_BOOT_SERVICE_CAPABILITY * ProtocolCapability);
144
145 typedef EFI_STATUS(EFIAPI * EFI_TCG2_GET_EVENT_LOG) (IN EFI_TCG2_PROTOCOL * This,
146 IN EFI_TCG2_EVENT_LOG_FORMAT EventLogFormat,
147 OUT EFI_PHYSICAL_ADDRESS * EventLogLocation,
148 OUT EFI_PHYSICAL_ADDRESS * EventLogLastEntry,
149 OUT BOOLEAN * EventLogTruncated);
150
151 typedef EFI_STATUS(EFIAPI * EFI_TCG2_HASH_LOG_EXTEND_EVENT) (IN EFI_TCG2_PROTOCOL * This,
152 IN UINT64 Flags,
153 IN EFI_PHYSICAL_ADDRESS DataToHash,
154 IN UINT64 DataToHashLen, IN EFI_TCG2_EVENT * EfiTcgEvent);
155
156 typedef EFI_STATUS(EFIAPI * EFI_TCG2_SUBMIT_COMMAND) (IN EFI_TCG2_PROTOCOL * This,
157 IN UINT32 InputParameterBlockSize,
158 IN UINT8 * InputParameterBlock,
159 IN UINT32 OutputParameterBlockSize, IN UINT8 * OutputParameterBlock);
160
161 typedef EFI_STATUS(EFIAPI * EFI_TCG2_GET_ACTIVE_PCR_BANKS) (IN EFI_TCG2_PROTOCOL * This, OUT UINT32 * ActivePcrBanks);
162
163 typedef EFI_STATUS(EFIAPI * EFI_TCG2_SET_ACTIVE_PCR_BANKS) (IN EFI_TCG2_PROTOCOL * This, IN UINT32 ActivePcrBanks);
164
165 typedef EFI_STATUS(EFIAPI * EFI_TCG2_GET_RESULT_OF_SET_ACTIVE_PCR_BANKS) (IN EFI_TCG2_PROTOCOL * This,
166 OUT UINT32 * OperationPresent, OUT UINT32 * Response);
167
168 typedef struct tdEFI_TCG2_PROTOCOL {
169 EFI_TCG2_GET_CAPABILITY GetCapability;
170 EFI_TCG2_GET_EVENT_LOG GetEventLog;
171 EFI_TCG2_HASH_LOG_EXTEND_EVENT HashLogExtendEvent;
172 EFI_TCG2_SUBMIT_COMMAND SubmitCommand;
173 EFI_TCG2_GET_ACTIVE_PCR_BANKS GetActivePcrBanks;
174 EFI_TCG2_SET_ACTIVE_PCR_BANKS SetActivePcrBanks;
175 EFI_TCG2_GET_RESULT_OF_SET_ACTIVE_PCR_BANKS GetResultOfSetActivePcrBanks;
176 } EFI_TCG2;
177
178
179 static EFI_STATUS tpm1_measure_to_pcr_and_event_log(const EFI_TCG *tcg, UINT32 pcrindex, const EFI_PHYSICAL_ADDRESS buffer,
180 UINTN buffer_size, const CHAR16 *description) {
181 EFI_STATUS status;
182 TCG_PCR_EVENT *tcg_event;
183 UINT32 event_number;
184 EFI_PHYSICAL_ADDRESS event_log_last;
185 UINTN desc_len;
186
187 desc_len = (StrLen(description) + 1) * sizeof(CHAR16);
188
189 tcg_event = AllocateZeroPool(desc_len + sizeof(TCG_PCR_EVENT));
190
191 if (tcg_event == NULL)
192 return EFI_OUT_OF_RESOURCES;
193
194 tcg_event->EventSize = desc_len;
195 CopyMem((VOID *) & tcg_event->Event[0], (VOID *) description, desc_len);
196
197 tcg_event->PCRIndex = pcrindex;
198 tcg_event->EventType = EV_IPL;
199
200 event_number = 1;
201 status = uefi_call_wrapper(tcg->HashLogExtendEvent, 7,
202 tcg, buffer, buffer_size, TCG_ALG_SHA, tcg_event, &event_number, &event_log_last);
203
204 if (EFI_ERROR(status))
205 return status;
206
207 uefi_call_wrapper(BS->FreePool, 1, tcg_event);
208
209 return EFI_SUCCESS;
210 }
211
212
213 static EFI_STATUS tpm2_measure_to_pcr_and_event_log(const EFI_TCG2 *tcg, UINT32 pcrindex, const EFI_PHYSICAL_ADDRESS buffer,
214 UINT64 buffer_size, const CHAR16 *description) {
215 EFI_STATUS status;
216 EFI_TCG2_EVENT *tcg_event;
217 UINTN desc_len;
218
219 desc_len = StrLen(description) * sizeof(CHAR16);
220
221 tcg_event = AllocateZeroPool(sizeof(*tcg_event) - sizeof(tcg_event->Event) + desc_len + 1);
222
223 if (tcg_event == NULL)
224 return EFI_OUT_OF_RESOURCES;
225
226 tcg_event->Size = sizeof(EFI_TCG2_EVENT) - sizeof(tcg_event->Event) + desc_len + 1;
227 tcg_event->Header.HeaderSize = sizeof(EFI_TCG2_EVENT_HEADER);
228 tcg_event->Header.HeaderVersion = EFI_TCG2_EVENT_HEADER_VERSION;
229 tcg_event->Header.PCRIndex = pcrindex;
230 tcg_event->Header.EventType = EV_IPL;
231
232 CopyMem((VOID *) tcg_event->Event, (VOID *) description, desc_len);
233
234 status = uefi_call_wrapper(tcg->HashLogExtendEvent, 5, tcg, 0, buffer, buffer_size, tcg_event);
235
236 uefi_call_wrapper(BS->FreePool, 1, tcg_event);
237
238 if (EFI_ERROR(status))
239 return status;
240
241 return EFI_SUCCESS;
242 }
243
244 static EFI_TCG * tcg1_interface_check(void) {
245 EFI_GUID tpm_guid = EFI_TCG_PROTOCOL_GUID;
246 EFI_STATUS status;
247 EFI_TCG *tcg;
248 TCG_BOOT_SERVICE_CAPABILITY capability;
249 UINT32 features;
250 EFI_PHYSICAL_ADDRESS event_log_location;
251 EFI_PHYSICAL_ADDRESS event_log_last_entry;
252
253 status = LibLocateProtocol(&tpm_guid, (void **) &tcg);
254
255 if (EFI_ERROR(status))
256 return NULL;
257
258 capability.Size = (UINT8) sizeof(capability);
259 status = uefi_call_wrapper(tcg->StatusCheck, 5, tcg, &capability, &features, &event_log_location, &event_log_last_entry);
260
261 if (EFI_ERROR(status))
262 return NULL;
263
264 if (capability.TPMDeactivatedFlag)
265 return NULL;
266
267 if (!capability.TPMPresentFlag)
268 return NULL;
269
270 return tcg;
271 }
272
273 static EFI_TCG2 * tcg2_interface_check(void) {
274 EFI_GUID tpm2_guid = EFI_TCG2_PROTOCOL_GUID;
275 EFI_STATUS status;
276 EFI_TCG2 *tcg;
277 EFI_TCG2_BOOT_SERVICE_CAPABILITY capability;
278
279 status = LibLocateProtocol(&tpm2_guid, (void **) &tcg);
280
281 if (EFI_ERROR(status))
282 return NULL;
283
284 capability.Size = (UINT8) sizeof(capability);
285 status = uefi_call_wrapper(tcg->GetCapability, 2, tcg, &capability);
286
287 if (EFI_ERROR(status))
288 return NULL;
289
290 if (!capability.TPMPresentFlag)
291 return NULL;
292
293 return tcg;
294 }
295
296 EFI_STATUS tpm_log_event(UINT32 pcrindex, const EFI_PHYSICAL_ADDRESS buffer, UINTN buffer_size, const CHAR16 *description) {
297 EFI_TCG *tpm1;
298 EFI_TCG2 *tpm2;
299
300 tpm2 = tcg2_interface_check();
301 if (tpm2)
302 return tpm2_measure_to_pcr_and_event_log(tpm2, pcrindex, buffer, buffer_size, description);
303
304 tpm1 = tcg1_interface_check();
305 if (tpm1)
306 return tpm1_measure_to_pcr_and_event_log(tpm1, pcrindex, buffer, buffer_size, description);
307
308 /* No active TPM found, so don't return an error */
309 return EFI_SUCCESS;
310 }
311
312 #endif