AWS Nitro Enclave emulation support
Subsystems (2)
v3 cover AWS Nitro Enclave emulation support
This is v3 submission for AWS Nitro Enclave emulation in QEMU. From the QEMU side the implementation for nitro enclaves is complete. A lot of changes from v2 so all the patches need to be reviewed. v2 is at:
https://mail.gnu.org/archive/html/qemu-devel/2024-06/msg00012.html
Changes in v3:
- Support for virtio-nsm device
- The EIF related logic has been removed from microvm.c i.e., the logic is
contained in enclave related code
- For vsock emulation in nitro-enclave, now vhost-user-vsock is being used
instead of vhost-vsock (more details in the cover-letter below)
- updated documentation accordingly
Changes in v2:
- moved eif.c and eif.h files from hw/i386 to hw/core
Hi,
Hope everyone is doing well. This is a patch series adding AWS Nitro Enclave[1] emulation support in QEMU. Alexander Graf is mentoring me on this work. I have a gitlab branch where you can view the patches in the gitlab web UI for each commit:
https://gitlab.com/dorjoy03/qemu/-/tree/nitro-enclave-emulation
AWS nitro enclaves is an Amazon EC2[2] feature that allows creating isolated execution environments, called enclaves, from Amazon EC2 instances, which are used for processing highly sensitive data. Enclaves have no persistent storage and no external networking. The enclave VMs are based on Firecracker microvm and have a vhost-vsock device for communication with the parent EC2 instance that spawned it and a Nitro Secure Module (NSM) device for cryptographic attestation. The parent instance VM always has CID 3 while the enclave VM gets a dynamic CID. The enclave VMs can communicate with the parent instance over various ports to CID 3, for example, the init process inside an enclave sends a heartbeat to port 9000 upon boot, expecting a heartbeat reply, letting the parent instance know that the enclave VM has successfully booted.
From inside an EC2 instance, nitro-cli[3] is used to spawn an enclave VM using an EIF (Enclave Image Format)[4] file. EIF files can be built using nitro-cli as well. The EIF specification can be found in the README of the github aws-nitro-enclaves-image-format repository[4]. An EIF file contains the kernel, cmdline and ramdisk(s) in different sections which are used to boot the enclave VM.
Adding nitro enclave emulation support in QEMU will make the life of AWS Nitro Enclave users easier as they will be able to test their EIF images locally without having to run real nitro enclaves which can be difficult for debugging due to its roots in security. This will also make quick prototyping easier.
In QEMU, the new nitro-enclave machine type is implemented based on the microvm machine type similar to how AWS Nitro Enclaves are based on Firecracker microvm.
The vsock emulation support is added using vhost-user-vsock device. This is needed as nitro VMs always talk to parent VM (CID 3) but there is no support for sibling VM communication in vhost-vsock. So to run nitro-enclave, a process that does vsock emulation in user-space like vhost-device-vsock[5] from rust-vmm must be run. I am working on adding proxying using vsock (right now it uses unix domain socket) to the host machine in vhost-device-vsock which I will be posting a PR to the rust-vmm repo. This will allow users to run the necessary parent VM applications in the host machine instead of a separate VM with CID 3. Once that work is done, I will update the documentation in docs/system/i386/nitro-enclave.rst accordingly.
A new device virtio-nsm support has been added to QEMU. This device is built-into the nitro-enclave VM. The virtio-nsm spec can be found here[6].
For local testing you need to generate a hello.eif image by first building nitro-cli locally[7]. Then you can use nitro-cli to build a hello.eif image[8]. More details about testing can be found in the docs/system/i386/nitro-enclave.rst file.
Thanks.
Regards,
Dorjoy
[1] https://docs.aws.amazon.com/enclaves/latest/user/nitro-enclave.html
[2] https://aws.amazon.com/ec2/
[3] https://docs.aws.amazon.com/enclaves/latest/user/getting-started.html
[4] https://github.com/aws/aws-nitro-enclaves-image-format?tab=readme-ov-file#enclave-image-file-eif-specification
[5] https://github.com/rust-vmm/vhost-device/tree/main/vhost-device-vsock
[6] https://lists.oasis-open.org/archives/virtio-comment/202310/msg00387.html
[7] https://github.com/aws/aws-nitro-enclaves-cli/blob/main/docs/ubuntu_20.04_how_to_install_nitro_cli_from_github_sources.md
[8] https://github.com/aws/aws-nitro-enclaves-cli/blob/main/examples/x86_64/hello/README.md
Dorjoy Chowdhury (5):
machine/nitro-enclave: New machine type for AWS Nitro Enclaves machine/nitro-enclave: Add vhost-user-vsock device device/virtio-nsm: Support for Nitro Secure Module device machine/nitro-enclave: Add built-in Nitro Secure Module device docs/nitro-enclave: Documentation for nitro-enclave machine type
MAINTAINERS | 17 + backends/hostmem-memfd.c | 2 - configs/devices/i386-softmmu/default.mak | 1 + docs/system/i386/nitro-enclave.rst | 80 + hw/core/eif.c | 707 ++++++++ hw/core/eif.h | 22 + hw/core/machine.c | 71 +- hw/core/meson.build | 3 + hw/i386/Kconfig | 6 + hw/i386/meson.build | 1 + hw/i386/microvm.c | 6 +- hw/i386/nitro_enclave.c | 299 ++++ hw/virtio/Kconfig | 5 + hw/virtio/meson.build | 4 + hw/virtio/virtio-nsm-pci.c | 73 + hw/virtio/virtio-nsm.c | 1929 ++++++++++++++++++++++ include/hw/boards.h | 2 + include/hw/i386/microvm.h | 2 + include/hw/i386/nitro_enclave.h | 56 + include/hw/virtio/virtio-nsm.h | 59 + include/sysemu/hostmem.h | 2 + 21 files changed, 3311 insertions(+), 36 deletions(-) create mode 100644 docs/system/i386/nitro-enclave.rst create mode 100644 hw/core/eif.c create mode 100644 hw/core/eif.h create mode 100644 hw/i386/nitro_enclave.c create mode 100644 hw/virtio/virtio-nsm-pci.c create mode 100644 hw/virtio/virtio-nsm.c create mode 100644 include/hw/i386/nitro_enclave.h create mode 100644 include/hw/virtio/virtio-nsm.h
Signature · 1 line
2.39.2
PATCH v3 1/5 machine/nitro-enclave: New machine type for AWS Nitro Enclaves
AWS nitro enclaves[1] is an Amazon EC2[2] feature that allows creating isolated execution environments, called enclaves, from Amazon EC2 instances which are used for processing highly sensitive data. Enclaves have no persistent storage and no external networking. The enclave VMs are based on Firecracker microvm with a vhost-vsock device for communication with the parent EC2 instance that spawned it and a Nitro Secure Module (NSM) device for cryptographic attestation. The parent instance VM always has CID 3 while the enclave VM gets a dynamic CID.
An EIF (Enclave Image Format)[3] file is used to boot an AWS nitro enclave virtual machine. The EIF file contains the necessary kernel, cmdline, ramdisk(s) sections to boot.
This commit adds support for limited AWS nitro enclave emulation using a new machine type option '-M nitro-enclave'. This new machine type is based on the 'microvm' machine type, similar to how real nitro enclave VMs are based on Firecracker microvm. For nitro-enclave to boot from an EIF file, the kernel and ramdisk(s) are extracted into a temporary kernel and a temporary initrd file which are then hooked into the regular x86 boot mechanism along with the extracted cmdline. The EIF file path should be provided using the '-kernel' QEMU option.
The vsock and NSM devices will be implemented so that they are available automatically in nitro-enclave machine type in the following commits.
[1] https://docs.aws.amazon.com/enclaves/latest/user/nitro-enclave.html
[2] https://aws.amazon.com/ec2/
[3] https://github.com/aws/aws-nitro-enclaves-image-format
--- MAINTAINERS | 9 + configs/devices/i386-softmmu/default.mak | 1 + hw/core/eif.c | 514 +++++++++++++++++++++++ hw/core/eif.h | 19 + hw/core/meson.build | 1 + hw/i386/Kconfig | 4 + hw/i386/meson.build | 1 + hw/i386/microvm.c | 6 +- hw/i386/nitro_enclave.c | 93 ++++ include/hw/i386/microvm.h | 2 + include/hw/i386/nitro_enclave.h | 29 ++ 11 files changed, 678 insertions(+), 1 deletion(-) create mode 100644 hw/core/eif.c create mode 100644 hw/core/eif.h create mode 100644 hw/i386/nitro_enclave.c create mode 100644 include/hw/i386/nitro_enclave.h
MAINTAINERS +9 -0
@@ -1877,6 +1877,15 @@ F: hw/i386/microvm.c F: include/hw/i386/microvm.h F: pc-bios/bios-microvm.bin +nitro-enclave +M: Alexander Graf <[email protected]> +M: Dorjoy Chowdhury <[email protected]> +S: Maintained +F: hw/core/eif.c +F: hw/core/eif.h +F: hw/i386/nitro_enclave.c +F: include/hw/i386/nitro_enclave.h + Machine core M: Eduardo Habkost <[email protected]> M: Marcel Apfelbaum <[email protected]>
configs/devices/i386-softmmu/default.mak +1 -0
@@ -29,3 +29,4 @@ # CONFIG_I440FX=n # CONFIG_Q35=n # CONFIG_MICROVM=n +# CONFIG_NITRO_ENCLAVE=n
hw/core/eif.c new file +514 -0
514 lines not on this page · raw message
hw/core/eif.h new file +19 -0
@@ -0,0 +1,19 @@ +/* +* EIF (Enclave Image Format) related helpers +* +* Copyright (c) 2024 Dorjoy Chowdhury <[email protected]> +* +* 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. +*/ + +#ifndef HW_CORE_EIF_H +#define HW_CORE_EIF_H + +bool read_eif_file (const char *eif_path,const char *machine_initrd, +char **kernel_path,char **initrd_path, +char **kernel_cmdline,Error **errp); + +#endif +
hw/core/meson.build +1 -0
@@ -24,6 +24,7 @@ system_ss.add(when: 'CONFIG_REGISTER', if_true: files('register.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(files( 'cpu-sysemu.c',
hw/i386/Kconfig +4 -0
@@ -129,6 +129,10 @@ config MICROVM select USB_XHCI_SYSBUS select I8254 +config NITRO_ENCLAVE + default y + depends on MICROVM + config X86_IOMMU bool depends on PC
hw/i386/meson.build +1 -0
@@ -15,6 +15,7 @@ i386_ss.add(when: 'CONFIG_AMD_IOMMU', if_true: files('amd_iommu.c'), if_false: files('amd_iommu-stub.c')) i386_ss.add(when: 'CONFIG_I440FX', if_true: files('pc_piix.c')) i386_ss.add(when: 'CONFIG_MICROVM', if_true: files('x86-common.c', 'microvm.c', 'acpi-microvm.c', 'microvm-dt.c')) +i386_ss.add(when: 'CONFIG_NITRO_ENCLAVE', if_true: files('nitro_enclave.c')) i386_ss.add(when: 'CONFIG_Q35', if_true: files('pc_q35.c')) i386_ss.add(when: 'CONFIG_VMMOUSE', if_true: files('vmmouse.c')) i386_ss.add(when: 'CONFIG_VMPORT', if_true: files('vmport.c'))
hw/i386/microvm.c +5 -1
@@ -283,6 +283,7 @@ static void microvm_devices_init(MicrovmMachineState *mms)static void microvm_memory_init (MicrovmMachineState *mms) { +MicrovmMachineClass *mmc =MICROVM_MACHINE_GET_CLASS (mms);MachineState *machine =MACHINE (mms);X86MachineState *x86ms =X86_MACHINE (mms);MemoryRegion *ram_below_4g, *ram_above_4g;
@@ -328,7 +329,7 @@ static void microvm_memory_init(MicrovmMachineState *mms)rom_set_fw (fw_cfg);if (machine->kernel_filename !=NULL ) { -x86_load_linux (x86ms, fw_cfg,0 , true); + mmc->x86_load_linux (x86ms, fw_cfg,0 , true); }if (mms->option_roms) {
@@ -637,9 +638,12 @@ GlobalProperty microvm_properties[] = {static void microvm_class_init (ObjectClass *oc,void *data) {X86MachineClass *x86mc =X86_MACHINE_CLASS (oc); +MicrovmMachineClass *mmc =MICROVM_MACHINE_CLASS (oc);MachineClass *mc =MACHINE_CLASS (oc);HotplugHandlerClass *hc =HOTPLUG_HANDLER_CLASS (oc); + mmc->x86_load_linux = x86_load_linux; + mc->init = microvm_machine_state_init; mc->family ="microvm_i386" ;
hw/i386/nitro_enclave.c new file +93 -0
93 lines not on this page · raw message
include/hw/i386/microvm.h +2 -0
@@ -78,6 +78,8 @@ struct MicrovmMachineClass {X86MachineClass parent; HotplugHandler *(*orig_hotplug_handler)(MachineState *machine, DeviceState *dev); +void (*x86_load_linux)(X86MachineState *x86ms,FWCfgState *fw_cfg, +int acpi_data_size,bool pvh_enabled); };struct MicrovmMachineState {
include/hw/i386/nitro_enclave.h new file +29 -0
@@ -0,0 +1,29 @@ +/* +* AWS nitro-enclave machine +* +* Copyright (c) 2024 Dorjoy Chowdhury <[email protected]> +* +* 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. +*/ + +#ifndef HW_I386_NITRO_ENCLAVE_H +#define HW_I386_NITRO_ENCLAVE_H + +#include "hw/i386/microvm.h" +#include "qom/object.h" + +struct NitroEnclaveMachineClass { +MicrovmMachineClass parent; +}; + +struct NitroEnclaveMachineState { +MicrovmMachineState parent; +}; + +#define TYPE_NITRO_ENCLAVE_MACHINE MACHINE_TYPE_NAME("nitro-enclave") +OBJECT_DECLARE_TYPE (NitroEnclaveMachineState, NitroEnclaveMachineClass, + NITRO_ENCLAVE_MACHINE) + +#endif
Signature · 1 line
2.39.2
Re: [PATCH v3 1/5] machine/nitro-enclave: New machine type for AWS Nitro Enclaves
Quoted 23 lines · Dorjoy Chowdhury
AWS nitro enclaves[1] is an Amazon EC2[2] feature that allows creating isolated execution environments, called enclaves, from Amazon EC2 instances which are used for processing highly sensitive data. Enclaves have no persistent storage and no external networking. The enclave VMs are based on Firecracker microvm with a vhost-vsock device for communication with the parent EC2 instance that spawned it and a Nitro Secure Module (NSM) device for cryptographic attestation. The parent instance VM always has CID 3 while the enclave VM gets a dynamic CID.
An EIF (Enclave Image Format)[3] file is used to boot an AWS nitro enclave virtual machine. The EIF file contains the necessary kernel, cmdline, ramdisk(s) sections to boot.
This commit adds support for limited AWS nitro enclave emulation using a new machine type option '-M nitro-enclave'. This new machine type is based on the 'microvm' machine type, similar to how real nitro enclave VMs are based on Firecracker microvm. For nitro-enclave to boot from an EIF file, the kernel and ramdisk(s) are extracted into a temporary kernel and a temporary initrd file which are then hooked into the regular x86 boot mechanism along with the extracted cmdline. The EIF file path should be provided using the '-kernel' QEMU option.
Quoted 8 lines
The vsock and NSM devices will be implemented so that they are available automatically in nitro-enclave machine type in the following commits.
[1] https://docs.aws.amazon.com/enclaves/latest/user/nitro-enclave.html
[2] https://aws.amazon.com/ec2/
[3] https://github.com/aws/aws-nitro-enclaves-image-formatSigned-off-by Dorjoy Chowdhury <[email protected]>
If I run this code with an invalid kernel parameter, something in the error path is off. Can you please try to exercise your error paths to validate they work correctly?
$ ./build/qemu-system-x86_64 -M nitro-enclave -nographic -kernel foobar qemu-system-x86_64: ../util/error.c:68: error_setv: Assertion `*errp == NULL' failed.
Alex
Amazon Web Services Development Center Germany GmbH Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B
Sitz: Berlin
Ust-ID: DE 365 538 597
PATCH v3 2/5 machine/nitro-enclave: Add vhost-user-vsock device
In reply to Dorjoy ChowdhuryAWS Nitro Enclaves have built-in vhost-vsock device support which enables applications in enclave VMs to communicate with the parent EC2 VM over vsock. The enclave VMs have dynamic CID while the parent always has CID 3. In QEMU, the vsock emulation for nitro enclave is added using vhost-user-vsock as opposed to vhost-vsock. vhost-vsock doesn't support sibling VM communication which is needed for nitro enclaves.
In QEMU's nitro-enclave emulation, for the vsock communication to CID 3 to work, another process that does the vsock emulation in userspace must be run, for example, vhost-device-vsock[1] from rust-vmm, with necessary vsock communication support in another guest VM with CID 3. A new mandatory nitro-enclave machine option 'vsock' has been added. The value for this option should be the chardev id from the '-chardev' option for the vhost-user-vsock device to work.
Using vhost-user-vsock also enables the possibility to implement some proxying support in the vhost-user-vsock daemon that will forward all the packets to the host machine instead of CID 3 so that users of nitro-enclave can run the necessary applications in their host machine instead of running another whole VM with CID 3.
[1] https://github.com/rust-vmm/vhost-device/tree/main/vhost-device-vsock
--- backends/hostmem-memfd.c | 2 - hw/core/machine.c | 71 +++++++++--------- hw/i386/Kconfig | 1 + hw/i386/nitro_enclave.c | 123 ++++++++++++++++++++++++++++++++ include/hw/boards.h | 2 + include/hw/i386/nitro_enclave.h | 8 +++ include/sysemu/hostmem.h | 2 + 7 files changed, 174 insertions(+), 35 deletions(-)
backends/hostmem-memfd.c +0 -2
@@ -18,8 +18,6 @@#include "qapi/error.h" #include "qom/object.h" -#define TYPE_MEMORY_BACKEND_MEMFD "memory-backend-memfd" -OBJECT_DECLARE_SIMPLE_TYPE (HostMemoryBackendMemfd,MEMORY_BACKEND_MEMFD )
hw/core/machine.c +38 -33
95 lines not on this page · raw message
hw/i386/Kconfig +1 -0
@@ -132,6 +132,7 @@ config MICROVM config NITRO_ENCLAVE default y depends on MICROVM + select VHOST_USER_VSOCK config X86_IOMMU bool
hw/i386/nitro_enclave.c +123 -0
149 lines not on this page · raw message
include/hw/boards.h +2 -0
@@ -308,6 +308,8 @@ struct MachineClass {int64_t (*get_default_cpu_node_id)(const MachineState *ms,int idx);ram_addr_t (*fixup_ram_size)(ram_addr_t size);uint64_t smbios_memory_device_size; +bool (*create_default_memdev)(MachineState *ms,const char *path, +Error **errp); };/**
include/hw/i386/nitro_enclave.h +8 -0
@@ -14,12 +14,20 @@#include "hw/i386/microvm.h" #include "qom/object.h" +/* Machine type options */ +#define NITRO_ENCLAVE_VSOCK_CHARDEV_ID "vsock" +struct NitroEnclaveMachineClass {MicrovmMachineClass parent; + +void (*parent_init)(MachineState *state); };struct NitroEnclaveMachineState {MicrovmMachineState parent; + +/* Machine type options */ +char *vsock; };#define TYPE_NITRO_ENCLAVE_MACHINE MACHINE_TYPE_NAME("nitro-enclave")
include/sysemu/hostmem.h +2 -0
@@ -39,6 +39,8 @@ OBJECT_DECLARE_TYPE(HostMemoryBackend, HostMemoryBackendClass,*/ #define TYPE_MEMORY_BACKEND_FILE "memory-backend-file" +#define TYPE_MEMORY_BACKEND_MEMFD "memory-backend-memfd" +/** * HostMemoryBackendClass:
Signature · 1 line
2.39.2
Re: [PATCH v3 2/5] machine/nitro-enclave: Add vhost-user-vsock device
Quoted 94 lines · Dorjoy Chowdhury
AWS Nitro Enclaves have built-in vhost-vsock device support which enables applications in enclave VMs to communicate with the parent EC2 VM over vsock. The enclave VMs have dynamic CID while the parent always has CID 3. In QEMU, the vsock emulation for nitro enclave is added using vhost-user-vsock as opposed to vhost-vsock. vhost-vsock doesn't support sibling VM communication which is needed for nitro enclaves.
In QEMU's nitro-enclave emulation, for the vsock communication to CID 3 to work, another process that does the vsock emulation in userspace must be run, for example, vhost-device-vsock[1] from rust-vmm, with necessary vsock communication support in another guest VM with CID 3. A new mandatory nitro-enclave machine option 'vsock' has been added. The value for this option should be the chardev id from the '-chardev' option for the vhost-user-vsock device to work.
Using vhost-user-vsock also enables the possibility to implement some proxying support in the vhost-user-vsock daemon that will forward all the packets to the host machine instead of CID 3 so that users of nitro-enclave can run the necessary applications in their host machine instead of running another whole VM with CID 3.
[1] https://github.com/rust-vmm/vhost-device/tree/main/vhost-device-vsock
Signed-off-by Dorjoy Chowdhury <[email protected]>--- backends/hostmem-memfd.c | 2 - hw/core/machine.c | 71 +++++++++--------- hw/i386/Kconfig | 1 + hw/i386/nitro_enclave.c | 123 ++++++++++++++++++++++++++++++++ include/hw/boards.h | 2 + include/hw/i386/nitro_enclave.h | 8 +++ include/sysemu/hostmem.h | 2 + 7 files changed, 174 insertions(+), 35 deletions(-)hw/i386/nitro_enclave.c +45
-0@@ -11,11 +11,81 @@#include "qemu/osdep.h" #include "qemu/error-report.h" #include "qapi/error.h" +#include "qom/object_interfaces.h" +#include "chardev/char.h" +#include "hw/sysbus.h" #include "hw/core/eif.h" #include "hw/i386/x86.h" #include "hw/i386/microvm.h" #include "hw/i386/nitro_enclave.h" +#include "hw/virtio/virtio-mmio.h" +#include "hw/virtio/vhost-user-vsock.h" +#include "sysemu/hostmem.h" + +static BusState *find_free_virtio_mmio_bus (void ) +{ +BusChild *kid; +BusState *bus =sysbus_get_default (); + + QTAILQ_FOREACH(kid, &bus->children, sibling) { +DeviceState *dev = kid->child; +if (object_dynamic_cast (OBJECT (dev),TYPE_VIRTIO_MMIO )) { +VirtIOMMIOProxy *mmio =VIRTIO_MMIO (OBJECT (dev)); +VirtioBusState *mmio_virtio_bus = &mmio->bus; +BusState *mmio_bus = &mmio_virtio_bus->parent_obj; +if (QTAILQ_EMPTY (&mmio_bus->children)) { +return mmio_bus; + } + } + } + +return NULL ; +} + +static void vhost_user_vsock_init (NitroEnclaveMachineState *nems) +{ +DeviceState *dev =qdev_new (TYPE_VHOST_USER_VSOCK ); +VHostUserVSock *vsock =VHOST_USER_VSOCK (dev); +BusState *bus; + +if (!nems->vsock) { +error_report ("A valid chardev id for vhost-user-vsock device must be " +"provided using the 'vsock' machine option" ); +exit (1 ); + } + + bus =find_free_virtio_mmio_bus (); +if (!bus) { +error_report ("Failed to find bus for vhost-user-vsock device" ); +exit (1 ); + } +
hw/i386/nitro_enclave.c line 73
Quoted 10 lines
hw/i386/nitro_enclave.c +10
-0+Chardev *chardev =qemu_chr_find (nems->vsock); +if (!chardev) { +error_report ("Failed to find chardev with id %s" , nems->vsock); +exit (1 ); + } + + vsock->conf.chardev.chr = chardev; + +qdev_realize_and_unref (dev, bus, &error_fatal); +}
Why does this machine need to create the vhost-user-vsock device itself ? Doing it this way prevents the mgmt app from changing any of the other vsock device settings beyond 'chardev'. The entity creating QEMU can use -device to create the vsock device.
Quoted 141 lines
hw/i386/nitro_enclave.c +68
-0+ +static void nitro_enclave_devices_init (NitroEnclaveMachineState *nems) +{ +vhost_user_vsock_init (nems); +} + +static void nitro_enclave_machine_state_init (MachineState *machine) +{ +NitroEnclaveMachineClass *ne_class = +NITRO_ENCLAVE_MACHINE_GET_CLASS (machine); +NitroEnclaveMachineState *ne_state =NITRO_ENCLAVE_MACHINE (machine); + + ne_class->parent_init (machine); +nitro_enclave_devices_init (ne_state); +}static void nitro_enclave_machine_initfn (Object *obj) {@@ -66,15 +136,68 @@ static void x86_load_eif(X86MachineState *x86ms, FWCfgState *fw_cfg,return ; } +static bool create_memfd_backend (MachineState *ms,const char *path, +Error **errp) +{ +Object *obj; +MachineClass *mc =MACHINE_GET_CLASS (ms); +bool r = false; + + obj =object_new (TYPE_MEMORY_BACKEND_MEMFD ); +if (!object_property_set_int (obj,"size" , ms->ram_size, errp)) { + goto out; + } +object_property_add_child (object_get_objects_root (), mc->default_ram_id, + obj); + +if (!user_creatable_complete (USER_CREATABLE (obj), errp)) { + goto out; + } + r =object_property_set_link (OBJECT (ms),"memory-backend" , obj, errp); + +out: +object_unref (obj); +return r; +} + +static char *nitro_enclave_get_vsock_chardev_id (Object *obj,Error **errp) +{ +NitroEnclaveMachineState *nems =NITRO_ENCLAVE_MACHINE (obj); + +return g_strdup (nems->vsock); +} + +static void nitro_enclave_set_vsock_chardev_id (Object *obj,const char *value, +Error **errp) +{ +NitroEnclaveMachineState *nems =NITRO_ENCLAVE_MACHINE (obj); + +g_free (nems->vsock); + nems->vsock =g_strdup (value); +} +static void nitro_enclave_class_init (ObjectClass *oc,void *data) {MachineClass *mc =MACHINE_CLASS (oc);MicrovmMachineClass *mmc =MICROVM_MACHINE_CLASS (oc); +NitroEnclaveMachineClass *nemc =NITRO_ENCLAVE_MACHINE_CLASS (oc); mmc->x86_load_linux = x86_load_eif; mc->family ="nitro_enclave_i386" ; mc->desc ="AWS Nitro Enclave" ; + + nemc->parent_init = mc->init; + mc->init = nitro_enclave_machine_state_init; + + mc->create_default_memdev = create_memfd_backend; + +object_class_property_add_str (oc,NITRO_ENCLAVE_VSOCK_CHARDEV_ID , + nitro_enclave_get_vsock_chardev_id, + nitro_enclave_set_vsock_chardev_id); +object_class_property_set_description (oc,NITRO_ENCLAVE_VSOCK_CHARDEV_ID , +"Set chardev id for vhost-user-vsock " +"device" ); }static const TypeInfo nitro_enclave_machine_info = {include/hw/boards.h +2
-0@@ -308,6 +308,8 @@ struct MachineClass {int64_t (*get_default_cpu_node_id)(const MachineState *ms,int idx);ram_addr_t (*fixup_ram_size)(ram_addr_t size);uint64_t smbios_memory_device_size; +bool (*create_default_memdev)(MachineState *ms,const char *path, +Error **errp); };/** include/hw/i386/nitro_enclave.h +8
-0@@ -14,12 +14,20 @@#include "hw/i386/microvm.h" #include "qom/object.h" +/* Machine type options */ +#define NITRO_ENCLAVE_VSOCK_CHARDEV_ID "vsock" +struct NitroEnclaveMachineClass {MicrovmMachineClass parent; + +void (*parent_init)(MachineState *state); };struct NitroEnclaveMachineState {MicrovmMachineState parent; + +/* Machine type options */ +char *vsock; };#define TYPE_NITRO_ENCLAVE_MACHINE MACHINE_TYPE_NAME("nitro-enclave")include/sysemu/hostmem.h +2
-0@@ -39,6 +39,8 @@ OBJECT_DECLARE_TYPE(HostMemoryBackend, HostMemoryBackendClass,*/ #define TYPE_MEMORY_BACKEND_FILE "memory-backend-file" +#define TYPE_MEMORY_BACKEND_MEMFD "memory-backend-memfd" +/** * HostMemoryBackendClass: Signature · 1 line
2.39.2
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH v3 2/5] machine/nitro-enclave: Add vhost-user-vsock device
Quoted 103 lines · Daniel P. Berrangé
Quoted 101 lines · Dorjoy Chowdhury
AWS Nitro Enclaves have built-in vhost-vsock device support which enables applications in enclave VMs to communicate with the parent EC2 VM over vsock. The enclave VMs have dynamic CID while the parent always has CID 3. In QEMU, the vsock emulation for nitro enclave is added using vhost-user-vsock as opposed to vhost-vsock. vhost-vsock doesn't support sibling VM communication which is needed for nitro enclaves.
In QEMU's nitro-enclave emulation, for the vsock communication to CID 3 to work, another process that does the vsock emulation in userspace must be run, for example, vhost-device-vsock[1] from rust-vmm, with necessary vsock communication support in another guest VM with CID 3. A new mandatory nitro-enclave machine option 'vsock' has been added. The value for this option should be the chardev id from the '-chardev' option for the vhost-user-vsock device to work.
Using vhost-user-vsock also enables the possibility to implement some proxying support in the vhost-user-vsock daemon that will forward all the packets to the host machine instead of CID 3 so that users of nitro-enclave can run the necessary applications in their host machine instead of running another whole VM with CID 3.
[1] https://github.com/rust-vmm/vhost-device/tree/main/vhost-device-vsock
Signed-off-by Dorjoy Chowdhury <[email protected]>--- backends/hostmem-memfd.c | 2 - hw/core/machine.c | 71 +++++++++--------- hw/i386/Kconfig | 1 + hw/i386/nitro_enclave.c | 123 ++++++++++++++++++++++++++++++++ include/hw/boards.h | 2 + include/hw/i386/nitro_enclave.h | 8 +++ include/sysemu/hostmem.h | 2 + 7 files changed, 174 insertions(+), 35 deletions(-)hw/i386/nitro_enclave.c +52
-0@@ -11,11 +11,81 @@#include "qemu/osdep.h" #include "qemu/error-report.h" #include "qapi/error.h" +#include "qom/object_interfaces.h" +#include "chardev/char.h" +#include "hw/sysbus.h" #include "hw/core/eif.h" #include "hw/i386/x86.h" #include "hw/i386/microvm.h" #include "hw/i386/nitro_enclave.h" +#include "hw/virtio/virtio-mmio.h" +#include "hw/virtio/vhost-user-vsock.h" +#include "sysemu/hostmem.h" + +static BusState *find_free_virtio_mmio_bus (void ) +{ +BusChild *kid; +BusState *bus =sysbus_get_default (); + + QTAILQ_FOREACH(kid, &bus->children, sibling) { +DeviceState *dev = kid->child; +if (object_dynamic_cast (OBJECT (dev),TYPE_VIRTIO_MMIO )) { +VirtIOMMIOProxy *mmio =VIRTIO_MMIO (OBJECT (dev)); +VirtioBusState *mmio_virtio_bus = &mmio->bus; +BusState *mmio_bus = &mmio_virtio_bus->parent_obj; +if (QTAILQ_EMPTY (&mmio_bus->children)) { +return mmio_bus; + } + } + } + +return NULL ; +} + +static void vhost_user_vsock_init (NitroEnclaveMachineState *nems) +{ +DeviceState *dev =qdev_new (TYPE_VHOST_USER_VSOCK ); +VHostUserVSock *vsock =VHOST_USER_VSOCK (dev); +BusState *bus; + +if (!nems->vsock) { +error_report ("A valid chardev id for vhost-user-vsock device must be " +"provided using the 'vsock' machine option" ); +exit (1 ); + } + + bus =find_free_virtio_mmio_bus (); +if (!bus) { +error_report ("Failed to find bus for vhost-user-vsock device" ); +exit (1 ); + } + +Chardev *chardev =qemu_chr_find (nems->vsock); +if (!chardev) { +error_report ("Failed to find chardev with id %s" , nems->vsock); +exit (1 ); + } + + vsock->conf.chardev.chr = chardev;
Quoted 9 lines
hw/i386/nitro_enclave.c line 73
Quoted 3 lines
hw/i386/nitro_enclave.c +3
-0+ +qdev_realize_and_unref (dev, bus, &error_fatal); +}Why does this machine need to create the vhost-user-vsock device itself ? Doing it this way prevents the mgmt app from changing any of the other vsock device settings beyond 'chardev'. The entity creating QEMU can use -device to create the vsock device.
Hi Daniel. Good point. The reason to make the vhost-user-vsock device built-in is because nitro VMs will always need it anyway (like the virtio-nsm device which is built-in too). When an EIF image is built using nitro-cli the "init" process in the EIF image tries to connect to (AF_VSOCK, CID 3, port 9000) to send a heartbeat and expects a heartbeat reply. So my understanding is that if we don't create it inside the machine code itself, users would always need to provide the explicit options for the device anyway. But as you point out this also makes the device settings non-configurable.
Hey Alex, do you have any suggestions on this?
Regards,
Dorjoy
Re: [PATCH v3 2/5] machine/nitro-enclave: Add vhost-user-vsock device
Quoted 120 lines · Dorjoy Chowdhury
Quoted 100 lines · Daniel P. Berrangé
Quoted 99 lines · Dorjoy Chowdhury
AWS Nitro Enclaves have built-in vhost-vsock device support which enables applications in enclave VMs to communicate with the parent EC2 VM over vsock. The enclave VMs have dynamic CID while the parent always has CID 3. In QEMU, the vsock emulation for nitro enclave is added using vhost-user-vsock as opposed to vhost-vsock. vhost-vsock doesn't support sibling VM communication which is needed for nitro enclaves.
In QEMU's nitro-enclave emulation, for the vsock communication to CID 3 to work, another process that does the vsock emulation in userspace must be run, for example, vhost-device-vsock[1] from rust-vmm, with necessary vsock communication support in another guest VM with CID 3. A new mandatory nitro-enclave machine option 'vsock' has been added. The value for this option should be the chardev id from the '-chardev' option for the vhost-user-vsock device to work.
Using vhost-user-vsock also enables the possibility to implement some proxying support in the vhost-user-vsock daemon that will forward all the packets to the host machine instead of CID 3 so that users of nitro-enclave can run the necessary applications in their host machine instead of running another whole VM with CID 3.
[1] https://github.com/rust-vmm/vhost-device/tree/main/vhost-device-vsock
Signed-off-by Dorjoy Chowdhury <[email protected]>--- backends/hostmem-memfd.c | 2 - hw/core/machine.c | 71 +++++++++--------- hw/i386/Kconfig | 1 + hw/i386/nitro_enclave.c | 123 ++++++++++++++++++++++++++++++++ include/hw/boards.h | 2 + include/hw/i386/nitro_enclave.h | 8 +++ include/sysemu/hostmem.h | 2 + 7 files changed, 174 insertions(+), 35 deletions(-)hw/i386/nitro_enclave.c +51
-0@@ -11,11 +11,81 @@#include "qemu/osdep.h" #include "qemu/error-report.h" #include "qapi/error.h" +#include "qom/object_interfaces.h" +#include "chardev/char.h" +#include "hw/sysbus.h" #include "hw/core/eif.h" #include "hw/i386/x86.h" #include "hw/i386/microvm.h" #include "hw/i386/nitro_enclave.h" +#include "hw/virtio/virtio-mmio.h" +#include "hw/virtio/vhost-user-vsock.h" +#include "sysemu/hostmem.h" + +static BusState *find_free_virtio_mmio_bus (void ) +{ +BusChild *kid; +BusState *bus =sysbus_get_default (); + + QTAILQ_FOREACH(kid, &bus->children, sibling) { +DeviceState *dev = kid->child; +if (object_dynamic_cast (OBJECT (dev),TYPE_VIRTIO_MMIO )) { +VirtIOMMIOProxy *mmio =VIRTIO_MMIO (OBJECT (dev)); +VirtioBusState *mmio_virtio_bus = &mmio->bus; +BusState *mmio_bus = &mmio_virtio_bus->parent_obj; +if (QTAILQ_EMPTY (&mmio_bus->children)) { +return mmio_bus; + } + } + } + +return NULL ; +} + +static void vhost_user_vsock_init (NitroEnclaveMachineState *nems) +{ +DeviceState *dev =qdev_new (TYPE_VHOST_USER_VSOCK ); +VHostUserVSock *vsock =VHOST_USER_VSOCK (dev); +BusState *bus; + +if (!nems->vsock) { +error_report ("A valid chardev id for vhost-user-vsock device must be " +"provided using the 'vsock' machine option" ); +exit (1 ); + } + + bus =find_free_virtio_mmio_bus (); +if (!bus) { +error_report ("Failed to find bus for vhost-user-vsock device" ); +exit (1 ); + } + +Chardev *chardev =qemu_chr_find (nems->vsock); +if (!chardev) { +error_report ("Failed to find chardev with id %s" , nems->vsock); +exit (1 ); + } +Quoted 9 lines
hw/i386/nitro_enclave.c line 73
Quoted 4 lines
hw/i386/nitro_enclave.c +4
-0+ vsock->conf.chardev.chr = chardev; + +qdev_realize_and_unref (dev, bus, &error_fatal); +}Why does this machine need to create the vhost-user-vsock device itself ? Doing it this way prevents the mgmt app from changing any of the other vsock device settings beyond 'chardev'. The entity creating QEMU can use -device to create the vsock device.
Hi Daniel. Good point. The reason to make the vhost-user-vsock device built-in is because nitro VMs will always need it anyway (like the virtio-nsm device which is built-in too). When an EIF image is built using nitro-cli the "init" process in the EIF image tries to connect to (
AF_VSOCK, CID 3, port 9000) to send a heartbeat and expects a heartbeat reply. So my understanding is that if we don't create it inside the machine code itself, users would always need to provide the explicit options for the device anyway. But as you point out this also makes the device settings non-configurable.
Quoted 1 line
Hey Alex, do you have any suggestions on this?
IMHO devices that are required for the machine to function should be part of the machine. Since vsock is a core part of the Nitro Enclave, it should be part of the machine definition. It's as much a board dependency as fw_cfg.
Alex
Amazon Web Services Development Center Germany GmbH Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B
Sitz: Berlin
Ust-ID: DE 365 538 597
PATCH v3 3/5 device/virtio-nsm: Support for Nitro Secure Module device
In reply to Dorjoy ChowdhuryNitro Secure Module (NSM)[1] device is used in AWS Nitro Enclaves for stripped down TPM functionality like cryptographic attestation. The requests to and responses from NSM device are CBOR[2] encoded.
This commit adds support for NSM device in QEMU. Although related to AWS Nitro Enclaves, the virito-nsm device is independent and can be used in other machine types as well. The libcbor[3] library has been used for the CBOR encoding and decoding functionalities.
[1] https://lists.oasis-open.org/archives/virtio-comment/202310/msg00387.html
[2] http://cbor.io/
[3] https://libcbor.readthedocs.io/en/latest/
--- MAINTAINERS | 8 + hw/virtio/Kconfig | 5 + hw/virtio/meson.build | 4 + hw/virtio/virtio-nsm-pci.c | 73 ++ hw/virtio/virtio-nsm.c | 1929 ++++++++++++++++++++++++++++++++ include/hw/virtio/virtio-nsm.h | 59 + 6 files changed, 2078 insertions(+) create mode 100644 hw/virtio/virtio-nsm-pci.c create mode 100644 hw/virtio/virtio-nsm.c create mode 100644 include/hw/virtio/virtio-nsm.h
MAINTAINERS +8 -0
@@ -2349,6 +2349,14 @@ F: include/sysemu/rng*.h F: backends/rng*.c F: tests/qtest/virtio-rng-test.c +virtio-nsm +M: Alexander Graf <[email protected]> +M: Dorjoy Chowdhury <[email protected]> +S: Maintained +F: hw/virtio/virtio-nsm.c +F: hw/virtio/virtio-nsm-pci.c +F: include/hw/virtio/virtio-nsm.h + vhost-user-stubs M: Alex Bennée <[email protected]> S: Maintained
hw/virtio/Kconfig +5 -0
@@ -6,6 +6,11 @@ config VIRTIO_RNG default y depends on VIRTIO +config VIRTIO_NSM + bool + default y + depends on VIRTIO + config VIRTIO_IOMMU bool default y
hw/virtio/meson.build +4 -0
@@ -48,12 +48,15 @@ else system_virtio_ss.add(files('vhost-stub.c')) endif +libcbor = dependency('libcbor', version: '>=0.8.0') + specific_virtio_ss.add(when: 'CONFIG_VIRTIO_BALLOON', if_true: files('virtio-balloon.c')) specific_virtio_ss.add(when: 'CONFIG_VHOST_USER_FS', if_true: files('vhost-user-fs.c')) specific_virtio_ss.add(when: 'CONFIG_VIRTIO_PMEM', if_true: files('virtio-pmem.c')) specific_virtio_ss.add(when: 'CONFIG_VHOST_VSOCK', if_true: files('vhost-vsock.c')) specific_virtio_ss.add(when: 'CONFIG_VHOST_USER_VSOCK', if_true: files('vhost-user-vsock.c')) specific_virtio_ss.add(when: 'CONFIG_VIRTIO_RNG', if_true: files('virtio-rng.c')) +specific_virtio_ss.add(when: 'CONFIG_VIRTIO_NSM', if_true: [files('virtio-nsm.c'), libcbor]) specific_virtio_ss.add(when: 'CONFIG_VIRTIO_MEM', if_true: files('virtio-mem.c')) specific_virtio_ss.add(when: 'CONFIG_VHOST_USER_SCMI', if_true: files('vhost-user-scmi.c')) specific_virtio_ss.add(when: ['CONFIG_VIRTIO_PCI', 'CONFIG_VHOST_USER_SCMI'], if_true: files('vhost-user-scmi-pci.c'))
@@ -70,6 +73,7 @@ virtio_pci_ss.add(when: 'CONFIG_VIRTIO_CRYPTO', if_true: files('virtio-crypto-pc virtio_pci_ss.add(when: 'CONFIG_VIRTIO_INPUT_HOST', if_true: files('virtio-input-host-pci.c')) virtio_pci_ss.add(when: 'CONFIG_VIRTIO_INPUT', if_true: files('virtio-input-pci.c')) virtio_pci_ss.add(when: 'CONFIG_VIRTIO_RNG', if_true: files('virtio-rng-pci.c')) +virtio_pci_ss.add(when: 'CONFIG_VIRTIO_NSM', if_true: [files('virtio-nsm-pci.c'), libcbor]) virtio_pci_ss.add(when: 'CONFIG_VIRTIO_BALLOON', if_true: files('virtio-balloon-pci.c')) virtio_pci_ss.add(when: 'CONFIG_VIRTIO_9P', if_true: files('virtio-9p-pci.c')) virtio_pci_ss.add(when: 'CONFIG_VIRTIO_SCSI', if_true: files('virtio-scsi-pci.c'))
hw/virtio/virtio-nsm-pci.c new file +73 -0
73 lines not on this page · raw message
hw/virtio/virtio-nsm.c new file +1929 -0
1,929 lines not on this page · raw message
include/hw/virtio/virtio-nsm.h new file +59 -0
59 lines not on this page · raw message
Signature · 1 line
2.39.2
Re: [PATCH v3 3/5] device/virtio-nsm: Support for Nitro Secure Module device
Quoted 213 lines · Dorjoy Chowdhury
Nitro Secure Module (NSM)[1] device is used in AWS Nitro Enclaves for stripped down TPM functionality like cryptographic attestation. The requests to and responses from NSM device are CBOR[2] encoded.
This commit adds support for NSM device in QEMU. Although related to AWS Nitro Enclaves, the virito-nsm device is independent and can be used in other machine types as well. The libcbor[3] library has been used for the CBOR encoding and decoding functionalities.
[1] https://lists.oasis-open.org/archives/virtio-comment/202310/msg00387.html
[2] http://cbor.io/
[3] https://libcbor.readthedocs.io/en/latest/Signed-off-by Dorjoy Chowdhury <[email protected]>--- MAINTAINERS | 8 + hw/virtio/Kconfig | 5 + hw/virtio/meson.build | 4 + hw/virtio/virtio-nsm-pci.c | 73 ++ hw/virtio/virtio-nsm.c | 1929 ++++++++++++++++++++++++++++++++ include/hw/virtio/virtio-nsm.h | 59 + 6 files changed, 2078 insertions(+) create mode 100644 hw/virtio/virtio-nsm-pci.c create mode 100644 hw/virtio/virtio-nsm.c create mode 100644 include/hw/virtio/virtio-nsm.hMAINTAINERS +8
-0@@ -2349,6 +2349,14 @@ F: include/sysemu/rng*.h F: backends/rng*.c F: tests/qtest/virtio-rng-test.c +virtio-nsm +M: Alexander Graf <[email protected]> +M: Dorjoy Chowdhury <[email protected]> +S: Maintained +F: hw/virtio/virtio-nsm.c +F: hw/virtio/virtio-nsm-pci.c +F: include/hw/virtio/virtio-nsm.h + vhost-user-stubs M: Alex Bennée <[email protected]> S: Maintainedhw/virtio/Kconfig +5
-0@@ -6,6 +6,11 @@ config VIRTIO_RNG default y depends on VIRTIO +config VIRTIO_NSM + bool + default y + depends on VIRTIO + config VIRTIO_IOMMU bool default yhw/virtio/meson.build +4
-0@@ -48,12 +48,15 @@ else system_virtio_ss.add(files('vhost-stub.c')) endif +libcbor = dependency('libcbor', version: '>=0.8.0') + specific_virtio_ss.add(when: 'CONFIG_VIRTIO_BALLOON', if_true: files('virtio-balloon.c')) specific_virtio_ss.add(when: 'CONFIG_VHOST_USER_FS', if_true: files('vhost-user-fs.c')) specific_virtio_ss.add(when: 'CONFIG_VIRTIO_PMEM', if_true: files('virtio-pmem.c')) specific_virtio_ss.add(when: 'CONFIG_VHOST_VSOCK', if_true: files('vhost-vsock.c')) specific_virtio_ss.add(when: 'CONFIG_VHOST_USER_VSOCK', if_true: files('vhost-user-vsock.c')) specific_virtio_ss.add(when: 'CONFIG_VIRTIO_RNG', if_true: files('virtio-rng.c')) +specific_virtio_ss.add(when: 'CONFIG_VIRTIO_NSM', if_true: [files('virtio-nsm.c'), libcbor]) specific_virtio_ss.add(when: 'CONFIG_VIRTIO_MEM', if_true: files('virtio-mem.c')) specific_virtio_ss.add(when: 'CONFIG_VHOST_USER_SCMI', if_true: files('vhost-user-scmi.c')) specific_virtio_ss.add(when: ['CONFIG_VIRTIO_PCI', 'CONFIG_VHOST_USER_SCMI'], if_true: files('vhost-user-scmi-pci.c'))@@ -70,6 +73,7 @@ virtio_pci_ss.add(when: 'CONFIG_VIRTIO_CRYPTO', if_true: files('virtio-crypto-pc virtio_pci_ss.add(when: 'CONFIG_VIRTIO_INPUT_HOST', if_true: files('virtio-input-host-pci.c')) virtio_pci_ss.add(when: 'CONFIG_VIRTIO_INPUT', if_true: files('virtio-input-pci.c')) virtio_pci_ss.add(when: 'CONFIG_VIRTIO_RNG', if_true: files('virtio-rng-pci.c')) +virtio_pci_ss.add(when: 'CONFIG_VIRTIO_NSM', if_true: [files('virtio-nsm-pci.c'), libcbor]) virtio_pci_ss.add(when: 'CONFIG_VIRTIO_BALLOON', if_true: files('virtio-balloon-pci.c')) virtio_pci_ss.add(when: 'CONFIG_VIRTIO_9P', if_true: files('virtio-9p-pci.c')) virtio_pci_ss.add(when: 'CONFIG_VIRTIO_SCSI', if_true: files('virtio-scsi-pci.c'))hw/virtio/virtio-nsm-pci.c new file +73
-0@@ -0,0 +1,73 @@ +/* +* AWS Nitro Secure Module (NSM) device +* +* Copyright (c) 2024 Dorjoy Chowdhury <[email protected]> +* +* 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 "hw/virtio/virtio-pci.h" +#include "hw/virtio/virtio-nsm.h" +#include "hw/qdev-properties.h" +#include "qapi/error.h" +#include "qemu/module.h" +#include "qom/object.h" + +typedef struct VirtIONsmPCI VirtIONsmPCI ; + +#define TYPE_VIRTIO_NSM_PCI "virtio-nsm-pci-base" +DECLARE_INSTANCE_CHECKER (VirtIONsmPCI,VIRTIO_NSM_PCI , + TYPE_VIRTIO_NSM_PCI) + +struct VirtIONsmPCI { +VirtIOPCIProxy parent_obj; +VirtIONSM vdev; +}; + +static void virtio_nsm_pci_realize (VirtIOPCIProxy *vpci_dev,Error **errp) +{ +VirtIONsmPCI *vnsm =VIRTIO_NSM_PCI (vpci_dev); +DeviceState *vdev =DEVICE (&vnsm->vdev); + +virtio_pci_force_virtio_1 (vpci_dev); + +if (!qdev_realize (vdev,BUS (&vpci_dev->bus), errp)) { +return ; + } +} + +static void virtio_nsm_pci_class_init (ObjectClass *klass,void *data) +{ +DeviceClass *dc =DEVICE_CLASS (klass); +VirtioPCIClass *k =VIRTIO_PCI_CLASS (klass); + + k->realize = virtio_nsm_pci_realize; +set_bit (DEVICE_CATEGORY_MISC , dc->categories); +} + +static void virtio_nsm_initfn (Object *obj) +{ +VirtIONsmPCI *dev =VIRTIO_NSM_PCI (obj); + +virtio_instance_init_common (obj, &dev->vdev,sizeof (dev->vdev), +TYPE_VIRTIO_NSM ); +} + +static const VirtioPCIDeviceTypeInfo virtio_nsm_pci_info = { + .base_name =TYPE_VIRTIO_NSM_PCI , + .generic_name ="virtio-nsm-pci" , + .instance_size =sizeof (VirtIONsmPCI), + .instance_init = virtio_nsm_initfn, + .class_init = virtio_nsm_pci_class_init, +}; + +static void virtio_nsm_pci_register (void ) +{ +virtio_pci_types_register (&virtio_nsm_pci_info); +} + +type_init (virtio_nsm_pci_register)hw/virtio/virtio-nsm.c new file +39
-0@@ -0,0 +1,1929 @@ +/* +* AWS Nitro Secure Module (NSM) device +* +* Copyright (c) 2024 Dorjoy Chowdhury <[email protected]> +* +* 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/guest-random.h" +#include "qapi/error.h" +#include <cbor.h> + +#include "hw/virtio/virtio.h" +#include "hw/virtio/virtio-nsm.h" +#include "standard-headers/linux/virtio_ids.h" + +#define NSM_PCR_DATA_REQ_MAX_SIZE 512 + +enum NSMResponseTypes { +NSM_SUCCESS =0 , +NSM_INVALID_ARGUMENT =1 , +NSM_INVALID_INDEX =2 , +NSM_READONLY_INDEX =3 , +NSM_INVALID_OPERATION =4 , +NSM_BUFFER_TOO_SMALL =5 , +NSM_INPUT_TOO_LARGE =6 , +NSM_INTERNAL_ERROR =7 , +}; + +static bool qemu_cbor_map_add (cbor_item_t *map,cbor_item_t *key, +cbor_item_t *value) +{ +bool success = false; +struct cbor_pair pair = (struct cbor_pair ) { + .key =cbor_move (key), + .value =cbor_move (value)
hw/virtio/virtio-nsm.c line 49
Quoted 10 lines
hw/virtio/virtio-nsm.c new file +10
-0+ }; + + success =cbor_map_add (map, pair); +if (!success) { +cbor_incref (pair.key); +cbor_incref (pair.value); + } + +return success; +}
I'd suggest all these 'qemu_cbor_' helper methods could be split off into a separate file, since this virtio-nsm.c is pretty large with all these helpers inlines.
Quoted 7 lines
hw/virtio/virtio-nsm.c new file +7
-0+static bool extend_pcr (VirtIONSM *vnsm,int ind,uint8_t *data,uint16_t len) +{ +GChecksum *hasher =g_checksum_new (G_CHECKSUM_SHA384 ); +struct PCRInfo *pcr = &(vnsm->pcrs[ind]); +size_t digest_len =SHA384_BYTE_LEN ; +if (!hasher) { +return false;
hw/virtio/virtio-nsm.c line 1853
Quoted 10 lines
hw/virtio/virtio-nsm.c new file +10
-0+ } + +g_checksum_update (hasher, pcr->data,SHA384_BYTE_LEN ); +g_checksum_update (hasher, data, len); + +g_checksum_get_digest (hasher, pcr->data, &digest_len); + +g_checksum_free (hasher); +return true; +}
Use the qcrypto hash APIs instead of GChecksum please.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH v3 3/5] device/virtio-nsm: Support for Nitro Secure Module device
In reply to Dorjoy ChowdhuryQuoted 26 lines · Dorjoy Chowdhury
Nitro Secure Module (NSM)[1] device is used in AWS Nitro Enclaves for stripped down TPM functionality like cryptographic attestation. The requests to and responses from NSM device are CBOR[2] encoded.
This commit adds support for NSM device in QEMU. Although related to AWS Nitro Enclaves, the virito-nsm device is independent and can be used in other machine types as well. The libcbor[3] library has been used for the CBOR encoding and decoding functionalities.
[1] https://lists.oasis-open.org/archives/virtio-comment/202310/msg00387.html
[2] http://cbor.io/
[3] https://libcbor.readthedocs.io/en/latest/Signed-off-by Dorjoy Chowdhury <[email protected]>--- MAINTAINERS | 8 + hw/virtio/Kconfig | 5 + hw/virtio/meson.build | 4 + hw/virtio/virtio-nsm-pci.c | 73 ++ hw/virtio/virtio-nsm.c | 1929 ++++++++++++++++++++++++++++++++ include/hw/virtio/virtio-nsm.h | 59 + 6 files changed, 2078 insertions(+)create mode 100644
hw/virtio/virtio-nsm-pci.ccreate mode 100644hw/virtio/virtio-nsm.ccreate mode 100644include/hw/virtio/virtio-nsm.h
[...]
Quoted 349 lines
hw/virtio/virtio-nsm.c new file +349
-0+ +/* +* Attestation request structure: +* +* Map(1) { +* key = String("Attestation"), +* value = Map(3) { +* key = String("user_data"), +* value = Byte_String() || null, +* key = String("nonce"), +* value = Byte_String() || null, +* key = String("public_key"), +* value = Byte_String() || null, +* } +* } +* } +*/ +typedef struct NSMAttestationReq { +uint16_t user_data_len; +uint8_t user_data[NSM_USER_DATA_MAX_SIZE ]; + +uint16_t nonce_len; +uint8_t nonce[NSM_NONCE_MAX_SIZE ]; + +uint16_t public_key_len; +uint8_t public_key[NSM_PUBLIC_KEY_MAX_SIZE ]; +}NSMAttestationReq ; + +static enum NSMResponseTypes get_nsm_attestation_req (uint8_t *req,size_t len, +NSMAttestationReq *nsm_req) +{ +cbor_item_t *item =NULL ; +size_t size; +uint8_t *str; +struct cbor_pair *pair; +struct cbor_load_result result; +enum NSMResponseTypes r =NSM_INVALID_ARGUMENT ; + + item =cbor_load (req, len, &result); +if (!item || result.error.code !=CBOR_ERR_NONE ) { + goto cleanup; + } + + pair =cbor_map_handle (item); +if (!cbor_isa_map (pair->value) ||cbor_map_size (pair->value) !=3 ) { + goto cleanup; + } + pair =cbor_map_handle (pair->value); +if (!cbor_isa_string (pair->key)) { + goto cleanup; + } + str =cbor_string_handle (pair->key); + size =cbor_string_length (pair->key); +if (!str || size !=9 ||memcmp (str,"user_data" ,9 ) !=0 ) { + goto cleanup; + } + +if (cbor_isa_bytestring (pair->value)) { + str =cbor_bytestring_handle (pair->value); + size =cbor_bytestring_length (pair->value); +if (!str || size ==0 ) { + goto cleanup; + } +if (size >NSM_USER_DATA_MAX_SIZE ) { + r =NSM_INPUT_TOO_LARGE ; + goto cleanup; + } +memcpy (nsm_req->user_data, str, size); + nsm_req->user_data_len = size; + }else if (cbor_is_null (pair->value)) { + nsm_req->user_data_len =0 ; + }else { + goto cleanup; + } + +/* let's move forward */ + pair++; +if (!cbor_isa_string (pair->key)) { + goto cleanup; + } + str =cbor_string_handle (pair->key); + size =cbor_string_length (pair->key); +if (!str || size !=5 ||memcmp (str,"nonce" ,5 ) !=0 ) { + goto cleanup; + } + +if (cbor_isa_bytestring (pair->value)) { + str =cbor_bytestring_handle (pair->value); + size =cbor_bytestring_length (pair->value); +if (!str || size ==0 ) { + goto cleanup; + } +if (size >NSM_NONCE_MAX_SIZE ) { + r =NSM_INPUT_TOO_LARGE ; + goto cleanup; + } +memcpy (nsm_req->nonce, str, size); + nsm_req->nonce_len = size; + }else if (cbor_is_null (pair->value)) { + nsm_req->nonce_len =0 ; + }else { + goto cleanup; + } + +/* let's move forward */ + pair++; +if (!cbor_isa_string (pair->key)) { + goto cleanup; + } + str =cbor_string_handle (pair->key); + size =cbor_string_length (pair->key); +if (!str || size !=10 ||memcmp (str,"public_key" ,10 ) !=0 ) { + goto cleanup; + } + +if (cbor_isa_bytestring (pair->value)) { + str =cbor_bytestring_handle (pair->value); + size =cbor_bytestring_length (pair->value); +if (!str || size ==0 ) { + goto cleanup; + } +if (size >NSM_PUBLIC_KEY_MAX_SIZE ) { + r =NSM_INPUT_TOO_LARGE ; + goto cleanup; + } +memcpy (nsm_req->public_key, str, size); + nsm_req->public_key_len = size; + }else if (cbor_is_null (pair->value)) { + nsm_req->public_key_len =0 ; + }else { + goto cleanup; + } + + r =NSM_SUCCESS ; + + cleanup: +if (item) { +cbor_decref (&item); + } +return r; +} + +static bool qemu_cbor_add_uint64_to_map (cbor_item_t *map,const char *key, +uint64_t value) +{ +cbor_item_t *key_cbor =NULL ; +cbor_item_t *value_cbor =NULL ; + + key_cbor =cbor_build_string (key); +if (!key_cbor) { + goto cleanup; + } + value_cbor =cbor_build_uint64 (value); +if (!value_cbor) { + goto cleanup; + } +if (!qemu_cbor_map_add (map, key_cbor, value_cbor)) { + goto cleanup; + } + +return true; + + cleanup: +if (key_cbor) { +cbor_decref (&key_cbor); + } +if (value_cbor) { +cbor_decref (&value_cbor); + } +return false; +} + +static bool add_protected_header_to_cose (cbor_item_t *cose) +{ +cbor_item_t *map =NULL ; +cbor_item_t *key =NULL ; +cbor_item_t *value =NULL ; +cbor_item_t *bs =NULL ; +size_t len; +bool r = false; +size_t buf_len =4096 ; +uint8_t *buf =g_malloc (buf_len); + + map =cbor_new_definite_map (1 ); +if (!map) { + goto cleanup; + } + key =cbor_build_uint8 (1 ); +if (!key) { + goto cleanup; + } + value =cbor_new_int8 (); +if (!value) { + goto cleanup; + } +cbor_mark_negint (value); +/* we don't actually sign the data, so we use -1 as the 'alg' value */ +cbor_set_uint8 (value,0 ); + +if (!qemu_cbor_map_add (map, key, value)) { + goto cleanup; + } + + len =cbor_serialize (map, buf, buf_len); +if (len ==0 ) { + goto cleanup_map; + } + + bs =cbor_build_bytestring (buf, len); +if (!bs) { + goto cleanup_map; + } +if (!qemu_cbor_array_push (cose, bs)) { +cbor_decref (&bs); + goto cleanup_map; + } + r = true; + goto cleanup_map; + + cleanup: +if (key) { +cbor_decref (&key); + } +if (value) { +cbor_decref (&value); + } + + cleanup_map: +if (map) { +cbor_decref (&map); + } +g_free (buf); +return r; +} + +static bool add_unprotected_header_to_cose (cbor_item_t *cose) +{ +cbor_item_t *map =cbor_new_definite_map (0 ); +if (!map) { + goto cleanup; + } +if (!qemu_cbor_array_push (cose, map)) { + goto cleanup; + } + +return true; + + cleanup: +if (map) { +cbor_decref (&map); + } +return false; +} + +static bool qemu_cbor_add_uint8_key_bytestring_to_map (cbor_item_t *map, +uint8_t key,uint8_t *buf, +size_t len) +{ +cbor_item_t *key_cbor =NULL ; +cbor_item_t *value_cbor =NULL ; + + key_cbor =cbor_build_uint8 (key); +if (!key_cbor) { + goto cleanup; + } + value_cbor =cbor_build_bytestring (buf, len); +if (!value_cbor) { + goto cleanup; + } +if (!qemu_cbor_map_add (map, key_cbor, value_cbor)) { + goto cleanup; + } + +return true; + + cleanup: +if (key_cbor) { +cbor_decref (&key_cbor); + } +if (value_cbor) { +cbor_decref (&value_cbor); + } +return false; +} + +static bool add_ca_bundle_to_payload (cbor_item_t *map) +{ +cbor_item_t *key_cbor =NULL ; +cbor_item_t *value_cbor =NULL ; +cbor_item_t *bs =NULL ; +uint8_t zero[64 ] = {0 }; + + key_cbor =cbor_build_string ("cabundle" ); +if (!key_cbor) { + goto cleanup; + } + value_cbor =cbor_new_definite_array (1 ); +if (!value_cbor) { + goto cleanup; + } + bs =cbor_build_bytestring (zero,64 ); +if (!bs) { + goto cleanup; + } +if (!qemu_cbor_array_push (value_cbor, bs)) { +cbor_decref (&bs); + goto cleanup; + } +if (!qemu_cbor_map_add (map, key_cbor, value_cbor)) { + goto cleanup; + } + +return true; + + cleanup: +if (key_cbor) { +cbor_decref (&key_cbor); + } +if (value_cbor) { +cbor_decref (&value_cbor); + } +return false; +} + +static bool add_payload_to_cose (cbor_item_t *cose,VirtIONSM *vnsm) +{ +cbor_item_t *root =NULL ; +cbor_item_t *nested_map; +cbor_item_t *bs =NULL ; +size_t locked_cnt; +uint8_t ind[NSM_MAX_PCRS ]; +size_t payload_map_size =6 ; +size_t len; +struct PCRInfo *pcr; +uint8_t zero[64 ] = {0 }; +bool r = false; +size_t buf_len =16384 ; +uint8_t *buf =g_malloc (buf_len); + +if (vnsm->public_key_len >0 ) { + payload_map_size++; + } +if (vnsm->user_data_len >0 ) { + payload_map_size++; + } +if (vnsm->nonce_len >0 ) { + payload_map_size++; + } + root =cbor_new_definite_map (payload_map_size);
hw/virtio/virtio-nsm.c line 1439
Quoted 10 lines
hw/virtio/virtio-nsm.c new file +10
-0+if (!root) { + goto cleanup; + } +if (!qemu_cbor_add_string_to_map (root,"module_id" , vnsm->module_id)) { + goto cleanup; + } +if (!qemu_cbor_add_string_to_map (root,"digest" , vnsm->digest)) { + goto cleanup; + } +if (!qemu_cbor_add_uint64_to_map (root,"timestamp" ,time (NULL ))) {
Timestamp is in milliseconds, so you need to multiply by 1000.
Alex
Quoted 31 lines
hw/virtio/virtio-nsm.c new file +31
-0+ goto cleanup; + } + + locked_cnt =0 ; +for (uint8_t i =0 ; i <NSM_MAX_PCRS ; ++i) { +if (vnsm->pcrs[i].locked) { + ind[locked_cnt++] = i; + } + } +if (!qemu_cbor_add_map_to_map (root,"pcrs" , locked_cnt, &nested_map)) { + goto cleanup; + } +for (uint8_t i =0 ; i < locked_cnt; ++i) { + pcr = &(vnsm->pcrs[ind[i]]); +if (!qemu_cbor_add_uint8_key_bytestring_to_map (nested_map, ind[i], + pcr->data, +SHA384_BYTE_LEN )) { + goto cleanup; + } + } +if (!qemu_cbor_add_bytestring_to_map (root,"certificate" , zero,64 )) { + goto cleanup; + } +if (!add_ca_bundle_to_payload (root)) { + goto cleanup; + } +if (vnsm->public_key_len >0 && + !qemu_cbor_add_bytestring_to_map (root,"public_key" , vnsm->public_key, + vnsm->public_key_len)) { + goto cleanup; + }
hw/virtio/virtio-nsm.c line 1480
Quoted 10 lines
hw/virtio/virtio-nsm.c new file +10
-0+if (vnsm->user_data_len >0 && + !qemu_cbor_add_bytestring_to_map (root,"user_data" , vnsm->user_data, + vnsm->user_data_len)) { + goto cleanup; + } +if (vnsm->nonce_len >0 && + !qemu_cbor_add_bytestring_to_map (root,"nonce" , vnsm->nonce, + vnsm->nonce_len)) { + goto cleanup; + }
A real Nitro Enclave attestation doc contains user_data and nonce even when they are empty. I think we should do the same.
Alex
Amazon Web Services Development Center Germany GmbH Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B
Sitz: Berlin
Ust-ID: DE 365 538 597
PATCH v3 4/5 machine/nitro-enclave: Add built-in Nitro Secure Module device
In reply to Dorjoy ChowdhuryAWS 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.
--- 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(-)
hw/core/eif.c +199 -6
@@ -12,6 +12,9 @@#include "qemu/bswap.h" #include "qapi/error.h" #include <zlib.h> /* for crc32 */ +#include <cbor.h> +#include <gnutls/gnutls.h> +#include <gnutls/x509.h> #include "hw/core/eif.h"
@@ -180,6 +183,8 @@ static void safe_unlink(char *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, +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, char **kernel_path, } *crc =crc32 (*crc, kernel, size); +g_checksum_update (image_hasher, kernel, size); +g_checksum_update (bootstrap_hasher, kernel, size);g_free (kernel);fclose (tmp_file);
@@ -230,6 +237,8 @@ static bool read_eif_kernel(FILE *f, uint64_t size, char **kernel_path, }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 =fread (cmdline,1 , size, f);
@@ -239,10 +248,14 @@ static bool read_eif_cmdline(FILE *f, uint64_t size, char *cmdline, } *crc =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; }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, uint64_t size, } *crc =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;
@@ -269,6 +284,125 @@ static bool read_eif_ramdisk(FILE *eif, FILE *initrd, uint64_t size,return false; } +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 =48 ; +gnutls_datum_t datum = {.data = cert, .size = size}; + +gnutls_global_init (); +gnutls_x509_crt_init (&crt); + +if (gnutls_x509_crt_import (crt, &datum,GNUTLS_X509_FMT_PEM ) !=0 ) { +error_setg (errp,"Failed to import certificate" ); + goto cleanup; + } + +if (gnutls_x509_crt_get_fingerprint (crt,GNUTLS_DIG_SHA384 , sha384, + &hash_size) !=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 =NULL ; +uint8_t *cert =NULL ; +cbor_item_t *item =NULL ; +cbor_item_t *pcr0 =NULL ; +size_t len; +struct cbor_pair *pair; +struct cbor_load_result result; + + sig =g_malloc (size); + 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_malloc (len); +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 (!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 =NULL ;FILE *machine_initrd_f =NULL ;
@@ -308,9 +444,33 @@ bool read_eif_file(const char *eif_path, const char *machine_initrd,uint32_t crc =0 ;EifHeader eif_header;bool seen_sections[EIF_SECTION_MAX ] = {false}; - +/* kernel + ramdisks + cmdline sha384 hash */ +GChecksum *image_hasher =NULL ; +/* kernel + boot ramdisk + cmdline sha384 hash */ +GChecksum *bootstrap_hasher =NULL ; +/* application ramdisk(s) hash */ +GChecksum *app_hasher =NULL ; +size_t digest_len; + + *signature_found = false; *kernel_path = *initrd_path = *cmdline =NULL ; + image_hasher =g_checksum_new (G_CHECKSUM_SHA384 ); +if (image_hasher ==NULL ) { +error_setg (errp,"Failed to initialize sha384 hash for image" ); + goto cleanup; + } + bootstrap_hasher =g_checksum_new (G_CHECKSUM_SHA384 ); +if (bootstrap_hasher ==NULL ) { +error_setg (errp,"Failed to initialize sha384 hash for bootstrap" ); + goto cleanup; + } + app_hasher =g_checksum_new (G_CHECKSUM_SHA384 ); +if (app_hasher ==NULL ) { +error_setg (errp,"Failed to initialize sha384 hash for app" ); + goto cleanup; + } + f =fopen (eif_path,"rb" );if (f ==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 *machine_initrd, goto cleanup; }if (!read_eif_kernel (f, section_header.section_size, kernel_path, - &crc, errp)) { + image_hasher, bootstrap_hasher, &crc, errp)) { goto cleanup; }
@@ -389,7 +549,8 @@ bool read_eif_file(const char *eif_path, const char *machine_initrd, } size = section_header.section_size; *cmdline =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] ='\0' ;
@@ -398,7 +559,13 @@ bool read_eif_file(const char *eif_path, const char *machine_initrd, }case EIF_SECTION_RAMDISK : { +/* +* If it's the first ramdisk, we need to hash it into bootstrap, +* otherwise we need to hash it into app. +*/ +GChecksum *hasher = app_hasher;if (!seen_sections[EIF_SECTION_RAMDISK ]) { + hasher = bootstrap_hasher;/* * If this is the first time we are seeing a ramdisk section, * we need to create the initrd temporary file.
@@ -415,12 +582,21 @@ bool read_eif_file(const char *eif_path, const char *machine_initrd, }if (!read_eif_ramdisk (f, initrd_path_f, section_header.section_size, - &crc, errp)) { + image_hasher, hasher, &crc, errp)) { goto cleanup; }break ; } +case EIF_SECTION_SIGNATURE : + *signature_found = true; +if (!get_signature_fingerprint_sha384 (f, + section_header.section_size, + 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, }if (!read_eif_ramdisk (machine_initrd_f, initrd_path_f, - machine_initrd_size, &crc, errp)) { + machine_initrd_size, image_hasher, app_hasher, + &crc, errp)) { goto cleanup; } } + digest_len =48 ; +g_checksum_get_digest (image_hasher, image_sha384, &digest_len); +g_checksum_free (image_hasher); + + digest_len =48 ; +g_checksum_get_digest (bootstrap_hasher, bootstrap_sha384, &digest_len); +g_checksum_free (bootstrap_hasher); + + digest_len =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; 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);
hw/core/eif.h +4 -1
@@ -13,7 +13,10 @@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);#endif
hw/core/meson.build +3 -1
@@ -12,6 +12,8 @@ hwcore_ss.add(files( 'qdev-clock.c', )) +libcbor = dependency('libcbor', version: '>=0.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('register.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]) system_ss.add(files( 'cpu-sysemu.c',
hw/i386/Kconfig +1 -0
@@ -133,6 +133,7 @@ config NITRO_ENCLAVE default y depends on MICROVM select VHOST_USER_VSOCK + select VIRTIO_NSM config X86_IOMMU bool
hw/i386/nitro_enclave.c +84 -1
136 lines not on this page · raw message
include/hw/i386/nitro_enclave.h +19 -0
@@ -13,14 +13,19 @@#include "hw/i386/microvm.h" #include "qom/object.h" +#include "hw/virtio/virtio-nsm.h" + +#define SHA384_BYTE_LEN 48/* Machine type options */ #define NITRO_ENCLAVE_VSOCK_CHARDEV_ID "vsock" +#define NITRO_ENCLAVE_ID "id"struct NitroEnclaveMachineClass {MicrovmMachineClass parent;void (*parent_init)(MachineState *state); +void (*parent_reset)(MachineState *machine,ShutdownCause reason); };struct NitroEnclaveMachineState {
@@ -28,6 +33,20 @@ struct NitroEnclaveMachineState {/* 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; };#define TYPE_NITRO_ENCLAVE_MACHINE MACHINE_TYPE_NAME("nitro-enclave")
Signature · 1 line
2.39.2
Re: [PATCH v3 4/5] machine/nitro-enclave: Add built-in Nitro Secure Module device
Quoted 23 lines · Dorjoy Chowdhury
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 <[email protected]>--- 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(-)
[...]
Quoted 14 lines
hw/core/meson.build +2
-0@@ -12,6 +12,8 @@ hwcore_ss.add(files( 'qdev-clock.c', )) +libcbor = dependency('libcbor', version: '>=0.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'))
hw/core/meson.build system_ss.add(when: 'CONFIG_REGISTER', if_true: files('register.c')) · line 29
Quoted 5 lines
hw/core/meson.build +1
-1@@ -24,7 +26,7 @@ system_ss.add(when: 'CONFIG_REGISTER', if_true: files('register.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])
I think this is missing a dependency check somewhere:
../hw/core/eif.c:16:10: fatal error: gnutls/gnutls.h: No such file or directory 16 | #include <gnutls/gnutls.h>
| ^~~~~~~~~~~~~~~~~
It's also the first time anything accesses gnutls directly instead of through the QEMU crypto framework. Is there a particular reason you can not use qcrypto?
Alex
Amazon Web Services Development Center Germany GmbH Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B
Sitz: Berlin
Ust-ID: DE 365 538 597
Re: [PATCH v3 4/5] machine/nitro-enclave: Add built-in Nitro Secure Module device
Quoted 52 lines · Alexander Graf
Quoted 23 lines · Dorjoy Chowdhury
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 <[email protected]>--- 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(-)[...]
Quoted 14 lines
hw/core/meson.build +2
-0@@ -12,6 +12,8 @@ hwcore_ss.add(files( 'qdev-clock.c', )) +libcbor = dependency('libcbor', version: '>=0.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'))hw/core/meson.build system_ss.add(when: 'CONFIG_REGISTER', if_true: files('register.c')) · line 29
Quoted 5 lines
hw/core/meson.build +1
-1@@ -24,7 +26,7 @@ system_ss.add(when: 'CONFIG_REGISTER', if_true: files('register.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])I think this is missing a dependency check somewhere:
Quoted 8 lines
../hw/core/eif.c:16:10: fatal error:gnutls/gnutls.h: No such file or directory
16 | #include <gnutls/gnutls.h>
| ^~~~~~~~~~~~~~~~~It's also the first time anything accesses gnutls directly instead of through the QEMU crypto framework. Is there a particular reason you can not use qcrypto?
None of the existing qcrypto APIs provide the functionality needed in this patch. The right answer though is to introduce a new API in qcrypto to avoid directly coupling this Nitro code to GNUTLS.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH v3 4/5] machine/nitro-enclave: Add built-in Nitro Secure Module device
In reply to Dorjoy ChowdhuryQuoted 373 lines · Dorjoy Chowdhury
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 <[email protected]>--- 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(-)hw/core/eif.c +199
-6@@ -12,6 +12,9 @@#include "qemu/bswap.h" #include "qapi/error.h" #include <zlib.h> /* for crc32 */ +#include <cbor.h> +#include <gnutls/gnutls.h> +#include <gnutls/x509.h> #include "hw/core/eif.h" @@ -180,6 +183,8 @@ static void safe_unlink(char *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, +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, char **kernel_path, } *crc =crc32 (*crc, kernel, size); +g_checksum_update (image_hasher, kernel, size); +g_checksum_update (bootstrap_hasher, kernel, size);g_free (kernel);fclose (tmp_file);@@ -230,6 +237,8 @@ static bool read_eif_kernel(FILE *f, uint64_t size, char **kernel_path, }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 =fread (cmdline,1 , size, f);@@ -239,10 +248,14 @@ static bool read_eif_cmdline(FILE *f, uint64_t size, char *cmdline, } *crc =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; }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, uint64_t size, } *crc =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;@@ -269,6 +284,125 @@ static bool read_eif_ramdisk(FILE *eif, FILE *initrd, uint64_t size,return false; } +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 =48 ; +gnutls_datum_t datum = {.data = cert, .size = size}; + +gnutls_global_init (); +gnutls_x509_crt_init (&crt); + +if (gnutls_x509_crt_import (crt, &datum,GNUTLS_X509_FMT_PEM ) !=0 ) { +error_setg (errp,"Failed to import certificate" ); + goto cleanup; + } + +if (gnutls_x509_crt_get_fingerprint (crt,GNUTLS_DIG_SHA384 , sha384, + &hash_size) !=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 =NULL ; +uint8_t *cert =NULL ; +cbor_item_t *item =NULL ; +cbor_item_t *pcr0 =NULL ; +size_t len; +struct cbor_pair *pair; +struct cbor_load_result result; + + sig =g_malloc (size); + 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_malloc (len); +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 (!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 =NULL ;FILE *machine_initrd_f =NULL ;@@ -308,9 +444,33 @@ bool read_eif_file(const char *eif_path, const char *machine_initrd,uint32_t crc =0 ;EifHeader eif_header;bool seen_sections[EIF_SECTION_MAX ] = {false}; - +/* kernel + ramdisks + cmdline sha384 hash */ +GChecksum *image_hasher =NULL ; +/* kernel + boot ramdisk + cmdline sha384 hash */ +GChecksum *bootstrap_hasher =NULL ; +/* application ramdisk(s) hash */ +GChecksum *app_hasher =NULL ; +size_t digest_len; + + *signature_found = false; *kernel_path = *initrd_path = *cmdline =NULL ; + image_hasher =g_checksum_new (G_CHECKSUM_SHA384 ); +if (image_hasher ==NULL ) { +error_setg (errp,"Failed to initialize sha384 hash for image" ); + goto cleanup; + } + bootstrap_hasher =g_checksum_new (G_CHECKSUM_SHA384 ); +if (bootstrap_hasher ==NULL ) { +error_setg (errp,"Failed to initialize sha384 hash for bootstrap" ); + goto cleanup; + } + app_hasher =g_checksum_new (G_CHECKSUM_SHA384 ); +if (app_hasher ==NULL ) { +error_setg (errp,"Failed to initialize sha384 hash for app" ); + goto cleanup; + } + f =fopen (eif_path,"rb" );if (f ==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 *machine_initrd, goto cleanup; }if (!read_eif_kernel (f, section_header.section_size, kernel_path, - &crc, errp)) { + image_hasher, bootstrap_hasher, &crc, errp)) { goto cleanup; }@@ -389,7 +549,8 @@ bool read_eif_file(const char *eif_path, const char *machine_initrd, } size = section_header.section_size; *cmdline =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] ='\0' ;@@ -398,7 +559,13 @@ bool read_eif_file(const char *eif_path, const char *machine_initrd, }case EIF_SECTION_RAMDISK : { +/* +* If it's the first ramdisk, we need to hash it into bootstrap, +* otherwise we need to hash it into app. +*/ +GChecksum *hasher = app_hasher;if (!seen_sections[EIF_SECTION_RAMDISK ]) { + hasher = bootstrap_hasher;/* * If this is the first time we are seeing a ramdisk section, * we need to create the initrd temporary file. @@ -415,12 +582,21 @@ bool read_eif_file(const char *eif_path, const char *machine_initrd, }if (!read_eif_ramdisk (f, initrd_path_f, section_header.section_size, - &crc, errp)) { + image_hasher, hasher, &crc, errp)) { goto cleanup; }break ; } +case EIF_SECTION_SIGNATURE : + *signature_found = true; +if (!get_signature_fingerprint_sha384 (f, + section_header.section_size, + 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, }if (!read_eif_ramdisk (machine_initrd_f, initrd_path_f, - machine_initrd_size, &crc, errp)) { + machine_initrd_size, image_hasher, app_hasher, + &crc, errp)) { goto cleanup; } } + digest_len =48 ; +g_checksum_get_digest (image_hasher, image_sha384, &digest_len); +g_checksum_free (image_hasher); + + digest_len =48 ; +g_checksum_get_digest (bootstrap_hasher, bootstrap_sha384, &digest_len); +g_checksum_free (bootstrap_hasher); + + digest_len =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; 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);hw/core/eif.h +4
-1@@ -13,7 +13,10 @@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);#endif hw/core/meson.build +0
-0
hw/core/meson.build hwcore_ss.add(files( · line 15
Quoted 4 lines
hw/core/meson.build +1
-0@@ -12,6 +12,8 @@ hwcore_ss.add(files( 'qdev-clock.c', )) +libcbor = dependency('libcbor', version: '>=0.8.0')
Amazon Linux 2023 only ships with 0.7.0. Fortunately, all of your code seems to work fine with 0.7.0 as well. All I needed to make it compile was to change the version check :)
Alex
Amazon Web Services Development Center Germany GmbH Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B
Sitz: Berlin
Ust-ID: DE 365 538 597
Re: [PATCH v3 4/5] machine/nitro-enclave: Add built-in Nitro Secure Module device
In reply to Dorjoy ChowdhuryQuoted 60 lines · Dorjoy Chowdhury
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 <[email protected]>--- 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(-)hw/core/eif.c +21
-0@@ -12,6 +12,9 @@#include "qemu/bswap.h" #include "qapi/error.h" #include <zlib.h> /* for crc32 */ +#include <cbor.h> +#include <gnutls/gnutls.h> +#include <gnutls/x509.h> #include "hw/core/eif.h" @@ -269,6 +284,125 @@ static bool read_eif_ramdisk(FILE *eif, FILE *initrd, uint64_t size,return false; } +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 =48 ; +gnutls_datum_t datum = {.data = cert, .size = size}; + +gnutls_global_init (); +gnutls_x509_crt_init (&crt); + +if (gnutls_x509_crt_import (crt, &datum,GNUTLS_X509_FMT_PEM ) !=0 ) { +error_setg (errp,"Failed to import certificate" ); + goto cleanup; + } + +if (gnutls_x509_crt_get_fingerprint (crt,GNUTLS_DIG_SHA384 , sha384, + &hash_size) !=0 ) { +error_setg (errp,"Failed to compute SHA384 fingerprint" );
hw/core/eif.c static bool read_eif_ramdisk(FILE *eif, FILE *initrd, uint64_t size, · line 314
Quoted 10 lines
hw/core/eif.c +10
-0+ goto cleanup; + } + +return true; + + cleanup: +gnutls_x509_crt_deinit (crt); +gnutls_global_deinit (); +return false; +}
I'd suggest this go into qcrypto/x509-utils.c & include/qcrypto/x509-utils.h, as:
int qcrypto_get_x509_cert_fingerprint(uint8_t *cert,
size_t size, QCryptoHashAlgorith hash, Error **errp);
there's no need to be calling gnutls_global_init() / deinit() either.
Quoted 32 lines
hw/core/eif.c +17
-2@@ -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 =NULL ;FILE *machine_initrd_f =NULL ;@@ -308,9 +444,33 @@ bool read_eif_file(const char *eif_path, const char *machine_initrd,uint32_t crc =0 ;EifHeader eif_header;bool seen_sections[EIF_SECTION_MAX ] = {false}; - +/* kernel + ramdisks + cmdline sha384 hash */ +GChecksum *image_hasher =NULL ; +/* kernel + boot ramdisk + cmdline sha384 hash */ +GChecksum *bootstrap_hasher =NULL ; +/* application ramdisk(s) hash */ +GChecksum *app_hasher =NULL ; +size_t digest_len; + + *signature_found = false; *kernel_path = *initrd_path = *cmdline =NULL ; + image_hasher =g_checksum_new (G_CHECKSUM_SHA384 ); +if (image_hasher ==NULL ) { +error_setg (errp,"Failed to initialize sha384 hash for image" ); + goto cleanup; + }
hw/core/eif.c bool read_eif_file(const char *eif_path, const char *machine_initrd, · line 472
Quoted 10 lines
hw/core/eif.c +10
-0+ bootstrap_hasher =g_checksum_new (G_CHECKSUM_SHA384 ); +if (bootstrap_hasher ==NULL ) { +error_setg (errp,"Failed to initialize sha384 hash for bootstrap" ); + goto cleanup; + } + app_hasher =g_checksum_new (G_CHECKSUM_SHA384 ); +if (app_hasher ==NULL ) { +error_setg (errp,"Failed to initialize sha384 hash for app" ); + goto cleanup; + }
Don't use GChecksum APIs please, use the qcrypto hash APIs instead, as we need all code to be using the designated QEMU crypto backend.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH v3 4/5] machine/nitro-enclave: Add built-in Nitro Secure Module device
Hi Daniel,
Quoted 121 lines · Daniel P. Berrangé
Quoted 60 lines · Dorjoy Chowdhury
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 <[email protected]>--- 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(-)hw/core/eif.c +21
-0@@ -12,6 +12,9 @@#include "qemu/bswap.h" #include "qapi/error.h" #include <zlib.h> /* for crc32 */ +#include <cbor.h> +#include <gnutls/gnutls.h> +#include <gnutls/x509.h> #include "hw/core/eif.h" @@ -269,6 +284,125 @@ static bool read_eif_ramdisk(FILE *eif, FILE *initrd, uint64_t size,return false; } +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 =48 ; +gnutls_datum_t datum = {.data = cert, .size = size}; + +gnutls_global_init (); +gnutls_x509_crt_init (&crt); + +if (gnutls_x509_crt_import (crt, &datum,GNUTLS_X509_FMT_PEM ) !=0 ) { +error_setg (errp,"Failed to import certificate" ); + goto cleanup; + } + +if (gnutls_x509_crt_get_fingerprint (crt,GNUTLS_DIG_SHA384 , sha384, + &hash_size) !=0 ) { +error_setg (errp,"Failed to compute SHA384 fingerprint" );hw/core/eif.c static bool read_eif_ramdisk(FILE *eif, FILE *initrd, uint64_t size, · line 314
Quoted 10 lines
hw/core/eif.c +10
-0+ goto cleanup; + } + +return true; + + cleanup: +gnutls_x509_crt_deinit (crt); +gnutls_global_deinit (); +return false; +}I'd suggest this go into
qcrypto/x509-utils.c&include/qcrypto/x509-utils.h, as:int qcrypto_get_x509_cert_fingerprint(uint8_t *cert,
size_tsize, QCryptoHashAlgorith hash, Error **errp);there's no need to be calling
gnutls_global_init()/deinit()either.Quoted 37 lines
hw/core/eif.c +22
-2@@ -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 =NULL ;FILE *machine_initrd_f =NULL ;@@ -308,9 +444,33 @@ bool read_eif_file(const char *eif_path, const char *machine_initrd,uint32_t crc =0 ;EifHeader eif_header;bool seen_sections[EIF_SECTION_MAX ] = {false}; - +/* kernel + ramdisks + cmdline sha384 hash */ +GChecksum *image_hasher =NULL ; +/* kernel + boot ramdisk + cmdline sha384 hash */ +GChecksum *bootstrap_hasher =NULL ; +/* application ramdisk(s) hash */ +GChecksum *app_hasher =NULL ; +size_t digest_len; + + *signature_found = false; *kernel_path = *initrd_path = *cmdline =NULL ; + image_hasher =g_checksum_new (G_CHECKSUM_SHA384 ); +if (image_hasher ==NULL ) { +error_setg (errp,"Failed to initialize sha384 hash for image" ); + goto cleanup; + } + bootstrap_hasher =g_checksum_new (G_CHECKSUM_SHA384 ); +if (bootstrap_hasher ==NULL ) { +error_setg (errp,"Failed to initialize sha384 hash for bootstrap" ); + goto cleanup; + }
Quoted 9 lines
hw/core/eif.c bool read_eif_file(const char *eif_path, const char *machine_initrd, · line 472
Quoted 5 lines
hw/core/eif.c +5
-0+ app_hasher =g_checksum_new (G_CHECKSUM_SHA384 ); +if (app_hasher ==NULL ) { +error_setg (errp,"Failed to initialize sha384 hash for app" ); + goto cleanup; + }Don't use GChecksum APIs please, use the qcrypto hash APIs instead, as we need all code to be using the designated QEMU crypto backend.
Thanks for the reviews. I was looking into replacing the GChecksum uses with qcrypto apis and was able to do it in the extendPCR function but I need some help with how I can do this in the eif.c file. For example, the "image_hash" needs to be a SHA384 hash of the kernel, cmdline, ramdisks sections' data as they appear in the order that is in the EIF file. Using GChecksum it was easy as I was able to just pass the hashers to the "read_eif_kernel", "read_eif_ramdisk" etc functions and call "update" on them. But the qcrypto apis are stateless i.e., I would need to pass all the buffers in a single api call so it wouldn't work right now out of the box. Do you have any suggestions how I should modify/create qcrypto apis so that I can easily do this (considering that I would need to implement for different qcrypto backends)? Thanks!
Regards,
Dorjoy
Re: [PATCH v3 4/5] machine/nitro-enclave: Add built-in Nitro Secure Module device
Quoted 134 lines · Dorjoy Chowdhury
Hi Daniel,
Quoted 121 lines · Daniel P. Berrangé
Quoted 60 lines · Dorjoy Chowdhury
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 <[email protected]>--- 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(-)hw/core/eif.c +21
-0@@ -12,6 +12,9 @@#include "qemu/bswap.h" #include "qapi/error.h" #include <zlib.h> /* for crc32 */ +#include <cbor.h> +#include <gnutls/gnutls.h> +#include <gnutls/x509.h> #include "hw/core/eif.h" @@ -269,6 +284,125 @@ static bool read_eif_ramdisk(FILE *eif, FILE *initrd, uint64_t size,return false; } +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 =48 ; +gnutls_datum_t datum = {.data = cert, .size = size}; + +gnutls_global_init (); +gnutls_x509_crt_init (&crt); + +if (gnutls_x509_crt_import (crt, &datum,GNUTLS_X509_FMT_PEM ) !=0 ) { +error_setg (errp,"Failed to import certificate" ); + goto cleanup; + } + +if (gnutls_x509_crt_get_fingerprint (crt,GNUTLS_DIG_SHA384 , sha384, + &hash_size) !=0 ) { +error_setg (errp,"Failed to compute SHA384 fingerprint" );hw/core/eif.c static bool read_eif_ramdisk(FILE *eif, FILE *initrd, uint64_t size, · line 314
Quoted 10 lines
hw/core/eif.c +10
-0+ goto cleanup; + } + +return true; + + cleanup: +gnutls_x509_crt_deinit (crt); +gnutls_global_deinit (); +return false; +}I'd suggest this go into
qcrypto/x509-utils.c&include/qcrypto/x509-utils.h, as:int qcrypto_get_x509_cert_fingerprint(uint8_t *cert,
size_tsize, QCryptoHashAlgorith hash, Error **errp);there's no need to be calling
gnutls_global_init()/deinit()either.Quoted 37 lines
hw/core/eif.c +22
-2@@ -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 =NULL ;FILE *machine_initrd_f =NULL ;@@ -308,9 +444,33 @@ bool read_eif_file(const char *eif_path, const char *machine_initrd,uint32_t crc =0 ;EifHeader eif_header;bool seen_sections[EIF_SECTION_MAX ] = {false}; - +/* kernel + ramdisks + cmdline sha384 hash */ +GChecksum *image_hasher =NULL ; +/* kernel + boot ramdisk + cmdline sha384 hash */ +GChecksum *bootstrap_hasher =NULL ; +/* application ramdisk(s) hash */ +GChecksum *app_hasher =NULL ; +size_t digest_len; + + *signature_found = false; *kernel_path = *initrd_path = *cmdline =NULL ; + image_hasher =g_checksum_new (G_CHECKSUM_SHA384 ); +if (image_hasher ==NULL ) { +error_setg (errp,"Failed to initialize sha384 hash for image" ); + goto cleanup; + } + bootstrap_hasher =g_checksum_new (G_CHECKSUM_SHA384 ); +if (bootstrap_hasher ==NULL ) { +error_setg (errp,"Failed to initialize sha384 hash for bootstrap" ); + goto cleanup; + }Quoted 9 lines
hw/core/eif.c bool read_eif_file(const char *eif_path, const char *machine_initrd, · line 472
Quoted 5 lines
hw/core/eif.c +5
-0+ app_hasher =g_checksum_new (G_CHECKSUM_SHA384 ); +if (app_hasher ==NULL ) { +error_setg (errp,"Failed to initialize sha384 hash for app" ); + goto cleanup; + }Don't use GChecksum APIs please, use the qcrypto hash APIs instead, as we need all code to be using the designated QEMU crypto backend.
Quoted 13 lines
Thanks for the reviews. I was looking into replacing the GChecksum uses with qcrypto apis and was able to do it in the extendPCR function but I need some help with how I can do this in the
eif.cfile. For example, the "image_hash" needs to be a SHA384 hash of the kernel, cmdline, ramdisks sections' data as they appear in the order that is in the EIF file. Using GChecksum it was easy as I was able to just pass the hashers to the "read_eif_kernel", "read_eif_ramdisk" etc functions and call "update" on them. But the qcrypto apis are stateless i.e., I would need to pass all the buffers in a single api call so it wouldn't work right now out of the box. Do you have any suggestions how I should modify/create qcrypto apis so that I can easily do this (considering that I would need to implement for different qcrypto backends)? Thanks!
Looking at the read_eif_* methods:
@ -213,6 +218,8 @@ static bool read_eif_kernel(FILE *f, uint64_t size, char **kernel_path,
}
*crc = crc32(*crc, kernel, size);
+ g_checksum_update(image_hasher, kernel, size);
+ g_checksum_update(bootstrap_hasher, kernel, size);
g_free(kernel);
fclose(tmp_file);@@ -230,6 +237,8 @@ static bool read_eif_kernel(FILE *f, uint64_t size, char **kernel_path,
}
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 = fread(cmdline, 1, size, f);
@@ -239,10 +248,14 @@ static bool read_eif_cmdline(FILE *f, uint64_t size, char *cmdline,
}
*crc = 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;}
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, uint64_t size,
}
*crc = 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;For the kernel + ramdisk, these methods are freeing the buffer. For the cmdline, the pointer is held by the caller.
I'd suggest that read_eif_kernel+ramddisk are changed to return the data pointer instead of free'ing it.
The caller can then stash the kernl+cmdline+ramdisk into an iovec and calc the hash in a single stateless operation.
If you really don't want to do that, then you'll have to wait for this series to be finalized & merged next dev cycle:
https://lists.nongnu.org/archive/html/qemu-devel/2024-08/msg01173.html
It is getting close to ready, so might not be too long, but I can't guarantee a timeframe.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH v3 4/5] machine/nitro-enclave: Add built-in Nitro Secure Module device
Quoted 206 lines · Daniel P. Berrangé
Quoted 134 lines · Dorjoy Chowdhury
Hi Daniel,
Quoted 121 lines · Daniel P. Berrangé
Quoted 60 lines · Dorjoy Chowdhury
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 <[email protected]>--- 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(-)hw/core/eif.c +21
-0@@ -12,6 +12,9 @@#include "qemu/bswap.h" #include "qapi/error.h" #include <zlib.h> /* for crc32 */ +#include <cbor.h> +#include <gnutls/gnutls.h> +#include <gnutls/x509.h> #include "hw/core/eif.h" @@ -269,6 +284,125 @@ static bool read_eif_ramdisk(FILE *eif, FILE *initrd, uint64_t size,return false; } +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 =48 ; +gnutls_datum_t datum = {.data = cert, .size = size}; + +gnutls_global_init (); +gnutls_x509_crt_init (&crt); + +if (gnutls_x509_crt_import (crt, &datum,GNUTLS_X509_FMT_PEM ) !=0 ) { +error_setg (errp,"Failed to import certificate" ); + goto cleanup; + } + +if (gnutls_x509_crt_get_fingerprint (crt,GNUTLS_DIG_SHA384 , sha384, + &hash_size) !=0 ) { +error_setg (errp,"Failed to compute SHA384 fingerprint" );hw/core/eif.c static bool read_eif_ramdisk(FILE *eif, FILE *initrd, uint64_t size, · line 314
Quoted 10 lines
hw/core/eif.c +10
-0+ goto cleanup; + } + +return true; + + cleanup: +gnutls_x509_crt_deinit (crt); +gnutls_global_deinit (); +return false; +}I'd suggest this go into
qcrypto/x509-utils.c&include/qcrypto/x509-utils.h, as:int qcrypto_get_x509_cert_fingerprint(uint8_t *cert,
size_tsize, QCryptoHashAlgorith hash, Error **errp);there's no need to be calling
gnutls_global_init()/deinit()either.Quoted 37 lines
hw/core/eif.c +22
-2@@ -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 =NULL ;FILE *machine_initrd_f =NULL ;@@ -308,9 +444,33 @@ bool read_eif_file(const char *eif_path, const char *machine_initrd,uint32_t crc =0 ;EifHeader eif_header;bool seen_sections[EIF_SECTION_MAX ] = {false}; - +/* kernel + ramdisks + cmdline sha384 hash */ +GChecksum *image_hasher =NULL ; +/* kernel + boot ramdisk + cmdline sha384 hash */ +GChecksum *bootstrap_hasher =NULL ; +/* application ramdisk(s) hash */ +GChecksum *app_hasher =NULL ; +size_t digest_len; + + *signature_found = false; *kernel_path = *initrd_path = *cmdline =NULL ; + image_hasher =g_checksum_new (G_CHECKSUM_SHA384 ); +if (image_hasher ==NULL ) { +error_setg (errp,"Failed to initialize sha384 hash for image" ); + goto cleanup; + } + bootstrap_hasher =g_checksum_new (G_CHECKSUM_SHA384 ); +if (bootstrap_hasher ==NULL ) { +error_setg (errp,"Failed to initialize sha384 hash for bootstrap" ); + goto cleanup; + }Quoted 9 lines
hw/core/eif.c bool read_eif_file(const char *eif_path, const char *machine_initrd, · line 472
Quoted 5 lines
hw/core/eif.c +5
-0+ app_hasher =g_checksum_new (G_CHECKSUM_SHA384 ); +if (app_hasher ==NULL ) { +error_setg (errp,"Failed to initialize sha384 hash for app" ); + goto cleanup; + }Don't use GChecksum APIs please, use the qcrypto hash APIs instead, as we need all code to be using the designated QEMU crypto backend.
Quoted 13 lines
Thanks for the reviews. I was looking into replacing the GChecksum uses with qcrypto apis and was able to do it in the extendPCR function but I need some help with how I can do this in the
eif.cfile. For example, the "image_hash" needs to be a SHA384 hash of the kernel, cmdline, ramdisks sections' data as they appear in the order that is in the EIF file. Using GChecksum it was easy as I was able to just pass the hashers to the "read_eif_kernel", "read_eif_ramdisk" etc functions and call "update" on them. But the qcrypto apis are stateless i.e., I would need to pass all the buffers in a single api call so it wouldn't work right now out of the box. Do you have any suggestions how I should modify/create qcrypto apis so that I can easily do this (considering that I would need to implement for different qcrypto backends)? Thanks!Looking at the read_eif_* methods:
@ -213,6 +218,8 @@ static bool read_eif_kernel(FILE *f,
uint64_tsize, char **kernel_path,} *crc = crc32(*crc, kernel, size);hw/core/eif.c +10
-0+g_checksum_update (image_hasher, kernel, size); +g_checksum_update (bootstrap_hasher, kernel, size);g_free (kernel);fclose (tmp_file);@@ -230,6 +237,8 @@ static bool read_eif_kernel(FILE *f, uint64_t size, char **kernel_path, }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 =fread (cmdline,1 , size, f);@@ -239,10 +248,14 @@ static bool read_eif_cmdline(FILE *f, uint64_t size, char *cmdline, } *crc =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; }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, uint64_t size, } *crc =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;For the kernel + ramdisk, these methods are freeing the buffer. For the cmdline, the pointer is held by the caller.
I'd suggest that read_eif_kernel+ramddisk are changed to return the data pointer instead of free'ing it.
The caller can then stash the kernl+cmdline+ramdisk into an iovec and calc the hash in a single stateless operation.
Quoted 8 lines
If you really don't want to do that, then you'll have to wait for this series to be finalized & merged next dev cycle:
https://lists.nongnu.org/archive/html/qemu-devel/2024-08/msg01173.html
It is getting close to ready, so might not be too long, but I can't guarantee a timeframe.
Thanks Daniel! It looks like returning the buffers is the way to move forward for now.
Regards,
Dorjoy
Re: [PATCH v3 4/5] machine/nitro-enclave: Add built-in Nitro Secure Module device
In reply to Dorjoy ChowdhuryQuoted 23 lines · Dorjoy Chowdhury
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 <[email protected]>--- 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(-)
[...]
Quoted 14 lines
hw/i386/nitro_enclave.c +10
-0@@ -87,10 +106,46 @@ static void nitro_enclave_machine_state_init(MachineState *machine)nitro_enclave_devices_init (ne_state); } +static void nitro_enclave_machine_reset (MachineState *machine, +ShutdownCause reason) +{ +NitroEnclaveMachineClass *ne_class = +NITRO_ENCLAVE_MACHINE_GET_CLASS (machine); +NitroEnclaveMachineState *ne_state =NITRO_ENCLAVE_MACHINE (machine); + + ne_class->parent_reset (machine, reason); + +memset (ne_state->vnsm->pcrs,0 ,sizeof (ne_state->vnsm->pcrs));
hw/i386/nitro_enclave.c static void nitro_enclave_machine_state_init(MachineState *machine) · line 128
Quoted 10 lines
hw/i386/nitro_enclave.c +10
-0+ +/* 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_sha384, +SHA384_BYTE_LEN ); +/* PCR2 */ + ne_state->vnsm->extend_pcr (ne_state->vnsm,2 , ne_state->app_sha384, +SHA384_BYTE_LEN );
What about PCR3 and PCR4? Both are just sha384 values of input strings[1]. Can you make these input strings NSM device as well as machine properties as well?
[1] https://docs.aws.amazon.com/enclaves/latest/user/set-up-attestation.html
Alex
Amazon Web Services Development Center Germany GmbH Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B
Sitz: Berlin
Ust-ID: DE 365 538 597
PATCH v3 5/5 docs/nitro-enclave: Documentation for nitro-enclave machine type
In reply to Dorjoy Chowdhury--- docs/system/i386/nitro-enclave.rst | 80 ++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 docs/system/i386/nitro-enclave.rst
docs/system/i386/nitro-enclave.rst new file +80 -0
@@ -0,0 +1,80 @@ +'nitro-enclave' virtual machine (``nitro-enclave``) +=================================================== + +``nitro-enclave`` is a machine type which emulates an ``AWS nitro enclave`` +virtual machine. `AWS nitro enclaves`_ is an `Amazon EC2`_ feature that allows +creating isolated execution environments, called enclaves, from Amazon EC2 +instances which are used for processing highly sensitive data. Enclaves have +no persistent storage and no external networking. The enclave VMs are based +on Firecracker microvm with a vhost-vsock device for communication with the +parent EC2 instance that spawned it and a Nitro Secure Module (NSM) device +for cryptographic attestation. The parent instance VM always has CID 3 while +the enclave VM gets a dynamic CID. Enclaves use an EIF (`Enclave Image Format`_) +file which contains the necessary kernel, cmdline and ramdisk(s) to boot. + +In QEMU, ``nitro-enclave`` is a machine type based on ``microvm`` similar to how +``AWS nitro enclaves`` are based on ``Firecracker`` microvm. This is useful for +local testing of EIF files using QEMU instead of running real AWS Nitro Enclaves +which can be difficult for debugging due to its roots in security. The vsock +device emulation is done using vhost-user-vsock which means another process that +can do the userspace emulation, like `vhost-device-vsock`_ from rust-vmm crate, +must be run alongside nitro-enclave for the vsock communication to work. + +.. _AWS nitro enlaves: https://docs.aws.amazon.com/enclaves/latest/user/nitro-enclave.html +.. _Amazon EC2: https://aws.amazon.com/ec2/ +.. _Enclave Image Format: https://github.com/aws/aws-nitro-enclaves-image-format +.. _vhost-device-vsock: https://github.com/rust-vmm/vhost-device/tree/main/vhost-device-vsock + +Using the nitro-enclave machine type +------------------------------ + +Machine-specific options +~~~~~~~~~~~~~~~~~~~~~~~~ + +It supports the following machine-specific options: + +- nitro-enclave.vsock=string (required) (Id of the chardev from '-chardev' option that vhost-user-vsock device will use) +- nitro-enclave.id=string (optional) (Set enclave identifier) + + +Running a nitro-enclave VM +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +First, run vhost-device-vsock (or a similar tool that supports vhost-user-vsock) + + $ vhost-device-vsock \ + --vm guest-cid=4,uds-path=/tmp/vm4.vsock,socket=/tmp/vhost4.socket \ + --vm guest-cid=3,uds-path=/tmp/vm3.vsock,socket=/tmp/vhost3.socket + +Then, run the parent VM that has the necessary vsock communication support. + + $ qemu-system-x86_64 -machine q35,memory-backend=mem0 -enable-kvm -m 8G \ + -nic user,model=virtio -drive file=test_vm.qcow2,media=disk,if=virtio \ + --display sdl -object memory-backend-memfd,id=mem0,size=8G \ + -chardev socket,id=char0,reconnect=0,path=/tmp/vhost3.socket \ + -device vhost-user-vsock-pci,chardev=char0 + +Inside this VM the necessary applications should be run so that the nitro-enclave +VM applications' vsock communication works. For example, the nitro-enclave VM's +init process connects to CID 3 and sends a single byte hello heartbeat (0xB7) to +let the parent VM know that it booted expecting a heartbeat (0xB7) response. + +Now run the nitro-enclave VM using the following command where ``hello.eif`` is +an EIF file you would use to spawn a real AWS nitro enclave virtual machine: + + $ qemu-system-x86_64 -M nitro-enclave,vsock=c,id=hello-world \ + -kernel hello-world.eif -nographic -m 4G --enable-kvm -cpu host \ + -chardev socket,id=c,path=/tmp/vhost4.socket + +In this example, the nitro-enclave VM has CID 4. + + +Limitations +----------- + +AWS nitro enclave emulation support in QEMU requires users to run vhost-device-vsock +or similar tool for vhost-user-vsock support and another VM with CID 3 with necessary +vsock communication support. Requirement of running another VM and necessary applications +inside it can be lifted if some proxying support is added to vhost-device-vsock to +forward all packets to the host machine, in which case, users can run the necessary +applications in the host machine instead of the parent VM with CID 3.
Signature · 1 line
2.39.2
Thanks for the reviews. I have now posted a v4 at:
https://mail.gnu.org/archive/html/qemu-devel/2024-08/msg02675.html
Regards,
Dorjoy