|
20 | 20 | #include <linux/sched/task_stack.h> |
21 | 21 | #include <linux/panic_notifier.h> |
22 | 22 | #include <linux/ptrace.h> |
| 23 | +#include <linux/random.h> |
| 24 | +#include <linux/efi.h> |
23 | 25 | #include <linux/kdebug.h> |
24 | 26 | #include <linux/kmsg_dump.h> |
| 27 | +#include <linux/sizes.h> |
25 | 28 | #include <linux/slab.h> |
26 | 29 | #include <linux/dma-map-ops.h> |
27 | 30 | #include <linux/set_memory.h> |
@@ -355,6 +358,72 @@ int __init hv_common_init(void) |
355 | 358 | return 0; |
356 | 359 | } |
357 | 360 |
|
| 361 | +void __init ms_hyperv_late_init(void) |
| 362 | +{ |
| 363 | + struct acpi_table_header *header; |
| 364 | + acpi_status status; |
| 365 | + u8 *randomdata; |
| 366 | + u32 length, i; |
| 367 | + |
| 368 | + /* |
| 369 | + * Seed the Linux random number generator with entropy provided by |
| 370 | + * the Hyper-V host in ACPI table OEM0. |
| 371 | + */ |
| 372 | + if (!IS_ENABLED(CONFIG_ACPI)) |
| 373 | + return; |
| 374 | + |
| 375 | + status = acpi_get_table("OEM0", 0, &header); |
| 376 | + if (ACPI_FAILURE(status) || !header) |
| 377 | + return; |
| 378 | + |
| 379 | + /* |
| 380 | + * Since the "OEM0" table name is for OEM specific usage, verify |
| 381 | + * that what we're seeing purports to be from Microsoft. |
| 382 | + */ |
| 383 | + if (strncmp(header->oem_table_id, "MICROSFT", 8)) |
| 384 | + goto error; |
| 385 | + |
| 386 | + /* |
| 387 | + * Ensure the length is reasonable. Requiring at least 8 bytes and |
| 388 | + * no more than 4K bytes is somewhat arbitrary and just protects |
| 389 | + * against a malformed table. Hyper-V currently provides 64 bytes, |
| 390 | + * but allow for a change in a later version. |
| 391 | + */ |
| 392 | + if (header->length < sizeof(*header) + 8 || |
| 393 | + header->length > sizeof(*header) + SZ_4K) |
| 394 | + goto error; |
| 395 | + |
| 396 | + length = header->length - sizeof(*header); |
| 397 | + randomdata = (u8 *)(header + 1); |
| 398 | + |
| 399 | + pr_debug("Hyper-V: Seeding rng with %d random bytes from ACPI table OEM0\n", |
| 400 | + length); |
| 401 | + |
| 402 | + add_bootloader_randomness(randomdata, length); |
| 403 | + |
| 404 | + /* |
| 405 | + * To prevent the seed data from being visible in /sys/firmware/acpi, |
| 406 | + * zero out the random data in the ACPI table and fixup the checksum. |
| 407 | + * The zero'ing is done out of an abundance of caution in avoiding |
| 408 | + * potential security risks to the rng. Similarly, reset the table |
| 409 | + * length to just the header size so that a subsequent kexec doesn't |
| 410 | + * try to use the zero'ed out random data. |
| 411 | + */ |
| 412 | + for (i = 0; i < length; i++) { |
| 413 | + header->checksum += randomdata[i]; |
| 414 | + randomdata[i] = 0; |
| 415 | + } |
| 416 | + |
| 417 | + for (i = 0; i < sizeof(header->length); i++) |
| 418 | + header->checksum += ((u8 *)&header->length)[i]; |
| 419 | + header->length = sizeof(*header); |
| 420 | + for (i = 0; i < sizeof(header->length); i++) |
| 421 | + header->checksum -= ((u8 *)&header->length)[i]; |
| 422 | + |
| 423 | +error: |
| 424 | + acpi_put_table(header); |
| 425 | +} |
| 426 | + |
358 | 427 | /* |
359 | 428 | * Hyper-V specific initialization and die code for |
360 | 429 | * individual CPUs that is common across all architectures. |
|
0 commit comments