From nobody Sun Nov 24 13:55:38 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1723308363; cv=none; d=zohomail.com; s=zohoarc; b=JzThyxePX0++6ssoBW718gb9rVxbgKFwvZE0J0Ms58z9O3vUXXo2ss024MV7h0FjIgJvLaGfyE8DKdvV8xKjnIphV7R2PurcutxXfYFgsvTx3AwVnc1bd8L7YgHJi3xGVbyAgBV0O/+rEqJAVz8HQAAptsSX6vwiSNrlbd+2jCA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1723308363; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=po1864G9kxPIA1SrB2pXf4Sek2hkmspEAs0nvNAI8ZI=; b=g15g8z0n3xSJnKHaHkSe4bfcEhY1MWlvEXjRbaqNtGmZ7tXT9rYfM9j7Qlg+InrSTnARJuz8VmrYCtbsVZZCykZcWwKSqUwsZ+wxUPk8/AgVtBIHUHgQwM/tCAHGIpgL/h3rOq/dDraBLIy91eKVLeVcXDuEU5nC4SctfgtPPbU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1723308363227666.8208353112002; Sat, 10 Aug 2024 09:46:03 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1scpDk-0004u9-C9; Sat, 10 Aug 2024 12:45:36 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1scpDj-0004tT-06 for qemu-devel@nongnu.org; Sat, 10 Aug 2024 12:45:35 -0400 Received: from mail-oi1-x233.google.com ([2607:f8b0:4864:20::233]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1scpDg-0003m2-80 for qemu-devel@nongnu.org; Sat, 10 Aug 2024 12:45:34 -0400 Received: by mail-oi1-x233.google.com with SMTP id 5614622812f47-3d9e13ef8edso2237415b6e.2 for ; Sat, 10 Aug 2024 09:45:31 -0700 (PDT) Received: from localhost.localdomain ([103.103.35.89]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-710e58735e2sm1424397b3a.30.2024.08.10.09.45.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 10 Aug 2024 09:45:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723308330; x=1723913130; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=po1864G9kxPIA1SrB2pXf4Sek2hkmspEAs0nvNAI8ZI=; b=fpwhHfiRDGzYODmmkdRrfnlmSbB3qY3HJ7Auu+BxNpY5WMwQG34ifILAQYXkvaKiN9 cEYTLFlHdpUfbv+BvWcaTmdWU61BYk8Sa3uyNYnSH2jsQF+GsWkcu73Q9xKo0vyxLAQp U9Vo6wnsFRvTOk37JrgRgO6Su6fJYK4piNPA048jwXa6EmYWoIMHah2WPhzPv/vXk4FW kt3jeT1qZzOTLf3DW+/p1Y2TCRLagMYixwxsK/fFlhmzqFW2LwriMdCTHKO2I6k9Jd35 JPWE+JAcVQfGUy9p/z+Xyo3oIvLjqYlhTJ8KPUHUx8DnPDLWvP84GrIWm5XMkPVMIhHB mbNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723308330; x=1723913130; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=po1864G9kxPIA1SrB2pXf4Sek2hkmspEAs0nvNAI8ZI=; b=vWjOzDZDrPivo2LndztADOMW2O3pYpfnV5IYOEcq2TFBv/4vm2lTSa6RRJtFaHPhGI lv2Tf8nDKBYBGVTGje5+lonSbF2u1iUtZrpD4vn4qV5yUNWIAmYzGes6VHoURYU74vkt uwDrOefo+gew7NSODzY1xv+hBsqIbum8ZKqekKgzeDU+KVQ1qbQyG4Gw5GZy1N9UXkCS eArJTcx2ona5oUGuL61cVGMdQYtMUOiT3NPTFWzgFurDVoR4Wj739twj8898H2b6jlf8 W+LFxlF4XuwfGFWTNajtXJIPyNVPw4Yro+Usqhr5gJ9Z8ngch78K8eEVuRyjlCg3Pdfl Lx/g== X-Gm-Message-State: AOJu0Yz2VkVMZKns9XR3LJQkAa9bU/RHxiKkvdlxUTj0JCIO4UnKHh+9 PPox0uLaQn54mqMVioHSQu6eTqu3dh94KxZNnavySDXlyAqgRNQQvjVTkIEW X-Google-Smtp-Source: AGHT+IHmJxD4jt9iTwfzD8rkevSYchcJ7oCY1wImU9aeBeBRQA4jniG3tWdnQGkeEALAhQXujWCl6w== X-Received: by 2002:a05:6808:2019:b0:3d9:2986:5a3b with SMTP id 5614622812f47-3dc416da611mr6678963b6e.37.1723308330459; Sat, 10 Aug 2024 09:45:30 -0700 (PDT) From: Dorjoy Chowdhury To: qemu-devel@nongnu.org Cc: graf@amazon.com, agraf@csgraf.de, stefanha@redhat.com, pbonzini@redhat.com, slp@redhat.com, richard.henderson@linaro.org, eduardo@habkost.net, mst@redhat.com, marcel.apfelbaum@gmail.com, berrange@redhat.com, philmd@linaro.org Subject: [PATCH v3 4/5] machine/nitro-enclave: Add built-in Nitro Secure Module device Date: Sat, 10 Aug 2024 22:45:01 +0600 Message-Id: <20240810164502.19693-5-dorjoychy111@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240810164502.19693-1-dorjoychy111@gmail.com> References: <20240810164502.19693-1-dorjoychy111@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::233; envelope-from=dorjoychy111@gmail.com; helo=mail-oi1-x233.google.com X-Spam_score_int: 15 X-Spam_score: 1.5 X-Spam_bar: + X-Spam_report: (1.5 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_SBL_CSS=3.335, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1723308363846116600 Content-Type: text/plain; charset="utf-8" AWS Nitro Enclaves have built-in Nitro Secure Module (NSM) device which is used for stripped down TPM functionality like attestation. This commit adds the built-in NSM device in the nitro-enclave machine type. In Nitro Enclaves, all the PCRs start in a known zero state and the first 16 PCRs are locked from boot and reserved. The PCR0, PCR1, PCR2 and PCR8 contain the SHA384 hashes related to the EIF file used to boot the VM for validation. A new optional nitro-enclave machine option 'id' has been added which will be the enclave identifier reflected in the module-id of the NSM device. Otherwise, the device will have a default id set. Signed-off-by: Dorjoy Chowdhury --- hw/core/eif.c | 205 +++++++++++++++++++++++++++++++- hw/core/eif.h | 5 +- hw/core/meson.build | 4 +- hw/i386/Kconfig | 1 + hw/i386/nitro_enclave.c | 85 ++++++++++++- include/hw/i386/nitro_enclave.h | 19 +++ 6 files changed, 310 insertions(+), 9 deletions(-) diff --git a/hw/core/eif.c b/hw/core/eif.c index 5558879a96..d2c65668ef 100644 --- a/hw/core/eif.c +++ b/hw/core/eif.c @@ -12,6 +12,9 @@ #include "qemu/bswap.h" #include "qapi/error.h" #include /* for crc32 */ +#include +#include +#include =20 #include "hw/core/eif.h" =20 @@ -180,6 +183,8 @@ static void safe_unlink(char *f) * Upon success, the caller is reponsible for unlinking and freeing *kerne= l_path */ static bool read_eif_kernel(FILE *f, uint64_t size, char **kernel_path, + GChecksum *image_hasher, + GChecksum *bootstrap_hasher, uint32_t *crc, Error **errp) { size_t got; @@ -213,6 +218,8 @@ static bool read_eif_kernel(FILE *f, uint64_t size, cha= r **kernel_path, } =20 *crc =3D crc32(*crc, kernel, size); + g_checksum_update(image_hasher, kernel, size); + g_checksum_update(bootstrap_hasher, kernel, size); g_free(kernel); fclose(tmp_file); =20 @@ -230,6 +237,8 @@ static bool read_eif_kernel(FILE *f, uint64_t size, cha= r **kernel_path, } =20 static bool read_eif_cmdline(FILE *f, uint64_t size, char *cmdline, + GChecksum *image_hasher, + GChecksum *bootstrap_hasher, uint32_t *crc, Error **errp) { size_t got =3D fread(cmdline, 1, size, f); @@ -239,10 +248,14 @@ static bool read_eif_cmdline(FILE *f, uint64_t size, = char *cmdline, } =20 *crc =3D crc32(*crc, (uint8_t *)cmdline, size); + g_checksum_update(image_hasher, (uint8_t *)cmdline, size); + g_checksum_update(bootstrap_hasher, (uint8_t *)cmdline, size); return true; } =20 static bool read_eif_ramdisk(FILE *eif, FILE *initrd, uint64_t size, + GChecksum *image_hasher, + GChecksum *bootstrap_or_app_hasher, uint32_t *crc, Error **errp) { size_t got; @@ -261,6 +274,8 @@ static bool read_eif_ramdisk(FILE *eif, FILE *initrd, u= int64_t size, } =20 *crc =3D crc32(*crc, ramdisk, size); + g_checksum_update(image_hasher, ramdisk, size); + g_checksum_update(bootstrap_or_app_hasher, ramdisk, size); g_free(ramdisk); return true; =20 @@ -269,6 +284,125 @@ static bool read_eif_ramdisk(FILE *eif, FILE *initrd,= uint64_t size, return false; } =20 +static bool get_fingerprint_sha384_from_cert(uint8_t *cert, size_t size, + uint8_t *sha384, Error **errp) +{ + gnutls_x509_crt_t crt; + size_t hash_size =3D 48; + gnutls_datum_t datum =3D {.data =3D cert, .size =3D size}; + + gnutls_global_init(); + gnutls_x509_crt_init(&crt); + + if (gnutls_x509_crt_import(crt, &datum, GNUTLS_X509_FMT_PEM) !=3D 0) { + error_setg(errp, "Failed to import certificate"); + goto cleanup; + } + + if (gnutls_x509_crt_get_fingerprint(crt, GNUTLS_DIG_SHA384, sha384, + &hash_size) !=3D 0) { + error_setg(errp, "Failed to compute SHA384 fingerprint"); + goto cleanup; + } + + return true; + + cleanup: + gnutls_x509_crt_deinit(crt); + gnutls_global_deinit(); + return false; +} + +static bool get_signature_fingerprint_sha384(FILE *eif, uint64_t size, + uint8_t *sha384, + uint32_t *crc, + Error **errp) +{ + size_t got; + uint8_t *sig =3D NULL; + uint8_t *cert =3D NULL; + cbor_item_t *item =3D NULL; + cbor_item_t *pcr0 =3D NULL; + size_t len; + struct cbor_pair *pair; + struct cbor_load_result result; + + sig =3D g_malloc(size); + got =3D fread(sig, 1, size, eif); + if ((uint64_t) got !=3D size) { + error_setg(errp, "Failed to read EIF signature section data"); + goto cleanup; + } + + *crc =3D crc32(*crc, sig, size); + + item =3D cbor_load(sig, size, &result); + if (!item || result.error.code !=3D 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 =3D 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) !=3D 2) { + error_setg(errp, "Invalid signature CBOR"); + goto cleanup; + } + pair =3D cbor_map_handle(pcr0); + if (!cbor_isa_string(pair->key) || cbor_string_length(pair->key) !=3D = 19 || + memcmp(cbor_string_handle(pair->key), "signing_certificate", 19) != =3D 0) { + error_setg(errp, "Invalid signautre CBOR"); + goto cleanup; + } + if (!cbor_isa_array(pair->value)) { + error_setg(errp, "Invalid signature CBOR"); + goto cleanup; + } + len =3D cbor_array_size(pair->value); + if (len =3D=3D 0) { + error_setg(errp, "Invalid signature CBOR"); + goto cleanup; + } + cert =3D g_malloc(len); + for (int i =3D 0; i < len; ++i) { + cbor_item_t *tmp =3D 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) !=3D CBOR_INT_8= ) { + cbor_decref(&tmp); + error_setg(errp, "Invalid signature CBOR"); + goto cleanup; + } + cert[i] =3D cbor_get_uint8(tmp); + cbor_decref(&tmp); + } + + if (!get_fingerprint_sha384_from_cert(cert, len, sha384, errp)) { + goto cleanup; + } + + return true; + + cleanup: + g_free(sig); + g_free(cert); + if (pcr0) { + cbor_decref(&pcr0); + } + if (item) { + cbor_decref(&item); + } + return false; +} + /* Expects file to have offset 0 before this function is called */ static long get_file_size(FILE *f, Error **errp) { @@ -299,7 +433,9 @@ static long get_file_size(FILE *f, Error **errp) */ bool read_eif_file(const char *eif_path, const char *machine_initrd, char **kernel_path, char **initrd_path, char **cmdline, - Error **errp) + uint8_t *image_sha384, uint8_t *bootstrap_sha384, + uint8_t *app_sha384, uint8_t *fingerprint_sha384, + bool *signature_found, Error **errp) { FILE *f =3D NULL; FILE *machine_initrd_f =3D NULL; @@ -308,9 +444,33 @@ bool read_eif_file(const char *eif_path, const char *m= achine_initrd, uint32_t crc =3D 0; EifHeader eif_header; bool seen_sections[EIF_SECTION_MAX] =3D {false}; - + /* kernel + ramdisks + cmdline sha384 hash */ + GChecksum *image_hasher =3D NULL; + /* kernel + boot ramdisk + cmdline sha384 hash */ + GChecksum *bootstrap_hasher =3D NULL; + /* application ramdisk(s) hash */ + GChecksum *app_hasher =3D NULL; + size_t digest_len; + + *signature_found =3D false; *kernel_path =3D *initrd_path =3D *cmdline =3D NULL; =20 + image_hasher =3D g_checksum_new(G_CHECKSUM_SHA384); + if (image_hasher =3D=3D NULL) { + error_setg(errp, "Failed to initialize sha384 hash for image"); + goto cleanup; + } + bootstrap_hasher =3D g_checksum_new(G_CHECKSUM_SHA384); + if (bootstrap_hasher =3D=3D NULL) { + error_setg(errp, "Failed to initialize sha384 hash for bootstrap"); + goto cleanup; + } + app_hasher =3D g_checksum_new(G_CHECKSUM_SHA384); + if (app_hasher =3D=3D NULL) { + error_setg(errp, "Failed to initialize sha384 hash for app"); + goto cleanup; + } + f =3D fopen(eif_path, "rb"); if (f =3D=3D NULL) { error_setg_errno(errp, errno, "Failed to open %s", eif_path); @@ -374,7 +534,7 @@ bool read_eif_file(const char *eif_path, const char *ma= chine_initrd, goto cleanup; } if (!read_eif_kernel(f, section_header.section_size, kernel_pa= th, - &crc, errp)) { + image_hasher, bootstrap_hasher, &crc, err= p)) { goto cleanup; } =20 @@ -389,7 +549,8 @@ bool read_eif_file(const char *eif_path, const char *ma= chine_initrd, } size =3D section_header.section_size; *cmdline =3D g_malloc(size + 1); - if (!read_eif_cmdline(f, size, *cmdline, &crc, errp)) { + if (!read_eif_cmdline(f, size, *cmdline, image_hasher, + bootstrap_hasher, &crc, errp)) { goto cleanup; } (*cmdline)[size] =3D '\0'; @@ -398,7 +559,13 @@ bool read_eif_file(const char *eif_path, const char *m= achine_initrd, } case EIF_SECTION_RAMDISK: { + /* + * If it's the first ramdisk, we need to hash it into bootstra= p, + * otherwise we need to hash it into app. + */ + GChecksum *hasher =3D app_hasher; if (!seen_sections[EIF_SECTION_RAMDISK]) { + hasher =3D bootstrap_hasher; /* * If this is the first time we are seeing a ramdisk secti= on, * we need to create the initrd temporary file. @@ -415,12 +582,21 @@ bool read_eif_file(const char *eif_path, const char *= machine_initrd, } =20 if (!read_eif_ramdisk(f, initrd_path_f, section_header.section= _size, - &crc, errp)) { + image_hasher, hasher, &crc, errp)) { goto cleanup; } =20 break; } + case EIF_SECTION_SIGNATURE: + *signature_found =3D true; + if (!get_signature_fingerprint_sha384(f, + section_header.section_s= ize, + fingerprint_sha384, &crc, + errp)) { + goto cleanup; + } + break; default: /* other sections including invalid or unknown sections */ { @@ -484,17 +660,34 @@ bool read_eif_file(const char *eif_path, const char *= machine_initrd, } =20 if (!read_eif_ramdisk(machine_initrd_f, initrd_path_f, - machine_initrd_size, &crc, errp)) { + machine_initrd_size, image_hasher, app_hashe= r, + &crc, errp)) { goto cleanup; } } =20 + digest_len =3D 48; + g_checksum_get_digest(image_hasher, image_sha384, &digest_len); + g_checksum_free(image_hasher); + + digest_len =3D 48; + g_checksum_get_digest(bootstrap_hasher, bootstrap_sha384, &digest_len); + g_checksum_free(bootstrap_hasher); + + digest_len =3D 48; + g_checksum_get_digest(app_hasher, app_sha384, &digest_len); + g_checksum_free(app_hasher); + fclose(f); fclose(initrd_path_f); safe_fclose(machine_initrd_f); return true; =20 cleanup: + g_checksum_free(image_hasher); + g_checksum_free(bootstrap_hasher); + g_checksum_free(app_hasher); + safe_fclose(f); safe_fclose(initrd_path_f); safe_fclose(machine_initrd_f); diff --git a/hw/core/eif.h b/hw/core/eif.h index 7063974d93..fed3cb5514 100644 --- a/hw/core/eif.h +++ b/hw/core/eif.h @@ -13,7 +13,10 @@ =20 bool read_eif_file(const char *eif_path, const char *machine_initrd, char **kernel_path, char **initrd_path, - char **kernel_cmdline, Error **errp); + char **kernel_cmdline, uint8_t *image_sha384, + uint8_t *bootstrap_sha384, uint8_t *app_sha384, + uint8_t *fingerprint_sha384, bool *signature_found, + Error **errp); =20 #endif =20 diff --git a/hw/core/meson.build b/hw/core/meson.build index f32d1ad943..7e7a14ee00 100644 --- a/hw/core/meson.build +++ b/hw/core/meson.build @@ -12,6 +12,8 @@ hwcore_ss.add(files( 'qdev-clock.c', )) =20 +libcbor =3D dependency('libcbor', version: '>=3D0.8.0') + common_ss.add(files('cpu-common.c')) common_ss.add(files('machine-smp.c')) system_ss.add(when: 'CONFIG_FITLOADER', if_true: files('loader-fit.c')) @@ -24,7 +26,7 @@ system_ss.add(when: 'CONFIG_REGISTER', if_true: files('re= gister.c')) system_ss.add(when: 'CONFIG_SPLIT_IRQ', if_true: files('split-irq.c')) system_ss.add(when: 'CONFIG_XILINX_AXI', if_true: files('stream.c')) system_ss.add(when: 'CONFIG_PLATFORM_BUS', if_true: files('sysbus-fdt.c')) -system_ss.add(when: 'CONFIG_NITRO_ENCLAVE', if_true: [files('eif.c'), zlib= ]) +system_ss.add(when: 'CONFIG_NITRO_ENCLAVE', if_true: [files('eif.c'), zlib= , libcbor, gnutls]) =20 system_ss.add(files( 'cpu-sysemu.c', diff --git a/hw/i386/Kconfig b/hw/i386/Kconfig index 821532c4c8..63271bf915 100644 --- a/hw/i386/Kconfig +++ b/hw/i386/Kconfig @@ -133,6 +133,7 @@ config NITRO_ENCLAVE default y depends on MICROVM select VHOST_USER_VSOCK + select VIRTIO_NSM =20 config X86_IOMMU bool diff --git a/hw/i386/nitro_enclave.c b/hw/i386/nitro_enclave.c index 280ab4cc9b..b34e8399ee 100644 --- a/hw/i386/nitro_enclave.c +++ b/hw/i386/nitro_enclave.c @@ -20,6 +20,7 @@ #include "hw/i386/microvm.h" #include "hw/i386/nitro_enclave.h" #include "hw/virtio/virtio-mmio.h" +#include "hw/virtio/virtio-nsm.h" #include "hw/virtio/vhost-user-vsock.h" #include "sysemu/hostmem.h" =20 @@ -72,9 +73,27 @@ static void vhost_user_vsock_init(NitroEnclaveMachineSta= te *nems) qdev_realize_and_unref(dev, bus, &error_fatal); } =20 +static void virtio_nsm_init(NitroEnclaveMachineState *nems) +{ + DeviceState *dev =3D qdev_new(TYPE_VIRTIO_NSM); + VirtIONSM *vnsm =3D VIRTIO_NSM(dev); + BusState *bus =3D find_free_virtio_mmio_bus(); + + if (!bus) { + error_report("Failed to find bus for virtio-nsm device."); + exit(1); + } + + qdev_prop_set_string(dev, "module-id", nems->id); + + qdev_realize_and_unref(dev, bus, &error_fatal); + nems->vnsm =3D vnsm; +} + static void nitro_enclave_devices_init(NitroEnclaveMachineState *nems) { vhost_user_vsock_init(nems); + virtio_nsm_init(nems); } =20 static void nitro_enclave_machine_state_init(MachineState *machine) @@ -87,10 +106,46 @@ static void nitro_enclave_machine_state_init(MachineSt= ate *machine) nitro_enclave_devices_init(ne_state); } =20 +static void nitro_enclave_machine_reset(MachineState *machine, + ShutdownCause reason) +{ + NitroEnclaveMachineClass *ne_class =3D + NITRO_ENCLAVE_MACHINE_GET_CLASS(machine); + NitroEnclaveMachineState *ne_state =3D NITRO_ENCLAVE_MACHINE(machine); + + ne_class->parent_reset(machine, reason); + + memset(ne_state->vnsm->pcrs, 0, sizeof(ne_state->vnsm->pcrs)); + + /* PCR0 */ + ne_state->vnsm->extend_pcr(ne_state->vnsm, 0, ne_state->image_sha384, + SHA384_BYTE_LEN); + /* PCR1 */ + ne_state->vnsm->extend_pcr(ne_state->vnsm, 1, ne_state->bootstrap_sha3= 84, + SHA384_BYTE_LEN); + /* PCR2 */ + ne_state->vnsm->extend_pcr(ne_state->vnsm, 2, ne_state->app_sha384, + SHA384_BYTE_LEN); + /* PCR8 */ + if (ne_state->signature_found) { + ne_state->vnsm->extend_pcr(ne_state->vnsm, 8, + ne_state->fingerprint_sha384, + SHA384_BYTE_LEN); + } + + /* First 16 PCRs are locked from boot and reserved for nitro enclave */ + for (int i =3D 0; i < 16; ++i) { + ne_state->vnsm->lock_pcr(ne_state->vnsm, i); + } +} + static void nitro_enclave_machine_initfn(Object *obj) { MicrovmMachineState *mms =3D MICROVM_MACHINE(obj); X86MachineState *x86ms =3D X86_MACHINE(obj); + NitroEnclaveMachineState *nems =3D NITRO_ENCLAVE_MACHINE(obj); + + nems->id =3D g_strdup("i-234-enc5678"); =20 /* AWS nitro enclaves have PCIE and ACPI disabled */ mms->pcie =3D ON_OFF_AUTO_OFF; @@ -103,9 +158,13 @@ static void x86_load_eif(X86MachineState *x86ms, FWCfg= State *fw_cfg, Error *err; char *eif_kernel, *eif_initrd, *eif_cmdline; MachineState *machine =3D MACHINE(x86ms); + NitroEnclaveMachineState *nems =3D NITRO_ENCLAVE_MACHINE(x86ms); =20 if (!read_eif_file(machine->kernel_filename, machine->initrd_filename, - &eif_kernel, &eif_initrd, &eif_cmdline, &err)) { + &eif_kernel, &eif_initrd, &eif_cmdline, + nems->image_sha384, nems->bootstrap_sha384, + nems->app_sha384, nems->fingerprint_sha384, + &(nems->signature_found), &err)) { error_report_err(err); exit(1); } @@ -176,6 +235,22 @@ static void nitro_enclave_set_vsock_chardev_id(Object = *obj, const char *value, nems->vsock =3D g_strdup(value); } =20 +static char *nitro_enclave_get_id(Object *obj, Error **errp) +{ + NitroEnclaveMachineState *nems =3D NITRO_ENCLAVE_MACHINE(obj); + + return g_strdup(nems->id); +} + +static void nitro_enclave_set_id(Object *obj, const char *value, + Error **errp) +{ + NitroEnclaveMachineState *nems =3D NITRO_ENCLAVE_MACHINE(obj); + + g_free(nems->id); + nems->id =3D g_strdup(value); +} + static void nitro_enclave_class_init(ObjectClass *oc, void *data) { MachineClass *mc =3D MACHINE_CLASS(oc); @@ -190,6 +265,9 @@ static void nitro_enclave_class_init(ObjectClass *oc, v= oid *data) nemc->parent_init =3D mc->init; mc->init =3D nitro_enclave_machine_state_init; =20 + nemc->parent_reset =3D mc->reset; + mc->reset =3D nitro_enclave_machine_reset; + mc->create_default_memdev =3D create_memfd_backend; =20 object_class_property_add_str(oc, NITRO_ENCLAVE_VSOCK_CHARDEV_ID, @@ -198,6 +276,11 @@ static void nitro_enclave_class_init(ObjectClass *oc, = void *data) object_class_property_set_description(oc, NITRO_ENCLAVE_VSOCK_CHARDEV_= ID, "Set chardev id for vhost-user-v= sock " "device"); + + object_class_property_add_str(oc, NITRO_ENCLAVE_ID, nitro_enclave_get_= id, + nitro_enclave_set_id); + object_class_property_set_description(oc, NITRO_ENCLAVE_ID, + "Set enclave identifier"); } =20 static const TypeInfo nitro_enclave_machine_info =3D { diff --git a/include/hw/i386/nitro_enclave.h b/include/hw/i386/nitro_enclav= e.h index 3e302de851..1a95191d76 100644 --- a/include/hw/i386/nitro_enclave.h +++ b/include/hw/i386/nitro_enclave.h @@ -13,14 +13,19 @@ =20 #include "hw/i386/microvm.h" #include "qom/object.h" +#include "hw/virtio/virtio-nsm.h" + +#define SHA384_BYTE_LEN 48 =20 /* Machine type options */ #define NITRO_ENCLAVE_VSOCK_CHARDEV_ID "vsock" +#define NITRO_ENCLAVE_ID "id" =20 struct NitroEnclaveMachineClass { MicrovmMachineClass parent; =20 void (*parent_init)(MachineState *state); + void (*parent_reset)(MachineState *machine, ShutdownCause reason); }; =20 struct NitroEnclaveMachineState { @@ -28,6 +33,20 @@ struct NitroEnclaveMachineState { =20 /* Machine type options */ char *vsock; + char *id; + + /* Machine state */ + VirtIONSM *vnsm; + + /* kernel + ramdisks + cmdline sha384 hash */ + uint8_t image_sha384[SHA384_BYTE_LEN]; + /* kernel + boot ramdisk + cmdline sha384 hash */ + uint8_t bootstrap_sha384[SHA384_BYTE_LEN]; + /* application ramdisk(s) hash */ + uint8_t app_sha384[SHA384_BYTE_LEN]; + /* certificate fingerprint hash */ + uint8_t fingerprint_sha384[SHA384_BYTE_LEN]; + bool signature_found; }; =20 #define TYPE_NITRO_ENCLAVE_MACHINE MACHINE_TYPE_NAME("nitro-enclave") --=20 2.39.2