Skip to contentSkip to conversation

AWS Nitro Enclave emulation support

PATCH In review Dorjoy Chowdhury 3 participants 17 messages Oct 8, 2024 – Oct 30, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v8 cover AWS Nitro Enclave emulation support

This is v8 submission for AWS Nitro Enclave emulation in QEMU. From the QEMU side the implementation for nitro enclaves is complete. v7 is at:
https://lore.kernel.org/qemu-devel/[email protected]/T/#t

Changes in v8:
- create_default_memdev related changes have been made into a separate commit
- PCR states are now saved during migration
- some changes in virtio-nsm.c for making the implementation similar to real
NSM in AWS which include returning the same error, ignoring unknown properties in nested map etc. I got a chance to test real NSM in AWS and I prepared a script to test various NSM operations and error conditions which I am maintaining a git repo
here: https://github.com/dorjoy03/nsm-api . The README has some details for testing.

Changes in v7:
- rebased with master as the first 3 crypto patches got in master now
- had to change some QCRYPTO_HASH_ALG refs to QCRYPTO_HASH_ALGO (with an 'O')
as that got changed in master
- had to re-do the libvirt-ci update patch as I got a conflict from master
- renamed handle_* functions in virtio-nsm.c to be not camel case

Changes in v6:
- updated MAINTAINERS in the commit that introduced eif.c, eif.h files
- used iov_to_buf and iov_from_buf in virtio-nsm.c handle_input
- used g_new0 for iovec_list instead of g_malloc
- updated documentation as now the vsock backend work[6] has been merged in
rust-vmm's vhost-device-vsock

Changes in v5:
- bunch of use of glib utilities like g_autofree, g_memdup2, GList etc
- updated libvirt-ci and added libcbor dependency
- files in this patch are built under libcbor and gnutls dependency check now and
libcbor dependency has been moved to root meson.build file
- separated and re-ordered commits as suggested
- user_data and nonce are added as null to attestation when empty and payload_map_size is fixed
- variables in eif.c have been shortened for readability

Changes in v4:
- fixed error_setv assertion failed. I could not reproduce this but I think
this was happening because I did not set Error *err = NULL in x86_load_eif
- qemu_cbor.. helpers moved to a separate file now
- libcbor version requirement reduced from 0.8.0 to 0.7.0
- replaced GChecksum uses with qcrypto apis
- timestamp multiplied by 1000 in virtio-nsm
- user_data and nonce are now included in attestation even when they are empty
- added x509-utils in crypto
- added G_CHECKSUM_SHA384 support in hash-glib.c
- PCR3 and PCR4 can be set from nitro-enclave machine options. I did not add
the options for virtio-nsm device though. I think the PCR states are set by machines so it made sense to add the options for machine only.

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 have already posted a PR[6] in 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. Update:
this has been merged now.

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[7].

For local testing you need to generate a hello.eif image by first building nitro-cli locally[8]. Then you can use nitro-cli to build a hello.eif image[9]. 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://github.com/rust-vmm/vhost-device/pull/706
[7] https://lists.oasis-open.org/archives/virtio-comment/202310/msg00387.html
[8] https://github.com/aws/aws-nitro-enclaves-cli/blob/main/docs/ubuntu_20.04_how_to_install_nitro_cli_from_github_sources.md
[9] https://github.com/aws/aws-nitro-enclaves-cli/blob/main/examples/x86_64/hello/README.md

Dorjoy Chowdhury (6):
tests/lcitool: Update libvirt-ci and add libcbor dependency device/virtio-nsm: Support for Nitro Secure Module device hw/core: Add Enclave Image Format (EIF) related helpers core/machine: Make create_default_memdev machine class property machine/nitro-enclave: New machine type for AWS Nitro Enclaves docs/nitro-enclave: Documentation for nitro-enclave machine type

 .gitlab-ci.d/cirrus/macos-14.vars             |    2 +-
 .gitlab-ci.d/cirrus/macos-15.vars             |    2 +-
 MAINTAINERS                                   |   20 +
 backends/hostmem-memfd.c                      |    2 -
 configs/devices/i386-softmmu/default.mak      |    1 +
 docs/system/i386/nitro-enclave.rst            |   78 +
 hw/core/eif.c                                 |  719 +++++++
 hw/core/eif.h                                 |   22 +
 hw/core/machine.c                             |   71 +-
 hw/core/meson.build                           |    3 +
 hw/i386/Kconfig                               |    6 +
 hw/i386/meson.build                           |    3 +
 hw/i386/microvm.c                             |    6 +-
 hw/i386/nitro_enclave.c                       |  354 ++++
 hw/virtio/Kconfig                             |    5 +
 hw/virtio/cbor-helpers.c                      |  321 +++
 hw/virtio/meson.build                         |    6 +
 hw/virtio/virtio-nsm-pci.c                    |   73 +
 hw/virtio/virtio-nsm.c                        | 1732 +++++++++++++++++
 include/hw/boards.h                           |    2 +
 include/hw/i386/microvm.h                     |    2 +
 include/hw/i386/nitro_enclave.h               |   62 +
 include/hw/virtio/cbor-helpers.h              |   45 +
 include/hw/virtio/virtio-nsm.h                |   49 +
 include/sysemu/hostmem.h                      |    2 +
 meson.build                                   |    2 +
 .../ci/setup/ubuntu/ubuntu-2204-aarch64.yaml  |    1 +
 .../ci/setup/ubuntu/ubuntu-2204-s390x.yaml    |    1 +
 tests/docker/dockerfiles/alpine.docker        |    1 +
 .../dockerfiles/debian-amd64-cross.docker     |    1 +
 .../dockerfiles/debian-arm64-cross.docker     |    1 +
 .../dockerfiles/debian-armhf-cross.docker     |    1 +
 .../dockerfiles/debian-i686-cross.docker      |    1 +
 .../dockerfiles/debian-mips64el-cross.docker  |    1 +
 .../dockerfiles/debian-mipsel-cross.docker    |    1 +
 .../dockerfiles/debian-ppc64el-cross.docker   |    1 +
 .../dockerfiles/debian-s390x-cross.docker     |    1 +
 tests/docker/dockerfiles/debian.docker        |    1 +
 tests/docker/dockerfiles/fedora.docker        |    1 +
 tests/docker/dockerfiles/opensuse-leap.docker |    1 +
 tests/docker/dockerfiles/ubuntu2204.docker    |    1 +
 tests/lcitool/projects/qemu.yml               |    1 +
 42 files changed, 3568 insertions(+), 38 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/cbor-helpers.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/cbor-helpers.h
 create mode 100644 include/hw/virtio/virtio-nsm.h
Signature · 1 line
2.39.5
#1 lore raw

