1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
|
/*
* EIF (Enclave Image Format) related helpers
*
* Copyright (c) 2024 Dorjoy Chowdhury <dorjoychy111@gmail.com>
*
* This work is licensed under the terms of the GNU GPL, version 2 or
* (at your option) any later version. See the COPYING file in the
* top-level directory.
*/
#include "qemu/osdep.h"
#include "qemu/bswap.h"
#include "qapi/error.h"
#include "crypto/hash.h"
#include "crypto/x509-utils.h"
#include <zlib.h> /* for crc32 */
#include <cbor.h>
#include "hw/core/eif.h"
#define MAX_SECTIONS 32
/* members are ordered according to field order in .eif file */
typedef struct EifHeader {
uint8_t magic[4]; /* must be .eif in ascii i.e., [46, 101, 105, 102] */
uint16_t version;
uint16_t flags;
uint64_t default_memory;
uint64_t default_cpus;
uint16_t reserved;
uint16_t section_cnt;
uint64_t section_offsets[MAX_SECTIONS];
uint64_t section_sizes[MAX_SECTIONS];
uint32_t unused;
uint32_t eif_crc32;
} QEMU_PACKED EifHeader;
/* members are ordered according to field order in .eif file */
typedef struct EifSectionHeader {
/*
* 0 = invalid, 1 = kernel, 2 = cmdline, 3 = ramdisk, 4 = signature,
* 5 = metadata
*/
uint16_t section_type;
uint16_t flags;
uint64_t section_size;
} QEMU_PACKED EifSectionHeader;
enum EifSectionTypes {
EIF_SECTION_INVALID = 0,
EIF_SECTION_KERNEL = 1,
EIF_SECTION_CMDLINE = 2,
EIF_SECTION_RAMDISK = 3,
EIF_SECTION_SIGNATURE = 4,
EIF_SECTION_METADATA = 5,
EIF_SECTION_MAX = 6,
};
static const char *section_type_to_string(uint16_t type)
{
const char *str;
switch (type) {
case EIF_SECTION_INVALID:
str = "invalid";
break;
case EIF_SECTION_KERNEL:
str = "kernel";
break;
case EIF_SECTION_CMDLINE:
str = "cmdline";
break;
case EIF_SECTION_RAMDISK:
str = "ramdisk";
break;
case EIF_SECTION_SIGNATURE:
str = "signature";
break;
case EIF_SECTION_METADATA:
str = "metadata";
break;
default:
str = "unknown";
break;
}
return str;
}
static bool read_eif_header(FILE *f, EifHeader *header, uint32_t *crc,
Error **errp)
{
size_t got;
size_t header_size = sizeof(*header);
got = fread(header, 1, header_size, f);
if (got != header_size) {
error_setg(errp, "Failed to read EIF header");
return false;
}
if (memcmp(header->magic, ".eif", 4) != 0) {
error_setg(errp, "Invalid EIF image. Magic mismatch.");
return false;
}
/* Exclude header->eif_crc32 field from CRC calculation */
*crc = crc32(*crc, (uint8_t *)header, header_size - 4);
header->version = be16_to_cpu(header->version);
header->flags = be16_to_cpu(header->flags);
header->default_memory = be64_to_cpu(header->default_memory);
header->default_cpus = be64_to_cpu(header->default_cpus);
header->reserved = be16_to_cpu(header->reserved);
header->section_cnt = be16_to_cpu(header->section_cnt);
for (int i = 0; i < MAX_SECTIONS; ++i) {
header->section_offsets[i] = be64_to_cpu(header->section_offsets[i]);
}
for (int i = 0; i < MAX_SECTIONS; ++i) {
header->section_sizes[i] = be64_to_cpu(header->section_sizes[i]);
if (header->section_sizes[i] > SSIZE_MAX) {
error_setg(errp, "Invalid EIF image. Section size out of bounds");
return false;
}
}
header->unused = be32_to_cpu(header->unused);
header->eif_crc32 = be32_to_cpu(header->eif_crc32);
return true;
}
static bool read_eif_section_header(FILE *f, EifSectionHeader *section_header,
uint32_t *crc, Error **errp)
{
size_t got;
size_t section_header_size = sizeof(*section_header);
got = fread(section_header, 1, section_header_size, f);
if (got != section_header_size) {
error_setg(errp, "Failed to read EIF section header");
return false;
}
*crc = crc32(*crc, (uint8_t *)section_header, section_header_size);
section_header->section_type = be16_to_cpu(section_header->section_type);
section_header->flags = be16_to_cpu(section_header->flags);
section_header->section_size = be64_to_cpu(section_header->section_size);
return true;
}
/*
* Upon success, the caller is responsible for unlinking and freeing *tmp_path.
*/
static bool get_tmp_file(const char *template, char **tmp_path, Error **errp)
{
int tmp_fd;
*tmp_path = NULL;
tmp_fd = g_file_open_tmp(template, tmp_path, NULL);
if (tmp_fd < 0 || *tmp_path == NULL) {
error_setg(errp, "Failed to create temporary file for template %s",
template);
return false;
}
close(tmp_fd);
return true;
}
static void safe_fclose(FILE *f)
{
if (f) {
fclose(f);
}
}
static void safe_unlink(char *f)
{
if (f) {
unlink(f);
}
}
/*
* Upon success, the caller is reponsible for unlinking and freeing *kernel_path
*/
static bool read_eif_kernel(FILE *f, uint64_t size, char **kernel_path,
QCryptoHash *hash0, QCryptoHash *hash1,
uint32_t *crc, Error **errp)
{
size_t got;
FILE *tmp_file = NULL;
uint8_t *kernel = g_try_malloc(size);
if (!kernel) {
error_setg(errp, "Out of memory reading kernel section");
goto cleanup;
}
*kernel_path = NULL;
if (!get_tmp_file("eif-kernel-XXXXXX", kernel_path, errp)) {
goto cleanup;
}
tmp_file = fopen(*kernel_path, "wb");
if (tmp_file == NULL) {
error_setg_errno(errp, errno, "Failed to open temporary file %s",
*kernel_path);
goto cleanup;
}
got = fread(kernel, 1, size, f);
if ((uint64_t) got != size) {
error_setg(errp, "Failed to read EIF kernel section data");
goto cleanup;
}
got = fwrite(kernel, 1, size, tmp_file);
if ((uint64_t) got != size) {
error_setg(errp, "Failed to write EIF kernel section data to temporary"
" file");
goto cleanup;
}
*crc = crc32(*crc, kernel, size);
if (qcrypto_hash_update(hash0, (char *)kernel, size, errp) != 0 ||
qcrypto_hash_update(hash1, (char *)kernel, size, errp) != 0) {
goto cleanup;
}
g_free(kernel);
fclose(tmp_file);
return true;
cleanup:
safe_fclose(tmp_file);
safe_unlink(*kernel_path);
g_free(*kernel_path);
*kernel_path = NULL;
g_free(kernel);
return false;
}
static bool read_eif_cmdline(FILE *f, uint64_t size, char *cmdline,
QCryptoHash *hash0, QCryptoHash *hash1,
uint32_t *crc, Error **errp)
{
size_t got = fread(cmdline, 1, size, f);
if ((uint64_t) got != size) {
error_setg(errp, "Failed to read EIF cmdline section data");
return false;
}
*crc = crc32(*crc, (uint8_t *)cmdline, size);
if (qcrypto_hash_update(hash0, cmdline, size, errp) != 0 ||
qcrypto_hash_update(hash1, cmdline, size, errp) != 0) {
return false;
}
return true;
}
static bool read_eif_ramdisk(FILE *eif, FILE *initrd, uint64_t size,
QCryptoHash *hash0, QCryptoHash *h, uint32_t *crc,
Error **errp)
{
size_t got;
bool ret = false;
uint8_t *ramdisk = g_try_malloc(size);
if (!ramdisk) {
error_setg(errp, "Out of memory reading initrd section");
goto cleanup;
}
got = fread(ramdisk, 1, size, eif);
if ((uint64_t) got != size) {
error_setg(errp, "Failed to read EIF ramdisk section data");
goto cleanup;
}
got = fwrite(ramdisk, 1, size, initrd);
if ((uint64_t) got != size) {
error_setg(errp, "Failed to write EIF ramdisk data to temporary file");
goto cleanup;
}
*crc = crc32(*crc, ramdisk, size);
if (qcrypto_hash_update(hash0, (char *)ramdisk, size, errp) != 0 ||
qcrypto_hash_update(h, (char *)ramdisk, size, errp) != 0) {
goto cleanup;
}
ret = true;
cleanup:
g_free(ramdisk);
return ret;
}
static bool get_signature_fingerprint_sha384(FILE *eif, uint64_t size,
uint8_t *sha384,
uint32_t *crc,
Error **errp)
{
size_t got;
g_autofree uint8_t *sig = NULL;
g_autofree uint8_t *cert = NULL;
cbor_item_t *item = NULL;
cbor_item_t *pcr0 = NULL;
size_t len;
size_t hash_len = QCRYPTO_HASH_DIGEST_LEN_SHA384;
struct cbor_pair *pair;
struct cbor_load_result result;
bool ret = false;
sig = g_try_malloc(size);
if (!sig) {
error_setg(errp, "Out of memory reading signature section");
goto cleanup;
}
got = fread(sig, 1, size, eif);
if ((uint64_t) got != size) {
error_setg(errp, "Failed to read EIF signature section data");
goto cleanup;
}
*crc = crc32(*crc, sig, size);
item = cbor_load(sig, size, &result);
if (!item || result.error.code != CBOR_ERR_NONE) {
error_setg(errp, "Failed to load signature section data as CBOR");
goto cleanup;
}
if (!cbor_isa_array(item) || cbor_array_size(item) < 1) {
error_setg(errp, "Invalid signature CBOR");
goto cleanup;
}
pcr0 = cbor_array_get(item, 0);
if (!pcr0) {
error_setg(errp, "Failed to get PCR0 signature");
goto cleanup;
}
if (!cbor_isa_map(pcr0) || cbor_map_size(pcr0) != 2) {
error_setg(errp, "Invalid signature CBOR");
goto cleanup;
}
pair = cbor_map_handle(pcr0);
if (!cbor_isa_string(pair->key) || cbor_string_length(pair->key) != 19 ||
memcmp(cbor_string_handle(pair->key), "signing_certificate", 19) != 0) {
error_setg(errp, "Invalid signautre CBOR");
goto cleanup;
}
if (!cbor_isa_array(pair->value)) {
error_setg(errp, "Invalid signature CBOR");
goto cleanup;
}
len = cbor_array_size(pair->value);
if (len == 0) {
error_setg(errp, "Invalid signature CBOR");
goto cleanup;
}
cert = g_try_malloc(len);
if (!cert) {
error_setg(errp, "Out of memory reading signature section");
goto cleanup;
}
for (int i = 0; i < len; ++i) {
cbor_item_t *tmp = cbor_array_get(pair->value, i);
if (!tmp) {
error_setg(errp, "Invalid signature CBOR");
goto cleanup;
}
if (!cbor_isa_uint(tmp) || cbor_int_get_width(tmp) != CBOR_INT_8) {
cbor_decref(&tmp);
error_setg(errp, "Invalid signature CBOR");
goto cleanup;
}
cert[i] = cbor_get_uint8(tmp);
cbor_decref(&tmp);
}
if (qcrypto_get_x509_cert_fingerprint(cert, len, QCRYPTO_HASH_ALGO_SHA384,
sha384, &hash_len, errp)) {
goto cleanup;
}
ret = true;
cleanup:
if (pcr0) {
cbor_decref(&pcr0);
}
if (item) {
cbor_decref(&item);
}
return ret;
}
/* Expects file to have offset 0 before this function is called */
static long get_file_size(FILE *f, Error **errp)
{
long size;
if (fseek(f, 0, SEEK_END) != 0) {
error_setg_errno(errp, errno, "Failed to seek to the end of file");
return -1;
}
size = ftell(f);
if (size == -1) {
error_setg_errno(errp, errno, "Failed to get offset");
return -1;
}
if (fseek(f, 0, SEEK_SET) != 0) {
error_setg_errno(errp, errno, "Failed to seek back to the start");
return -1;
}
return size;
}
static bool get_SHA384_hash(QCryptoHash *h, uint8_t *hash, Error **errp)
{
size_t hash_len = QCRYPTO_HASH_DIGEST_LEN_SHA384;
return qcrypto_hash_finalize_bytes(h, &hash, &hash_len, errp) == 0;
}
/*
* Upon success, the caller is reponsible for unlinking and freeing
* *kernel_path, *initrd_path and freeing *cmdline.
*/
bool read_eif_file(const char *eif_path, const char *machine_initrd,
char **kernel_path, char **initrd_path, char **cmdline,
uint8_t *image_hash, uint8_t *bootstrap_hash,
uint8_t *app_hash, uint8_t *fingerprint_hash,
bool *signature_found, Error **errp)
{
FILE *f = NULL;
FILE *machine_initrd_f = NULL;
FILE *initrd_path_f = NULL;
long machine_initrd_size;
uint32_t crc = 0;
EifHeader eif_header;
bool seen_sections[EIF_SECTION_MAX] = {false};
/* kernel + ramdisks + cmdline SHA384 hash */
g_autoptr(QCryptoHash) hash0 = NULL;
/* kernel + boot ramdisk + cmdline SHA384 hash */
g_autoptr(QCryptoHash) hash1 = NULL;
/* application ramdisk(s) SHA384 hash */
g_autoptr(QCryptoHash) hash2 = NULL;
*signature_found = false;
*kernel_path = *initrd_path = *cmdline = NULL;
hash0 = qcrypto_hash_new(QCRYPTO_HASH_ALGO_SHA384, errp);
if (!hash0) {
goto cleanup;
}
hash1 = qcrypto_hash_new(QCRYPTO_HASH_ALGO_SHA384, errp);
if (!hash1) {
goto cleanup;
}
hash2 = qcrypto_hash_new(QCRYPTO_HASH_ALGO_SHA384, errp);
if (!hash2) {
goto cleanup;
}
f = fopen(eif_path, "rb");
if (f == NULL) {
error_setg_errno(errp, errno, "Failed to open %s", eif_path);
goto cleanup;
}
if (!read_eif_header(f, &eif_header, &crc, errp)) {
goto cleanup;
}
if (eif_header.version < 4) {
error_setg(errp, "Expected EIF version 4 or greater");
goto cleanup;
}
if (eif_header.flags != 0) {
error_setg(errp, "Expected EIF flags to be 0");
goto cleanup;
}
if (eif_header.section_cnt > MAX_SECTIONS) {
error_setg(errp, "EIF header section count must not be greater than "
"%d but found %d", MAX_SECTIONS, eif_header.section_cnt);
goto cleanup;
}
for (int i = 0; i < eif_header.section_cnt; ++i) {
EifSectionHeader hdr;
uint16_t section_type;
if (eif_header.section_offsets[i] > OFF_MAX) {
error_setg(errp, "Invalid EIF image. Section offset out of bounds");
goto cleanup;
}
if (fseek(f, eif_header.section_offsets[i], SEEK_SET) != 0) {
error_setg_errno(errp, errno, "Failed to offset to %" PRIu64 " in EIF file",
eif_header.section_offsets[i]);
goto cleanup;
}
if (!read_eif_section_header(f, &hdr, &crc, errp)) {
goto cleanup;
}
if (hdr.flags != 0) {
error_setg(errp, "Expected EIF section header flags to be 0");
goto cleanup;
}
if (eif_header.section_sizes[i] != hdr.section_size) {
error_setg(errp, "EIF section size mismatch between header and "
"section header: header %" PRIu64 ", section header %" PRIu64,
eif_header.section_sizes[i],
hdr.section_size);
goto cleanup;
}
section_type = hdr.section_type;
switch (section_type) {
case EIF_SECTION_KERNEL:
if (seen_sections[EIF_SECTION_KERNEL]) {
error_setg(errp, "Invalid EIF image. More than 1 kernel "
"section");
goto cleanup;
}
if (!read_eif_kernel(f, hdr.section_size, kernel_path, hash0,
hash1, &crc, errp)) {
goto cleanup;
}
break;
case EIF_SECTION_CMDLINE:
{
uint64_t size;
if (seen_sections[EIF_SECTION_CMDLINE]) {
error_setg(errp, "Invalid EIF image. More than 1 cmdline "
"section");
goto cleanup;
}
size = hdr.section_size;
*cmdline = g_try_malloc(size + 1);
if (!*cmdline) {
error_setg(errp, "Out of memory reading command line section");
goto cleanup;
}
if (!read_eif_cmdline(f, size, *cmdline, hash0, hash1, &crc,
errp)) {
goto cleanup;
}
(*cmdline)[size] = '\0';
break;
}
case EIF_SECTION_RAMDISK:
{
QCryptoHash *h = hash2;
if (!seen_sections[EIF_SECTION_RAMDISK]) {
/*
* If this is the first time we are seeing a ramdisk section,
* we need to:
* 1) hash it into bootstrap (hash1) instead of app (hash2)
* along with image (hash0)
* 2) create the initrd temporary file.
*/
h = hash1;
if (!get_tmp_file("eif-initrd-XXXXXX", initrd_path, errp)) {
goto cleanup;
}
initrd_path_f = fopen(*initrd_path, "wb");
if (initrd_path_f == NULL) {
error_setg_errno(errp, errno, "Failed to open file %s",
*initrd_path);
goto cleanup;
}
}
if (!read_eif_ramdisk(f, initrd_path_f, hdr.section_size, hash0, h,
&crc, errp)) {
goto cleanup;
}
break;
}
case EIF_SECTION_SIGNATURE:
*signature_found = true;
if (!get_signature_fingerprint_sha384(f, hdr.section_size,
fingerprint_hash, &crc,
errp)) {
goto cleanup;
}
break;
default:
/* other sections including invalid or unknown sections */
{
uint8_t *buf;
size_t got;
uint64_t size = hdr.section_size;
buf = g_try_malloc(size);
if (!buf) {
error_setg(errp, "Out of memory reading unknown section");
goto cleanup;
}
got = fread(buf, 1, size, f);
if ((uint64_t) got != size) {
g_free(buf);
error_setg(errp, "Failed to read EIF %s section data",
section_type_to_string(section_type));
goto cleanup;
}
crc = crc32(crc, buf, size);
g_free(buf);
break;
}
}
if (section_type < EIF_SECTION_MAX) {
seen_sections[section_type] = true;
}
}
if (!seen_sections[EIF_SECTION_KERNEL]) {
error_setg(errp, "Invalid EIF image. No kernel section.");
goto cleanup;
}
if (!seen_sections[EIF_SECTION_CMDLINE]) {
error_setg(errp, "Invalid EIF image. No cmdline section.");
goto cleanup;
}
if (!seen_sections[EIF_SECTION_RAMDISK]) {
error_setg(errp, "Invalid EIF image. No ramdisk section.");
goto cleanup;
}
if (eif_header.eif_crc32 != crc) {
error_setg(errp, "CRC mismatch. Expected %u but header has %u.",
crc, eif_header.eif_crc32);
goto cleanup;
}
/*
* Let's append the initrd file from "-initrd" option if any. Although
* we pass the crc pointer to read_eif_ramdisk, it is not useful anymore.
* We have already done the crc mismatch check above this code.
*/
if (machine_initrd) {
machine_initrd_f = fopen(machine_initrd, "rb");
if (machine_initrd_f == NULL) {
error_setg_errno(errp, errno, "Failed to open initrd file %s",
machine_initrd);
goto cleanup;
}
machine_initrd_size = get_file_size(machine_initrd_f, errp);
if (machine_initrd_size == -1) {
goto cleanup;
}
if (!read_eif_ramdisk(machine_initrd_f, initrd_path_f,
machine_initrd_size, hash0, hash2, &crc, errp)) {
goto cleanup;
}
}
if (!get_SHA384_hash(hash0, image_hash, errp)) {
goto cleanup;
}
if (!get_SHA384_hash(hash1, bootstrap_hash, errp)) {
goto cleanup;
}
if (!get_SHA384_hash(hash2, app_hash, errp)) {
goto cleanup;
}
fclose(f);
fclose(initrd_path_f);
safe_fclose(machine_initrd_f);
return true;
cleanup:
safe_fclose(f);
safe_fclose(initrd_path_f);
safe_fclose(machine_initrd_f);
safe_unlink(*kernel_path);
g_free(*kernel_path);
*kernel_path = NULL;
safe_unlink(*initrd_path);
g_free(*initrd_path);
*initrd_path = NULL;
g_free(*cmdline);
*cmdline = NULL;
return false;
}
|