eif.c 22 KB

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