PATCH v8 1/6 tests/lcitool: Update libvirt-ci and add libcbor dependency

libcbor dependecy is necessary for adding virtio-nsm and nitro-enclave machine support in the following commits. libvirt-ci has already been updated with the dependency upstream and this commit updates libvirt-ci submodule in QEMU to latest upstream. Also the libcbor dependency has been added to tests/lcitool/projects/qemu.yml.

---
 .gitlab-ci.d/cirrus/macos-14.vars                     | 2 +-
 .gitlab-ci.d/cirrus/macos-15.vars                     | 2 +-
 scripts/ci/setup/ubuntu/ubuntu-2204-aarch64.yaml      | 1 +
 scripts/ci/setup/ubuntu/ubuntu-2204-s390x.yaml        | 1 +
 tests/docker/dockerfiles/alpine.docker                | 1 +
 tests/docker/dockerfiles/debian-amd64-cross.docker    | 1 +
 tests/docker/dockerfiles/debian-arm64-cross.docker    | 1 +
 tests/docker/dockerfiles/debian-armhf-cross.docker    | 1 +
 tests/docker/dockerfiles/debian-i686-cross.docker     | 1 +
 tests/docker/dockerfiles/debian-mips64el-cross.docker | 1 +
 tests/docker/dockerfiles/debian-mipsel-cross.docker   | 1 +
 tests/docker/dockerfiles/debian-ppc64el-cross.docker  | 1 +
 tests/docker/dockerfiles/debian-s390x-cross.docker    | 1 +
 tests/docker/dockerfiles/debian.docker                | 1 +
 tests/docker/dockerfiles/fedora.docker                | 1 +
 tests/docker/dockerfiles/opensuse-leap.docker         | 1 +
 tests/docker/dockerfiles/ubuntu2204.docker            | 1 +
 tests/lcitool/projects/qemu.yml                       | 1 +
 18 files changed, 18 insertions(+), 2 deletions(-)
.gitlab-ci.d/cirrus/macos-14.vars +1 -1
index 24cfec3b89..2bbab50ca0 100644
@@ -11,6 +11,6 @@ MAKE='/opt/homebrew/bin/gmake'
 NINJA='/opt/homebrew/bin/ninja'
 PACKAGING_COMMAND='brew'
 PIP3='/opt/homebrew/bin/pip3'
-PKGS='bash bc bison bzip2 capstone ccache cmocka ctags curl dbus diffutils dtc flex gcovr gettext git glib gnu-sed gnutls gtk+3 gtk-vnc jemalloc jpeg-turbo json-c libepoxy libffi libgcrypt libiscsi libnfs libpng libslirp libssh libtasn1 libusb llvm lzo make meson mtools ncurses nettle ninja pixman pkg-config python3 rpm2cpio sdl2 sdl2_image snappy socat sparse spice-protocol swtpm tesseract usbredir vde vte3 xorriso zlib zstd'
+PKGS='bash bc bison bzip2 capstone ccache cmocka ctags curl dbus diffutils dtc flex gcovr gettext git glib gnu-sed gnutls gtk+3 gtk-vnc jemalloc jpeg-turbo json-c libcbor libepoxy libffi libgcrypt libiscsi libnfs libpng libslirp libssh libtasn1 libusb llvm lzo make meson mtools ncurses nettle ninja pixman pkg-config python3 rpm2cpio sdl2 sdl2_image snappy socat sparse spice-protocol swtpm tesseract usbredir vde vte3 xorriso zlib zstd'
 PYPI_PKGS='PyYAML numpy pillow sphinx sphinx-rtd-theme tomli'
 PYTHON='/opt/homebrew/bin/python3'
.gitlab-ci.d/cirrus/macos-15.vars +1 -1
index 23b2c1d22f..f76ad3b1ed 100644
@@ -11,6 +11,6 @@ MAKE='/opt/homebrew/bin/gmake'
 NINJA='/opt/homebrew/bin/ninja'
 PACKAGING_COMMAND='brew'
 PIP3='/opt/homebrew/bin/pip3'
-PKGS='bash bc bison bzip2 capstone ccache cmocka ctags curl dbus diffutils dtc flex gcovr gettext git glib gnu-sed gnutls gtk+3 gtk-vnc jemalloc jpeg-turbo json-c libepoxy libffi libgcrypt libiscsi libnfs libpng libslirp libssh libtasn1 libusb llvm lzo make meson mtools ncurses nettle ninja pixman pkg-config python3 rpm2cpio sdl2 sdl2_image snappy socat sparse spice-protocol swtpm tesseract usbredir vde vte3 xorriso zlib zstd'
+PKGS='bash bc bison bzip2 capstone ccache cmocka ctags curl dbus diffutils dtc flex gcovr gettext git glib gnu-sed gnutls gtk+3 gtk-vnc jemalloc jpeg-turbo json-c libcbor libepoxy libffi libgcrypt libiscsi libnfs libpng libslirp libssh libtasn1 libusb llvm lzo make meson mtools ncurses nettle ninja pixman pkg-config python3 rpm2cpio sdl2 sdl2_image snappy socat sparse spice-protocol swtpm tesseract usbredir vde vte3 xorriso zlib zstd'
 PYPI_PKGS='PyYAML numpy pillow sphinx sphinx-rtd-theme tomli'
 PYTHON='/opt/homebrew/bin/python3'
scripts/ci/setup/ubuntu/ubuntu-2204-aarch64.yaml +1 -0
index 71a0f0c433..4fb3c7555f 100644
@@ -35,6 +35,7 @@ packages:
   - libcacard-dev
   - libcap-ng-dev
   - libcapstone-dev
+  - libcbor-dev
   - libcmocka-dev
   - libcurl4-gnutls-dev
   - libdaxctl-dev
scripts/ci/setup/ubuntu/ubuntu-2204-s390x.yaml +1 -0
index d8de967b18..d79399caf3 100644
@@ -35,6 +35,7 @@ packages:
   - libcacard-dev
   - libcap-ng-dev
   - libcapstone-dev
+  - libcbor-dev
   - libcmocka-dev
   - libcurl4-gnutls-dev
   - libdaxctl-dev
tests/docker/dockerfiles/alpine.docker +1 -0
index 54b9721997..a0136181d3 100644
@@ -45,6 +45,7 @@ RUN apk update && \
         libaio-dev \
         libbpf-dev \
         libcap-ng-dev \
+        libcbor-dev \
         libdrm-dev \
         libepoxy-dev \
         libffi-dev \
tests/docker/dockerfiles/debian-amd64-cross.docker +1 -0
index 136c3a79a1..5c5a753aca 100644
@@ -92,6 +92,7 @@ RUN export DEBIAN_FRONTEND=noninteractive && \
                       libcacard-dev:amd64 \
                       libcap-ng-dev:amd64 \
                       libcapstone-dev:amd64 \
