Debian Codegraph @

src/vendorcode/eltan/security/verified_boot/vboot_check.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <boot_device.h>
4 #include <bootmem.h>
5 #include <bootmode.h>
6 #include <cbfs.h>
7 #include <fmap_config.h>
8 #include <security/tpm/tss_errors.h>
9 #include <vboot_check.h>
10 #include <vboot_common.h>
11 #include <vb2_internals_please_do_not_use.h>
12
13 #define RSA_PUBLICKEY_FILE_NAME "vboot_public_key.bin"
14
15 #if CONFIG(VENDORCODE_ELTAN_VBOOT_USE_SHA512)
16 #define DIGEST_SIZE VB2_SHA512_DIGEST_SIZE
17 #define HASH_ALG VB2_HASH_SHA512
18 #else
19 #define DIGEST_SIZE VB2_SHA256_DIGEST_SIZE
20 #define HASH_ALG VB2_HASH_SHA256
21 #endif
22
23 int verified_boot_check_manifest(void)
24 {
25 uint8_t *buffer;
26 struct vb2_context *ctx;
27 struct vb2_kernel_preamble *pre;
28 static struct vb2_shared_data *sd;
29 size_t size;
30 uint8_t wb_buffer[3000];
31
32 if (vb2api_init(&wb_buffer, sizeof(wb_buffer), &ctx)) {
33 goto fail;
34 }
35
36 sd = vb2_get_sd(ctx);
37
38 buffer = cbfs_map(RSA_PUBLICKEY_FILE_NAME, &size);
39 if (!buffer || !size) {
40 printk(BIOS_ERR, "Public key not found!\n");
41 goto fail;
42 }
43
44 if ((size != CONFIG_VENDORCODE_ELTAN_VBOOT_KEY_SIZE) ||
45 (buffer != (void *)CONFIG_VENDORCODE_ELTAN_VBOOT_KEY_LOCATION)) {
46 printk(BIOS_ERR, "Illegal public key!\n");
47 goto fail;
48 }
49
50 /*
51 * Check if all items will fit into workbuffer:
52 * vb2_shared data, Public Key, Preamble data
53 */
54 if ((sd->workbuf_used + size + sizeof(struct vb2_kernel_preamble) +
55 ((CONFIG_VENDORCODE_ELTAN_OEM_MANIFEST_ITEMS * DIGEST_SIZE) + (2048/8))) >
56 sizeof(wb_buffer)) {
57 printk(BIOS_ERR, "Work buffer too small\n");
58 goto fail;
59 }
60
61 /* Add public key */
62 sd->data_key_offset = sd->workbuf_used;
63 sd->data_key_size = size;
64 sd->workbuf_used += sd->data_key_size;
65 memcpy((void *)((void *)sd + (long)sd->data_key_offset), (uint8_t *)buffer, size);
66
67 /* Fill preamble area */
68 sd->preamble_size = sizeof(struct vb2_kernel_preamble);
69 sd->preamble_offset = sd->data_key_offset + sd->data_key_size;
70 sd->workbuf_used += sd->preamble_size;
71 pre = (struct vb2_kernel_preamble *)((void *)sd + (long)sd->preamble_offset);
72
73 pre->flags = VB2_FIRMWARE_PREAMBLE_DISALLOW_HWCRYPTO;
74
75 /* Fill body_signature (vb2_structure). RSA2048 key is used */
76 cbfs_map("oemmanifest.bin", &size);
77 if (size != ((CONFIG_VENDORCODE_ELTAN_OEM_MANIFEST_ITEMS * DIGEST_SIZE) + (2048/8))) {
78 printk(BIOS_ERR, "Incorrect manifest size!\n");
79 goto fail;
80 }
81 pre->body_signature.data_size = CONFIG_VENDORCODE_ELTAN_OEM_MANIFEST_ITEMS *
82 DIGEST_SIZE;
83 pre->body_signature.sig_offset = sizeof(struct vb2_signature) +
84 pre->body_signature.data_size;
85 pre->body_signature.sig_size = size - pre->body_signature.data_size;
86 sd->workbuf_used += size;
87 memcpy((void *)((void *)&pre->body_signature + (long)sizeof(struct vb2_signature)),
88 (uint8_t *)CONFIG_VENDORCODE_ELTAN_OEM_MANIFEST_LOC, size);
89
90
91 if (vb2api_verify_kernel_data(ctx, (void *)CONFIG_VENDORCODE_ELTAN_OEM_MANIFEST_LOC,
92 pre->body_signature.data_size))
93 goto fail;
94
95 printk(BIOS_INFO, "%s: Successfully verified hash_table signature.\n", __func__);
96 return 0;
97
98 fail:
99 die("ERROR: HASH table verification failed!\n");
100 return -1;
101 }
102
103 /*
104 *
105 * measure_item
106 *
107 * extends the defined pcr using the hash calculated by the verified boot
108 * routines.
109 *
110 * @param[in] pcr PCR to extend
111 * @param[in] *hashData Pointer to the hash data
112 * @param[in] hashDataLen Length of the hash data
113 * @param[in] *event_msg Message to log or display
114 * @param[in] eventType Event type to use when logging
115
116 * @retval TPM_SUCCESS Operation completed successfully.
117 * @retval TPM_IOERROR Unexpected device behavior.
118 */
119 static tpm_result_t measure_item(uint32_t pcr, uint8_t *hashData, uint32_t hashDataLen,
120 int8_t *event_msg, TCG_EVENTTYPE eventType)
121 {
122 tpm_result_t rc = TPM_SUCCESS;
123 TCG_PCR_EVENT2_HDR tcgEventHdr;
124
125 memset(&tcgEventHdr, 0, sizeof(tcgEventHdr));
126 tcgEventHdr.pcrIndex = pcr;
127 tcgEventHdr.eventType = eventType;
128 if (event_msg) {
129 rc = mboot_hash_extend_log(MBOOT_HASH_PROVIDED, hashData,
130 hashDataLen, &tcgEventHdr,
131 (uint8_t *)event_msg);
132 if (rc == TPM_SUCCESS)
133 printk(BIOS_INFO, "%s: Success! %s measured to pcr %d.\n", __func__,
134 event_msg, pcr);
135 }
136 return rc;
137 }
138
139 static void verified_boot_check_buffer(const char *name, void *start, size_t size,
140 uint32_t hash_index, int32_t pcr)
141 {
142 uint8_t digest[DIGEST_SIZE];
143 vb2_error_t status;
144 tpm_result_t rc = TPM_SUCCESS;
145
146 printk(BIOS_DEBUG, "%s: %s HASH verification buffer %p size %d\n", __func__, name,
147 start, (int)size);
148
149 if (start && size) {
150 struct vb2_hash tmp_hash;
151
152 status = vb2_hash_calculate(false, start, size, HASH_ALG, &tmp_hash);
153 if (!status)
154 memcpy(digest, tmp_hash.raw, DIGEST_SIZE);
155
156 if ((CONFIG(VENDORCODE_ELTAN_VBOOT) && memcmp((void *)(
157 (uint8_t *)CONFIG_VENDORCODE_ELTAN_OEM_MANIFEST_LOC +
158 sizeof(digest) * hash_index), digest, sizeof(digest))) || status) {
159 printk(BIOS_DEBUG, "%s: buffer hash\n", __func__);
160 hexdump(digest, sizeof(digest));
161 printk(BIOS_DEBUG, "%s: manifest hash\n", __func__);
162 hexdump((void *)( (uint8_t *)CONFIG_VENDORCODE_ELTAN_OEM_MANIFEST_LOC +
163 sizeof(digest) * hash_index), sizeof(digest));
164 printk(BIOS_EMERG, "%s ", name);
165 die("HASH verification failed!\n");
166 } else {
167 if (!ENV_BOOTBLOCK && CONFIG(VENDORCODE_ELTAN_MBOOT)) {
168 if (pcr != -1) {
169 printk(BIOS_DEBUG, "%s: measuring %s\n", __func__,
170 name);
171 rc = measure_item(pcr, digest, sizeof(digest),
172 (int8_t *)name, 0);
173 if (rc)
174 printk(BIOS_DEBUG, "%s: measuring failed with error %#x!\n",
175 __func__, rc);
176 }
177 }
178 if (CONFIG(VENDORCODE_ELTAN_VBOOT))
179 printk(BIOS_DEBUG, "%s HASH verification success\n", name);
180 }
181 } else {
182 printk(BIOS_EMERG, "Invalid buffer\n");
183 die("HASH verification failed!\n");
184 }
185 }
186
187 #define COREBOOT_CBFS_START (0xffffffff - CONFIG_ROM_SIZE + 1 + FMAP_SECTION_COREBOOT_START)
188
189 void verified_boot_check_cbfsfile(const char *name, uint32_t type, uint32_t hash_index,
190 void **buffer, uint32_t *filesize, int32_t pcr)
191 {
192 void *start;
193 size_t size;
194
195 start = cbfs_map(name, &size);
196 if (start && size) {
197 /* Speed up processing by copying the file content to memory first */
198 if (!ENV_ROMSTAGE_OR_BEFORE && (type & VERIFIED_BOOT_COPY_BLOCK)) {
199
200 if ((buffer) && (*buffer) && (*filesize >= size) &&
201 ((uint32_t) start > COREBOOT_CBFS_START)) {
202
203 /* Use the buffer passed in if possible */
204 printk(BIOS_DEBUG, "%s: move buffer to memory\n", __func__);
205 /* Move the file to memory buffer passed in */
206 memcpy(*buffer, start, size);
207 start = *buffer;
208 printk(BIOS_DEBUG, "%s: done\n", __func__);
209
210 } else if (ENV_RAMSTAGE) {
211 /* Try to allocate a buffer from boot_mem */
212 void *local_buffer = bootmem_allocate_buffer(size);
213
214 if (local_buffer) {
215
216 /* Use the allocated buffer */
217 printk(BIOS_DEBUG, "%s: move file to memory\n",
218 __func__);
219 memcpy(local_buffer, start, size);
220 start = local_buffer;
221 printk(BIOS_DEBUG, "%s: done\n", __func__);
222 }
223 }
224 }
225 verified_boot_check_buffer(name, start, size, hash_index, pcr);
226 } else {
227 printk(BIOS_EMERG, "CBFS Failed to get file content for %s\n", name);
228 die("HASH verification failed!\n");
229 }
230 if (buffer)
231 *buffer = start;
232 if (filesize)
233 *filesize = size;
234 }
235
236 void process_verify_list(const verify_item_t list[])
237 {
238 int i = 0;
239
240 while (list[i].type != VERIFY_TERMINATOR) {
241 switch (list[i].type) {
242 case VERIFY_FILE:
243 verified_boot_check_cbfsfile(list[i].name, list[i].data.file.cbfs_type,
244 list[i].hash_index, NULL, NULL, list[i].pcr);
245 if (list[i].data.file.related_items) {
246 printk(BIOS_SPEW, "process related items\n");
247 process_verify_list(
248 (verify_item_t *)list[i].data.file.related_items);
249 }
250 break;
251 case VERIFY_BLOCK:
252 verified_boot_check_buffer(list[i].name,
253 (void *)list[i].data.block.start,
254 list[i].data.block.size,
255 list[i].hash_index, list[i].pcr);
256 break;
257 default:
258 printk(BIOS_EMERG, "INVALID TYPE IN VERIFY LIST %#x\n", list[i].type);
259 die("HASH verification failed!\n");
260 }
261 i++;
262 }
263 }
264
265 /*
266 * BOOTBLOCK
267 */
268
269 void verified_boot_bootblock_check(void)
270 {
271 printk(BIOS_SPEW, "%s: processing bootblock items\n", __func__);
272
273 if (CONFIG(VENDORCODE_ELTAN_VBOOT_SIGNED_MANIFEST)) {
274 printk(BIOS_SPEW, "%s: check the manifest\n", __func__);
275 if (verified_boot_check_manifest() != 0)
276 die("invalid manifest");
277 }
278 printk(BIOS_SPEW, "%s: process bootblock verify list\n", __func__);
279 process_verify_list(bootblock_verify_list);
280 }
281
282 /*
283 * ROMSTAGE
284 */
285
286 void verified_boot_early_check(void)
287 {
288 printk(BIOS_SPEW, "%s: processing early items\n", __func__);
289
290 if (CONFIG(VENDORCODE_ELTAN_MBOOT)) {
291 printk(BIOS_DEBUG, "mb_measure returned %#x\n",
292 mb_measure(platform_is_resuming()));
293 }
294
295 printk(BIOS_SPEW, "%s: process early verify list\n", __func__);
296 process_verify_list(romstage_verify_list);
297 }
298
299 /*
300 * RAM STAGE
301 */
302
303 static int process_oprom_list(const verify_item_t list[],
304 struct rom_header *rom_header)
305 {
306 int i = 0;
307 struct pci_data *rom_data;
308 uint32_t viddevid = 0;
309
310 if (le32_to_cpu(rom_header->signature) != PCI_ROM_HDR) {
311 printk(BIOS_ERR, "Incorrect expansion ROM header signature %04x DONT START\n",
312 le32_to_cpu(rom_header->signature));
313 return 0;
314 }
315
316 rom_data = (((void *)rom_header) + le32_to_cpu(rom_header->data));
317
318 viddevid |= (rom_data->vendor << 16);
319 viddevid |= rom_data->device;
320
321 while (list[i].type != VERIFY_TERMINATOR) {
322 switch (list[i].type) {
323 case VERIFY_OPROM:
324 if (viddevid == list[i].data.oprom.viddev) {
325 verified_boot_check_buffer(list[i].name,
326 (void *)rom_header,
327 rom_header->size * 512,
328 list[i].hash_index, list[i].pcr);
329 if (list[i].data.oprom.related_items) {
330 printk(BIOS_SPEW, "%s: process related items\n",
331 __func__);
332 process_verify_list(
333 (verify_item_t *)list[i].data.oprom.related_items);
334 }
335 printk(BIOS_SPEW, "%s: option rom can be started\n", __func__);
336 return 1;
337 }
338 break;
339 default:
340 printk(BIOS_EMERG, "%s: INVALID TYPE IN OPTION ROM LIST %#x\n",
341 __func__, list[i].type);
342 die("HASH verification failed!\n");
343 }
344 i++;
345 }
346 printk(BIOS_ERR, "%s: option rom not in list DONT START\n", __func__);
347 return 0;
348 }
349
350 int verified_boot_should_run_oprom(struct rom_header *rom_header)
351 {
352 return process_oprom_list(oprom_verify_list, rom_header);
353 }
354
355 int prog_locate_hook(struct prog *prog)
356 {
357 static int initialized;
358
359 if (ENV_BOOTBLOCK)
360 verified_boot_bootblock_check();
361
362 if (ENV_RAMINIT) {
363 if (!initialized && ((prog->type == PROG_REFCODE) ||
364 (prog->type == PROG_POSTCAR))) {
365 verified_boot_early_check();
366 initialized = 1;
367 }
368
369 if (CONFIG(POSTCAR_STAGE) && prog->type == PROG_POSTCAR)
370 process_verify_list(postcar_verify_list);
371
372 if (!CONFIG(POSTCAR_STAGE) && prog->type == PROG_RAMSTAGE)
373 process_verify_list(ramstage_verify_list);
374 }
375
376 if (ENV_POSTCAR && prog->type == PROG_RAMSTAGE)
377 process_verify_list(ramstage_verify_list);
378
379 if (ENV_RAMSTAGE && prog->type == PROG_PAYLOAD)
380 process_verify_list(payload_verify_list);
381
382 return 0;
383 }