+                      libcbor-dev:amd64 \
                       libcmocka-dev:amd64 \
                       libcurl4-gnutls-dev:amd64 \
                       libdaxctl-dev:amd64 \
tests/docker/dockerfiles/debian-arm64-cross.docker +1 -0
index 233f6ee1de..7724b5f7fb 100644
@@ -92,6 +92,7 @@ RUN export DEBIAN_FRONTEND=noninteractive && \
                       libcacard-dev:arm64 \
                       libcap-ng-dev:arm64 \
                       libcapstone-dev:arm64 \
+                      libcbor-dev:arm64 \
                       libcmocka-dev:arm64 \
                       libcurl4-gnutls-dev:arm64 \
                       libdaxctl-dev:arm64 \
tests/docker/dockerfiles/debian-armhf-cross.docker +1 -0
index f26385e0b9..d64c2a2cbd 100644
@@ -92,6 +92,7 @@ RUN export DEBIAN_FRONTEND=noninteractive && \
                       libcacard-dev:armhf \
                       libcap-ng-dev:armhf \
                       libcapstone-dev:armhf \
+                      libcbor-dev:armhf \
                       libcmocka-dev:armhf \
                       libcurl4-gnutls-dev:armhf \
                       libdaxctl-dev:armhf \
tests/docker/dockerfiles/debian-i686-cross.docker +1 -0
index 2328ee1732..f4501866f9 100644
@@ -92,6 +92,7 @@ RUN export DEBIAN_FRONTEND=noninteractive && \
                       libcacard-dev:i386 \
                       libcap-ng-dev:i386 \
                       libcapstone-dev:i386 \
+                      libcbor-dev:i386 \
                       libcmocka-dev:i386 \
                       libcurl4-gnutls-dev:i386 \
                       libdaxctl-dev:i386 \
tests/docker/dockerfiles/debian-mips64el-cross.docker +1 -0
index bfa96cb507..126b22c311 100644
@@ -91,6 +91,7 @@ RUN export DEBIAN_FRONTEND=noninteractive && \
                       libcacard-dev:mips64el \
                       libcap-ng-dev:mips64el \
                       libcapstone-dev:mips64el \
+                      libcbor-dev:mips64el \
                       libcmocka-dev:mips64el \
                       libcurl4-gnutls-dev:mips64el \
                       libdaxctl-dev:mips64el \
tests/docker/dockerfiles/debian-mipsel-cross.docker +1 -0
index 4ac314e22e..0fcb88a174 100644
@@ -91,6 +91,7 @@ RUN export DEBIAN_FRONTEND=noninteractive && \
                       libcacard-dev:mipsel \
                       libcap-ng-dev:mipsel \
                       libcapstone-dev:mipsel \
+                      libcbor-dev:mipsel \
                       libcmocka-dev:mipsel \
                       libcurl4-gnutls-dev:mipsel \
                       libdaxctl-dev:mipsel \
tests/docker/dockerfiles/debian-ppc64el-cross.docker +1 -0
index 8c1dcec9cf..393d0300c6 100644
@@ -92,6 +92,7 @@ RUN export DEBIAN_FRONTEND=noninteractive && \
                       libcacard-dev:ppc64el \
                       libcap-ng-dev:ppc64el \
                       libcapstone-dev:ppc64el \
+                      libcbor-dev:ppc64el \
                       libcmocka-dev:ppc64el \
                       libcurl4-gnutls-dev:ppc64el \
                       libdaxctl-dev:ppc64el \
tests/docker/dockerfiles/debian-s390x-cross.docker +1 -0
index 72668e0315..cbe590080d 100644
@@ -92,6 +92,7 @@ RUN export DEBIAN_FRONTEND=noninteractive && \
                       libcacard-dev:s390x \
                       libcap-ng-dev:s390x \
                       libcapstone-dev:s390x \
+                      libcbor-dev:s390x \
                       libcmocka-dev:s390x \
                       libcurl4-gnutls-dev:s390x \
                       libdaxctl-dev:s390x \
tests/docker/dockerfiles/debian.docker +1 -0
index 42bd0067d1..631fe6b671 100644
@@ -41,6 +41,7 @@ RUN export DEBIAN_FRONTEND=noninteractive && \
                       libcacard-dev \
                       libcap-ng-dev \
                       libcapstone-dev \
+                      libcbor-dev \
                       libcmocka-dev \
                       libcurl4-gnutls-dev \
                       libdaxctl-dev \
tests/docker/dockerfiles/fedora.docker +1 -0
index ecdefaff1a..a01c004cfa 100644
@@ -61,6 +61,7 @@ exec "$@"\n' > /usr/bin/nosync && \
                libbpf-devel \
                libcacard-devel \
                libcap-ng-devel \
+               libcbor-devel \
                libcmocka-devel \
                libcurl-devel \
                libdrm-devel \
tests/docker/dockerfiles/opensuse-leap.docker +1 -0
index 0dab4e4873..751919c1ec 100644
@@ -46,6 +46,7 @@ RUN zypper update -y && \
            libbz2-devel \
            libcacard-devel \
            libcap-ng-devel \
+           libcbor-devel \
            libcmocka-devel \
            libcurl-devel \
            libdrm-devel \
tests/docker/dockerfiles/ubuntu2204.docker +1 -0
index 3a7de6a318..ecd5c4fd08 100644
@@ -41,6 +41,7 @@ RUN export DEBIAN_FRONTEND=noninteractive && \
                       libcacard-dev \
                       libcap-ng-dev \
                       libcapstone-dev \
+                      libcbor-dev \
                       libcmocka-dev \
                       libcurl4-gnutls-dev \
                       libdaxctl-dev \
tests/lcitool/projects/qemu.yml +1 -0
index 252e871f80..35878951b6 100644
@@ -42,6 +42,7 @@ packages:
  - libc-static
  - libcacard
  - libcap-ng
+ - libcbor
  - libcurl
  - libdrm
  - libepoxy
Signature · 1 line
2.39.5
#2 lore raw

PATCH v8 2/6 device/virtio-nsm: Support for Nitro Secure Module device

In reply to Dorjoy Chowdhury

Nitro Secure Module (NSM)[1] device is used in AWS Nitro Enclaves[2] for stripped down TPM functionality like cryptographic attestation. The requests to and responses from NSM device are CBOR[3] 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[4] library has been used for the CBOR encoding and decoding functionalities.

[1] https://lists.oasis-open.org/archives/virtio-comment/202310/msg00387.html
[2] https://docs.aws.amazon.com/enclaves/latest/user/nitro-enclave.html
[3] http://cbor.io/
[4] https://libcbor.readthedocs.io/en/latest/

---
 MAINTAINERS                      |   10 +
 hw/virtio/Kconfig                |    5 +
 hw/virtio/cbor-helpers.c         |  321 ++++++
 hw/virtio/meson.build            |    6 +
 hw/virtio/virtio-nsm-pci.c       |   73 ++
 hw/virtio/virtio-nsm.c           | 1732 ++++++++++++++++++++++++++++++
 include/hw/virtio/cbor-helpers.h |   45 +
 include/hw/virtio/virtio-nsm.h   |   49 +
 meson.build                      |    2 +
 9 files changed, 2243 insertions(+)
 create mode 100644 hw/virtio/cbor-helpers.c
 create mode 100644 hw/virtio/virtio-nsm-pci.c
 create mode 100644 hw/virtio/virtio-nsm.c
 create mode 100644 include/hw/virtio/cbor-helpers.h
 create mode 100644 include/hw/virtio/virtio-nsm.h
MAINTAINERS +10 -0
index d7a11fe601..ca1cbc7118 100644
@@ -2278,6 +2278,16 @@ 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/cbor-helpers.c
+F: hw/virtio/virtio-nsm.c
+F: hw/virtio/virtio-nsm-pci.c
+F: include/hw/virtio/cbor-helpers.h
+F: include/hw/virtio/virtio-nsm.h
+
 vhost-user-stubs
 M: Alex Bennée <[email protected]>
 S: Maintained
hw/virtio/Kconfig +5 -0
index 17595ff350..0cedf48f98 100644
@@ -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/cbor-helpers.c new file +321 -0

321 lines not on this page · raw message

hw/virtio/meson.build +6 -0
index 621fc65454..1fe7cb4d72 100644
@@ -54,6 +54,9 @@ 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'))
+if libcbor.found()
+  specific_virtio_ss.add(when: 'CONFIG_VIRTIO_NSM', if_true: [files('virtio-nsm.c', 'cbor-helpers.c'), libcbor])
+endif
 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,9 @@ 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'))
+if libcbor.found()
+  virtio_pci_ss.add(when: 'CONFIG_VIRTIO_NSM', if_true: [files('virtio-nsm-pci.c', 'cbor-helpers.c'), libcbor])
+endif
 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 +1732 -0

1,732 lines not on this page · raw message

include/hw/virtio/cbor-helpers.h new file +45 -0

45 lines not on this page · raw message

include/hw/virtio/virtio-nsm.h new file +49 -0

49 lines not on this page · raw message

meson.build +2 -0
index 33954b3eba..c578a6aede 100644
@@ -1683,6 +1683,8 @@ if (have_system or have_tools) and (virgl.found() or opengl.found())
 endif
 have_vhost_user_gpu = have_vhost_user_gpu and virgl.found() and opengl.found() and gbm.found()
 
+libcbor = dependency('libcbor', version: '>=0.7.0', required: false)
+
 gnutls = not_found
 gnutls_crypto = not_found
 if get_option('gnutls').enabled() or (get_option('gnutls').auto() and have_system)
Signature · 1 line
2.39.5
#3 lore raw

PATCH v8 3/6 hw/core: Add Enclave Image Format (EIF) related helpers

In reply to Dorjoy Chowdhury

An EIF (Enclave Image Format)[1] file is used to boot an AWS nitro enclave[2] virtual machine. The EIF file contains the necessary kernel, cmdline, ramdisk(s) sections to boot.

Some helper functions have been introduced for extracting the necessary sections from an EIF file and then writing them to temporary files as well as computing SHA384 hashes from the section data. These will be used in the following commit to add support for nitro-enclave machine type in QEMU.

The files added in this commit are not compiled yet but will be added to the hw/core/meson.build file in the following commit where CONFIG_NITRO_ENCLAVE will be introduced.

[1] https://github.com/aws/aws-nitro-enclaves-image-format
[2] https://docs.aws.amazon.com/enclaves/latest/user/nitro-enclave.html

---
 MAINTAINERS   |   7 +
 hw/core/eif.c | 719 ++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/core/eif.h |  22 ++
 3 files changed, 748 insertions(+)
 create mode 100644 hw/core/eif.c
 create mode 100644 hw/core/eif.h
MAINTAINERS +7 -0
index ca1cbc7118..54e985bc19 100644
@@ -1819,6 +1819,13 @@ 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
+
 Machine core
 M: Eduardo Habkost <[email protected]>
 M: Marcel Apfelbaum <[email protected]>
hw/core/eif.c new file +719 -0

719 lines not on this page · raw message

hw/core/eif.h new file +22 -0
new file mode 100644
index 0000000000..fed3cb5514
@@ -0,0 +1,22 @@
+/*
+ * 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, uint8_t *image_sha384,
+                   uint8_t *bootstrap_sha384, uint8_t *app_sha384,
+                   uint8_t *fingerprint_sha384, bool *signature_found,
+                   Error **errp);
+
+#endif
+
Signature · 1 line
2.39.5
#4 lore raw

PATCH v8 4/6 core/machine: Make create_default_memdev machine class property

In reply to Dorjoy Chowdhury

This is in preparation for the next commit where the nitro-enclave machine type will need to instead use a memfd backend for the built-in vhost-user-vsock device to work.

---
 backends/hostmem-memfd.c |  2 --
 hw/core/machine.c        | 71 +++++++++++++++++++++-------------------
 include/hw/boards.h      |  2 ++
 include/sysemu/hostmem.h |  2 ++
 4 files changed, 42 insertions(+), 35 deletions(-)
backends/hostmem-memfd.c +0 -2
index 6a3c89a12b..9f890a813e 100644
@@ -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

include/hw/boards.h +2 -0
index 5966069baa..91f2edd392 100644
@@ -314,6 +314,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/sysemu/hostmem.h +2 -0
index de47ae59e4..67f45abe39 100644
@@ -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.5
#5 lore raw

PATCH v8 5/6 machine/nitro-enclave: New machine type for AWS Nitro Enclaves

In reply to 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 the 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. This commit adds support for 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.

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. So 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[4] from rust-vmm, with necessary vsock communication support in another guest VM with CID 3. 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. The following mandatory nitro-enclave machine option has been added related to the vhost-user-vsock device.
- 'vsock': The chardev id from the '-chardev' option for the
vhost-user-vsock device.

AWS Nitro Enclaves have built-in Nitro Secure Module (NSM) device which has been added using the virtio-nsm device added in a previous commit. 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. The following optional nitro-enclave machine options have been added related to the NSM device.
- 'id': Enclave identifier, reflected in the module-id of the NSM
device. If not provided, a default id will be set.
- 'parent-role': Parent instance IAM role ARN, reflected in PCR3
of the NSM device.
- 'parent-id': Parent instance identifier, reflected in PCR4 of the
NSM device.

[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
[4] https://github.com/rust-vmm/vhost-device/tree/main/vhost-device-vsock

---
 MAINTAINERS                              |   2 +
 configs/devices/i386-softmmu/default.mak |   1 +
 hw/core/meson.build                      |   3 +
 hw/i386/Kconfig                          |   6 +
 hw/i386/meson.build                      |   3 +
 hw/i386/microvm.c                        |   6 +-
 hw/i386/nitro_enclave.c                  | 354 +++++++++++++++++++++++
 include/hw/i386/microvm.h                |   2 +
 include/hw/i386/nitro_enclave.h          |  62 ++++
 9 files changed, 438 insertions(+), 1 deletion(-)
 create mode 100644 hw/i386/nitro_enclave.c
 create mode 100644 include/hw/i386/nitro_enclave.h
MAINTAINERS +2 -0
index 54e985bc19..f78a7cca06 100644
@@ -1825,6 +1825,8 @@ 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]>
configs/devices/i386-softmmu/default.mak +1 -0
index 448e3e3b1b..4faf2f0315 100644
@@ -29,3 +29,4 @@
 # CONFIG_I440FX=n
 # CONFIG_Q35=n
 # CONFIG_MICROVM=n
+# CONFIG_NITRO_ENCLAVE=n
hw/core/meson.build +3 -0
index a3d9bab9f4..5437a94490 100644
@@ -24,6 +24,9 @@ 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'))
+if libcbor.found() and gnutls.found()
+  system_ss.add(when: 'CONFIG_NITRO_ENCLAVE', if_true: [files('eif.c'), zlib, libcbor, gnutls])
+endif
 
 system_ss.add(files(
   'cpu-sysemu.c',
hw/i386/Kconfig +6 -0
index f4a33b6c08..63271bf915 100644
@@ -129,6 +129,12 @@ config MICROVM
     select USB_XHCI_SYSBUS
     select I8254
 
+config NITRO_ENCLAVE
+    default y
+    depends on MICROVM
+    select VHOST_USER_VSOCK
+    select VIRTIO_NSM
+
 config X86_IOMMU
     bool
     depends on PC
hw/i386/meson.build +3 -0
index 03aad10df7..1ddd7a83be 100644
@@ -15,6 +15,9 @@ 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'))
+if libcbor.found() and gnutls.found()
+  i386_ss.add(when: 'CONFIG_NITRO_ENCLAVE', if_true: files('nitro_enclave.c'))
+endif
 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
index 693099f225..86637afa0f 100644
@@ -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 +354 -0

354 lines not on this page · raw message

include/hw/i386/microvm.h +2 -0
index fad97a891d..b9ac34a3ef 100644
@@ -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 +62 -0

62 lines not on this page · raw message

Signature · 1 line
2.39.5
#6 lore raw

PATCH v8 6/6 docs/nitro-enclave: Documentation for nitro-enclave machine type

In reply to Dorjoy Chowdhury
---
 MAINTAINERS                        |  1 +
 docs/system/i386/nitro-enclave.rst | 78 ++++++++++++++++++++++++++++++
 2 files changed, 79 insertions(+)
 create mode 100644 docs/system/i386/nitro-enclave.rst
MAINTAINERS +1 -0
index f78a7cca06..a857e056e1 100644
@@ -1827,6 +1827,7 @@ F: hw/core/eif.c
 F: hw/core/eif.h
 F: hw/i386/nitro_enclave.c
 F: include/hw/i386/nitro_enclave.h
+F: docs/system/i386/nitro-enclave.rst
 
 Machine core
 M: Eduardo Habkost <[email protected]>
docs/system/i386/nitro-enclave.rst new file +78 -0

78 lines not on this page · raw message

Signature · 1 line
2.39.5
Reviewed-by on the v8 series
#7 lore raw
In reply to Dorjoy Chowdhury
Quoted 105 lines · Dorjoy Chowdhury

This is v8 submission for AWS Nitro Enclave emulation in QEMU. From the QEMU side the implementation for nitro enclaves is complete. v7 is at:
https://lore.kernel.org/qemu-devel/[email protected]/T/#t

Changes in v8:
- create_default_memdev related changes have been made into a separate commit
- PCR states are now saved during migration
- some changes in virtio-nsm.c for making the implementation similar to real
NSM in AWS which include returning the same error, ignoring unknown properties in nested map etc. I got a chance to test real NSM in AWS and I prepared a script to test various NSM operations and error conditions which I am maintaining a git repo
here: https://github.com/dorjoy03/nsm-api . The README has some details for testing.

Changes in v7:
- rebased with master as the first 3 crypto patches got in master now
- had to change some QCRYPTO_HASH_ALG refs to QCRYPTO_HASH_ALGO (with an 'O')
as that got changed in master
- had to re-do the libvirt-ci update patch as I got a conflict from master
- renamed handle_* functions in virtio-nsm.c to be not camel case

Changes in v6:
- updated MAINTAINERS in the commit that introduced eif.c, eif.h files
- used iov_to_buf and iov_from_buf in virtio-nsm.c handle_input
- used g_new0 for iovec_list instead of g_malloc
- updated documentation as now the vsock backend work[6] has been merged in
rust-vmm's vhost-device-vsock

Changes in v5:
- bunch of use of glib utilities like g_autofree, g_memdup2, GList etc
- updated libvirt-ci and added libcbor dependency
- files in this patch are built under libcbor and gnutls dependency check now and
libcbor dependency has been moved to root meson.build file
- separated and re-ordered commits as suggested
- user_data and nonce are added as null to attestation when empty and payload_map_size is fixed
- variables in eif.c have been shortened for readability

Changes in v4:
- fixed error_setv assertion failed. I could not reproduce this but I think
this was happening because I did not set Error *err = NULL in x86_load_eif
- qemu_cbor.. helpers moved to a separate file now
- libcbor version requirement reduced from 0.8.0 to 0.7.0
- replaced GChecksum uses with qcrypto apis
- timestamp multiplied by 1000 in virtio-nsm
- user_data and nonce are now included in attestation even when they are empty
- added x509-utils in crypto
- added G_CHECKSUM_SHA384 support in hash-glib.c
- PCR3 and PCR4 can be set from nitro-enclave machine options. I did not add
the options for virtio-nsm device though. I think the PCR states are set by machines so it made sense to add the options for machine only.

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 have already posted a PR[6] in 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. Update:
this has been merged now.

Quoted 7 lines

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[7].

For local testing you need to generate a hello.eif image by first building nitro-cli locally[8]. Then you can use nitro-cli to build a hello.eif image[9]. More details about testing can be found in the docs/system/i386/nitro-enclave.rst file.

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

#8 lore raw

Ping

This patch series has been reviewed by Alex. I am not sure if it needs more review. If not, maybe this can be picked up for merging. Thanks!

Regards,
Dorjoy

#9 lore raw
Quoted 6 lines · Dorjoy Chowdhury

Ping

This patch series has been reviewed by Alex. I am not sure if it needs more review. If not, maybe this can be picked up for merging. Thanks!

Gentle ping.

This patch series has been reviewed by Alex and there hasn't been any more reviews. it would be great if this could be picked up for merging. Thanks!

Regards,
Dorjoy

#10 lore raw
Quoted 8 lines · Dorjoy Chowdhury
Quoted 6 lines · Dorjoy Chowdhury

Ping

This patch series has been reviewed by Alex. I am not sure if it needs more review. If not, maybe this can be picked up for merging. Thanks!

Quoted 5 lines

Gentle ping.

This patch series has been reviewed by Alex and there hasn't been any more reviews. it would be great if this could be picked up for merging. Thanks!

Hi,

sorry about the delay -- the patches failed CI and I didn't have much time to investigate until now.

The issues are basically:

1) some rST syntax errors

2) failures on non-Linux due to lack of VHOST_USER

3) failures on 32-bit due to uint64_t/long mismatch.

While fixing (2) I also moved the dependency on libcbor and gnutls from meson to Kconfig, and added --enable-libcbor to configure. I also split hw/core/eif.c to a separate symbol, just to simplify reproducing the 32-bit failure on the right commit.

And finally, VIRTIO_NSM should default to no (the nitro-enclave machne takes care of selecting it).

No big deal; it's easier done than described. See attached patch for the differences.

Paolo

docs/system/i386/nitro-enclave.rst +9 -9
index 3fb9e068930..73e3edefe5b 100644
@@ -1,8 +1,8 @@
 '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
+``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
@@ -13,7 +13,7 @@ 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
+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
@@ -23,13 +23,13 @@ must be run alongside nitro-enclave for the vsock communication to work.
 ``libcbor`` and ``gnutls`` are required dependencies for nitro-enclave machine
 support to be added when building QEMU from source.
 
-.. _AWS nitro enlaves: https://docs.aws.amazon.com/enclaves/latest/user/nitro-enclave.html
-.. _Amazon EC2: https://aws.amazon.com/ec2/
+.. _AWS nitro enclaves: https://docs.aws.amazon.com/enclaves/latest/user/nitro-enclave.html
 .. _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
+.. _Firecracker: https://firecracker-microvm.github.io
 
 Using the nitro-enclave machine type
-------------------------------
+------------------------------------
 
 Machine-specific options
 ~~~~~~~~~~~~~~~~~~~~~~~~
@@ -45,11 +45,13 @@ It supports the following machine-specific options:
 Running a nitro-enclave VM
 ~~~~~~~~~~~~~~~~~~~~~~~~~~
 
-First, run `vhost-device-vsock`_ (or a similar tool that supports vhost-user-vsock).
+First, run `vhost-device-vsock`__ (or a similar tool that supports vhost-user-vsock).
 The forward-cid option below with value 1 forwards all connections from the enclave
 VM to the host machine and the forward-listen (port numbers separated by '+') is used
 for forwarding connections from the host machine to the enclave VM.
 
+__ https://github.com/rust-vmm/vhost-device/tree/main/vhost-device-vsock#using-the-vsock-backend
+
   $ vhost-device-vsock \
      --vm guest-cid=4,forward-cid=1,forward-listen=9001+9002,socket=/tmp/vhost4.socket
 
@@ -74,5 +76,3 @@ connect to the enclave VM, run them on the host machine after enclave VM starts.
 You need to modify the applications to connect to CID 1 (instead of the enclave
 VM's CID) and use the forward-listen (e.g., 9001+9002) option of vhost-device-vsock
 to forward the ports they connect to.
-
-.. _vhost-device-vsock: https://github.com/rust-vmm/vhost-device/tree/main/vhost-device-vsock#using-the-vsock-backend
docs/system/target-i386.rst +2 -1
index 23e84e3ba76..ab7af1a75d6 100644
@@ -14,8 +14,9 @@ Board-specific documentation
 .. toctree::
    :maxdepth: 1
 
-   i386/microvm
    i386/pc
+   i386/microvm
+   i386/nitro-enclave
 
 Architectural features
 ~~~~~~~~~~~~~~~~~~~~~~
meson.build +8 -1
index 20300fca206..41458e3dba1 100644
@@ -1709,7 +1709,11 @@ if (have_system or have_tools) and (virgl.found() or opengl.found())
 endif
 have_vhost_user_gpu = have_vhost_user_gpu and virgl.found() and opengl.found() and gbm.found()
 
-libcbor = dependency('libcbor', version: '>=0.7.0', required: false)
+libcbor = not_found
+if not get_option('libcbor').auto() or have_system
+  libcbor = dependency('libcbor', version: '>=0.7.0',
+                       required: get_option('libcbor'))
+endif
 
 gnutls = not_found
 gnutls_crypto = not_found
@@ -3119,6 +3123,8 @@ host_kconfig = \
   (spice.found() ? ['CONFIG_SPICE=y'] : []) + \
   (have_ivshmem ? ['CONFIG_IVSHMEM=y'] : []) + \
   (opengl.found() ? ['CONFIG_OPENGL=y'] : []) + \
+  (libcbor.found() ? ['CONFIG_LIBCBOR=y'] : []) + \
+  (gnutls.found() ? ['CONFIG_GNUTLS=y'] : []) + \
   (x11.found() ? ['CONFIG_X11=y'] : []) + \
   (fdt.found() ? ['CONFIG_FDT=y'] : []) + \
   (have_vhost_user ? ['CONFIG_VHOST_USER=y'] : []) + \
@@ -4699,6 +4705,7 @@ summary_info += {'NUMA host support': numa}
 summary_info += {'capstone':          capstone}
 summary_info += {'libpmem support':   libpmem}
 summary_info += {'libdaxctl support': libdaxctl}
+summary_info += {'libcbor support':   libcbor}
 summary_info += {'libudev':           libudev}
 # Dummy dependency, keep .found()
 summary_info += {'FUSE lseek':        fuse_lseek.found()}
hw/core/eif.c +2 -2
index d737d7f23c1..7f3b2edc9a7 100644
@@ -467,7 +467,7 @@ bool read_eif_file(const char *eif_path, const char *machine_initrd,
         uint16_t section_type;
 
         if (fseek(f, eif_header.section_offsets[i], SEEK_SET) != 0) {
-            error_setg_errno(errp, errno, "Failed to offset to %lu in EIF file",
+            error_setg_errno(errp, errno, "Failed to offset to %" PRIu64 " in EIF file",
                              eif_header.section_offsets[i]);
             goto cleanup;
         }
@@ -483,7 +483,7 @@ bool read_eif_file(const char *eif_path, const char *machine_initrd,
 
         if (eif_header.section_sizes[i] != hdr.section_size) {
             error_setg(errp, "EIF section size mismatch between header and "
-                       "section header: header %lu, section header %lu",
+                       "section header: header %" PRIu64 ", section header %" PRIu64,
                        eif_header.section_sizes[i],
                        hdr.section_size);
             goto cleanup;
Kconfig.host +6 -0
index 4ade7899d67..842cbe0d6c5 100644
@@ -5,6 +5,12 @@
 config LINUX
     bool
 
+config LIBCBOR
+    bool
+
+config GNUTLS
+    bool
+
 config OPENGL
     bool
 
hw/core/Kconfig +4 -0
index 24411f59306..d1bdf765ee8 100644
@@ -34,3 +34,7 @@ config REGISTER
 
 config SPLIT_IRQ
     bool
+
+config EIF
+    bool
+    depends on LIBCBOR && GNUTLS
hw/core/meson.build +1 -3
index 5437a944902..9fd0b5aaa5e 100644
@@ -24,9 +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'))
-if libcbor.found() and gnutls.found()
-  system_ss.add(when: 'CONFIG_NITRO_ENCLAVE', if_true: [files('eif.c'), zlib, libcbor, gnutls])
-endif
+system_ss.add(when: 'CONFIG_EIF', if_true: [files('eif.c'), zlib, libcbor, gnutls])
 
 system_ss.add(files(
   'cpu-sysemu.c',
hw/i386/Kconfig +5 -1
index 63271bf9153..32818480d26 100644
@@ -131,7 +131,11 @@ config MICROVM
 
 config NITRO_ENCLAVE
     default y
-    depends on MICROVM
+    depends on I386 && FDT # for MICROVM
+    depends on LIBCBOR && GNUTLS # for EIF and VIRTIO_NSM
+    depends on VHOST_USER # for VHOST_USER_VSOCK
+    select EIF
+    select MICROVM
     select VHOST_USER_VSOCK
     select VIRTIO_NSM
 
hw/i386/meson.build +1 -3
index 1ddd7a83be9..10bdfde27c6 100644
@@ -15,9 +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'))
-if libcbor.found() and gnutls.found()
-  i386_ss.add(when: 'CONFIG_NITRO_ENCLAVE', if_true: files('nitro_enclave.c'))
-endif
+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/virtio/Kconfig +1 -2
index 0cedf48f982..70c77e183d4 100644
@@ -8,8 +8,7 @@ config VIRTIO_RNG
 
 config VIRTIO_NSM
    bool
-   default y
-   depends on VIRTIO
+   depends on LIBCBOR && VIRTIO
 
 config VIRTIO_IOMMU
     bool
hw/virtio/meson.build +2 -6
index 1fe7cb4d72c..a5f9f7999dd 100644
@@ -54,9 +54,7 @@ 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'))
-if libcbor.found()
-  specific_virtio_ss.add(when: 'CONFIG_VIRTIO_NSM', if_true: [files('virtio-nsm.c', 'cbor-helpers.c'), libcbor])
-endif
+specific_virtio_ss.add(when: 'CONFIG_VIRTIO_NSM', if_true: [files('virtio-nsm.c', 'cbor-helpers.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'))
@@ -73,9 +71,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'))
-if libcbor.found()
-  virtio_pci_ss.add(when: 'CONFIG_VIRTIO_NSM', if_true: [files('virtio-nsm-pci.c', 'cbor-helpers.c'), libcbor])
-endif
+virtio_pci_ss.add(when: 'CONFIG_VIRTIO_NSM', if_true: [files('virtio-nsm-pci.c', 'cbor-helpers.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'))
meson_options.txt +2 -0
index 0ee4d7bb86b..24bf0090560 100644
@@ -168,6 +168,8 @@ option('iconv', type : 'feature', value : 'auto',
        description: 'Font glyph conversion support')
 option('curses', type : 'feature', value : 'auto',
        description: 'curses UI')
+option('libcbor', type : 'feature', value : 'auto',
+       description: 'libcbor support')
 option('gnutls', type : 'feature', value : 'auto',
        description: 'GNUTLS cryptography support')
 option('nettle', type : 'feature', value : 'auto',
scripts/meson-buildoptions.sh +3 -0
index 6d08605b771..6f2bb08ecd8 100644
@@ -133,6 +133,7 @@ meson_options_help() {
   printf "%s\n" '  keyring         Linux keyring support'
   printf "%s\n" '  kvm             KVM acceleration support'
   printf "%s\n" '  l2tpv3          l2tpv3 network backend support'
+  printf "%s\n" '  libcbor         libcbor support'
   printf "%s\n" '  libdaxctl       libdaxctl support'
   printf "%s\n" '  libdw           debuginfo support'
   printf "%s\n" '  libiscsi        libiscsi userspace initiator'
@@ -358,6 +359,8 @@ _meson_option_parse() {
     --disable-kvm) printf "%s" -Dkvm=disabled ;;
     --enable-l2tpv3) printf "%s" -Dl2tpv3=enabled ;;
     --disable-l2tpv3) printf "%s" -Dl2tpv3=disabled ;;
+    --enable-libcbor) printf "%s" -Dlibcbor=enabled ;;
+    --disable-libcbor) printf "%s" -Dlibcbor=disabled ;;
     --enable-libdaxctl) printf "%s" -Dlibdaxctl=enabled ;;
     --disable-libdaxctl) printf "%s" -Dlibdaxctl=disabled ;;
     --libdir=*) quote_sh "-Dlibdir=$2" ;;
#11 lore raw

Hi Paolo,

Quoted 35 lines · Paolo Bonzini
Quoted 8 lines · Dorjoy Chowdhury
Quoted 6 lines · Dorjoy Chowdhury

Ping

This patch series has been reviewed by Alex. I am not sure if it needs more review. If not, maybe this can be picked up for merging. Thanks!

Quoted 5 lines

Gentle ping.

This patch series has been reviewed by Alex and there hasn't been any more reviews. it would be great if this could be picked up for merging. Thanks!

Hi,

sorry about the delay -- the patches failed CI and I didn't have much time to investigate until now.

The issues are basically:

1) some rST syntax errors

2) failures on non-Linux due to lack of VHOST_USER

3) failures on 32-bit due to uint64_t/long mismatch.

While fixing (2) I also moved the dependency on libcbor and gnutls from meson to Kconfig, and added --enable-libcbor to configure. I also split hw/core/eif.c to a separate symbol, just to simplify reproducing the 32-bit failure on the right commit.

Quoted 6 lines

And finally, VIRTIO_NSM should default to no (the nitro-enclave machne takes care of selecting it).

No big deal; it's easier done than described. See attached patch for the differences.

Thanks for fixing. The attached patch looks great to me. I just have one suggestion. Now that the CONFIG_* symbols have the dependencies listed explicitly in the Kconfig files, maybe we don't need the explicit dependencies in the meson.build files? For example, the following line in hw/core/meson.build file:
system_ss.add(when: 'CONFIG_EIF', if_true: [files('eif.c'), zlib, libcbor, gnutls])
can be changed to:
system_ss.add(when: 'CONFIG_EIF', if_true: [files('eif.c')])

I am not sure if zlib is a required dependency for QEMU, probably not needed to be listed above as well. I am just guessing.

Same goes for files added in hw/virtio/meson.build.

Thanks!

Regards,
Dorjoy

#12 lore raw
Quoted 10 lines · Dorjoy Chowdhury

Thanks for fixing. The attached patch looks great to me. I just have one suggestion. Now that the CONFIG_* symbols have the dependencies listed explicitly in the Kconfig files, maybe we don't need the explicit dependencies in the meson.build files? For example, the following line in hw/core/meson.build file:
system_ss.add(when: 'CONFIG_EIF', if_true: [files('eif.c'), zlib, libcbor, gnutls])
can be changed to:
system_ss.add(when: 'CONFIG_EIF', if_true: [files('eif.c')])

Quoted 2 lines

I am not sure if zlib is a required dependency for QEMU, probably not needed to be listed above as well. I am just guessing.

No, because the dependencies are not automatically added to all compiler and linker commands. Having them in the "add" call lets the compiler find include files and the linker add the dependency to the executable.

As an aside,

if foo.found()

    system_ss.add(files('x.c'))

endif

can be written

system_ss.add(when: foo, if_true: files('x.c'))

and "when:" supports multiple entries. But in this case it's okay to put it only in "if_true", since the dependency is handled in the Kconfig files and guaranteed to be present.

Paolo

#13 lore raw
Quoted 26 lines · Paolo Bonzini
Quoted 10 lines · Dorjoy Chowdhury

Thanks for fixing. The attached patch looks great to me. I just have one suggestion. Now that the CONFIG_* symbols have the dependencies listed explicitly in the Kconfig files, maybe we don't need the explicit dependencies in the meson.build files? For example, the following line in hw/core/meson.build file:
system_ss.add(when: 'CONFIG_EIF', if_true: [files('eif.c'), zlib, libcbor, gnutls])
can be changed to:
system_ss.add(when: 'CONFIG_EIF', if_true: [files('eif.c')])

Quoted 2 lines

I am not sure if zlib is a required dependency for QEMU, probably not needed to be listed above as well. I am just guessing.

No, because the dependencies are not automatically added to all compiler and linker commands. Having them in the "add" call lets the compiler find include files and the linker add the dependency to the executable.

As an aside,

if foo.found()

    system_ss.add(files('x.c'))

endif

Quoted 8 lines

can be written

system_ss.add(when: foo, if_true: files('x.c'))

and "when:" supports multiple entries. But in this case it's okay to put it only in "if_true", since the dependency is handled in the Kconfig files and guaranteed to be present.

Understood. All good then. Thanks!

Regards,
Dorjoy

#14 lore raw
In reply to Paolo Bonzini

Hey Paolo,

Quoted 35 lines · Paolo Bonzini
Quoted 8 lines · Dorjoy Chowdhury
Quoted 6 lines · Dorjoy Chowdhury

Ping

This patch series has been reviewed by Alex. I am not sure if it needs more review. If not, maybe this can be picked up for merging. Thanks!

Quoted 5 lines

Gentle ping.

This patch series has been reviewed by Alex and there hasn't been any more reviews. it would be great if this could be picked up for merging. Thanks!

Hi,

sorry about the delay -- the patches failed CI and I didn't have much time to investigate until now.

The issues are basically:

1) some rST syntax errors

2) failures on non-Linux due to lack of VHOST_USER

3) failures on 32-bit due to uint64_t/long mismatch.

While fixing (2) I also moved the dependency on libcbor and gnutls from meson to Kconfig, and added --enable-libcbor to configure.  I also split hw/core/eif.c to a separate symbol, just to simplify reproducing the 32-bit failure on the right commit.

Quoted 5 lines

And finally, VIRTIO_NSM should default to no (the nitro-enclave machne takes care of selecting it).

No big deal; it's easier done than described.  See attached patch for the differences.

Thanks a bunch for looking deeply and fixing the issues above :). I suppose the next step is to post v9 with your changes included?

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

#15 lore raw
Quoted 18 lines · Alexander Graf
Quoted 18 lines

Hi,

sorry about the delay -- the patches failed CI and I didn't have much time to investigate until now.

The issues are basically:

1) some rST syntax errors

2) failures on non-Linux due to lack of VHOST_USER

3) failures on 32-bit due to uint64_t/long mismatch.

While fixing (2) I also moved the dependency on libcbor and gnutls from meson to Kconfig, and added --enable-libcbor to configure.  I also split hw/core/eif.c to a separate symbol, just to simplify reproducing the 32-bit failure on the right commit.

Quoted 7 lines
Quoted 5 lines

And finally, VIRTIO_NSM should default to no (the nitro-enclave machne takes care of selecting it).

No big deal; it's easier done than described.  See attached patch for the differences.

Thanks a bunch for looking deeply and fixing the issues above :). I suppose the next step is to post v9 with your changes included?

No, I can see that it wasn't clear but I've queued it. :)

Paolo

#16 lore raw
Quoted 22 lines · Paolo Bonzini
Quoted 21 lines · Alexander Graf
Quoted 21 lines

Hi,

sorry about the delay -- the patches failed CI and I didn't have much time to investigate until now.

The issues are basically:

1) some rST syntax errors

2) failures on non-Linux due to lack of VHOST_USER

3) failures on 32-bit due to uint64_t/long mismatch.

While fixing (2) I also moved the dependency on libcbor and gnutls from meson to Kconfig, and added --enable-libcbor to configure. I also split hw/core/eif.c to a separate symbol, just to simplify reproducing the 32-bit failure on the right commit.

And finally, VIRTIO_NSM should default to no (the nitro-enclave machne takes care of selecting it).

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

No big deal; it's easier done than described. See attached patch for the differences.

Thanks a bunch for looking deeply and fixing the issues above :). I suppose the next step is to post v9 with your changes included?

No, I can see that it wasn't clear but I've queued it. :)

Thank you for fixing the errors and queueing this, Paolo.

Regards,
Dorjoy