105bad41bSDorjoy Chowdhury'nitro-enclave' virtual machine (``nitro-enclave``) 205bad41bSDorjoy Chowdhury=================================================== 305bad41bSDorjoy Chowdhury 405bad41bSDorjoy Chowdhury``nitro-enclave`` is a machine type which emulates an *AWS nitro enclave* 505bad41bSDorjoy Chowdhuryvirtual machine. `AWS nitro enclaves`_ is an Amazon EC2 feature that allows 605bad41bSDorjoy Chowdhurycreating isolated execution environments, called enclaves, from Amazon EC2 705bad41bSDorjoy Chowdhuryinstances which are used for processing highly sensitive data. Enclaves have 805bad41bSDorjoy Chowdhuryno persistent storage and no external networking. The enclave VMs are based 905bad41bSDorjoy Chowdhuryon Firecracker microvm with a vhost-vsock device for communication with the 1005bad41bSDorjoy Chowdhuryparent EC2 instance that spawned it and a Nitro Secure Module (NSM) device 1105bad41bSDorjoy Chowdhuryfor cryptographic attestation. The parent instance VM always has CID 3 while 1205bad41bSDorjoy Chowdhurythe enclave VM gets a dynamic CID. Enclaves use an EIF (`Enclave Image Format`_) 1305bad41bSDorjoy Chowdhuryfile which contains the necessary kernel, cmdline and ramdisk(s) to boot. 1405bad41bSDorjoy Chowdhury 1505bad41bSDorjoy ChowdhuryIn QEMU, ``nitro-enclave`` is a machine type based on ``microvm`` similar to how 16*d024d0adSAlexander GrafAWS nitro enclaves look like a `Firecracker`_ microvm. This is useful for 1705bad41bSDorjoy Chowdhurylocal testing of EIF files using QEMU instead of running real AWS Nitro Enclaves 1805bad41bSDorjoy Chowdhurywhich can be difficult for debugging due to its roots in security. The vsock 1905bad41bSDorjoy Chowdhurydevice emulation is done using vhost-user-vsock which means another process that 2005bad41bSDorjoy Chowdhurycan do the userspace emulation, like `vhost-device-vsock`_ from rust-vmm crate, 2105bad41bSDorjoy Chowdhurymust be run alongside nitro-enclave for the vsock communication to work. 2205bad41bSDorjoy Chowdhury 2305bad41bSDorjoy Chowdhury``libcbor`` and ``gnutls`` are required dependencies for nitro-enclave machine 2405bad41bSDorjoy Chowdhurysupport to be added when building QEMU from source. 2505bad41bSDorjoy Chowdhury 2605bad41bSDorjoy Chowdhury.. _AWS nitro enclaves: https://docs.aws.amazon.com/enclaves/latest/user/nitro-enclave.html 2705bad41bSDorjoy Chowdhury.. _Enclave Image Format: https://github.com/aws/aws-nitro-enclaves-image-format 2805bad41bSDorjoy Chowdhury.. _vhost-device-vsock: https://github.com/rust-vmm/vhost-device/tree/main/vhost-device-vsock 2905bad41bSDorjoy Chowdhury.. _Firecracker: https://firecracker-microvm.github.io 3005bad41bSDorjoy Chowdhury 3105bad41bSDorjoy ChowdhuryUsing the nitro-enclave machine type 3205bad41bSDorjoy Chowdhury------------------------------------ 3305bad41bSDorjoy Chowdhury 3405bad41bSDorjoy ChowdhuryMachine-specific options 3505bad41bSDorjoy Chowdhury~~~~~~~~~~~~~~~~~~~~~~~~ 3605bad41bSDorjoy Chowdhury 3705bad41bSDorjoy ChowdhuryIt supports the following machine-specific options: 3805bad41bSDorjoy Chowdhury 3905bad41bSDorjoy Chowdhury- nitro-enclave.vsock=string (required) (Id of the chardev from '-chardev' option that vhost-user-vsock device will use) 4005bad41bSDorjoy Chowdhury- nitro-enclave.id=string (optional) (Set enclave identifier) 4105bad41bSDorjoy Chowdhury- nitro-enclave.parent-role=string (optional) (Set parent instance IAM role ARN) 4205bad41bSDorjoy Chowdhury- nitro-enclave.parent-id=string (optional) (Set parent instance identifier) 4305bad41bSDorjoy Chowdhury 4405bad41bSDorjoy Chowdhury 4505bad41bSDorjoy ChowdhuryRunning a nitro-enclave VM 4605bad41bSDorjoy Chowdhury~~~~~~~~~~~~~~~~~~~~~~~~~~ 4705bad41bSDorjoy Chowdhury 4805bad41bSDorjoy ChowdhuryFirst, run `vhost-device-vsock`__ (or a similar tool that supports vhost-user-vsock). 4905bad41bSDorjoy ChowdhuryThe forward-cid option below with value 1 forwards all connections from the enclave 5005bad41bSDorjoy ChowdhuryVM to the host machine and the forward-listen (port numbers separated by '+') is used 5144d9fab1SDorjoy Chowdhuryfor forwarding connections from the host machine to the enclave VM:: 5205bad41bSDorjoy Chowdhury 5305bad41bSDorjoy Chowdhury $ vhost-device-vsock \ 5405bad41bSDorjoy Chowdhury --vm guest-cid=4,forward-cid=1,forward-listen=9001+9002,socket=/tmp/vhost4.socket 5505bad41bSDorjoy Chowdhury 5644d9fab1SDorjoy Chowdhury__ https://github.com/rust-vmm/vhost-device/tree/main/vhost-device-vsock#using-the-vsock-backend 5744d9fab1SDorjoy Chowdhury 5805bad41bSDorjoy ChowdhuryNow run the necessary applications on the host machine so that the nitro-enclave VM 5905bad41bSDorjoy Chowdhuryapplications' vsock communication works. For example, the nitro-enclave VM's init 6005bad41bSDorjoy Chowdhuryprocess connects to CID 3 and sends a single byte hello heartbeat (0xB7) to let the 6105bad41bSDorjoy Chowdhuryparent VM know that it booted expecting a heartbeat (0xB7) response. So you must run 6205bad41bSDorjoy Chowdhurya AF_VSOCK server on the host machine that listens on port 9000 and sends the heartbeat 6305bad41bSDorjoy Chowdhuryafter it receives the heartbeat for enclave VM to boot successfully. You should run all 6405bad41bSDorjoy Chowdhurythe applications on the host machine that would typically be running in the parent EC2 6505bad41bSDorjoy ChowdhuryVM for successful communication with the enclave VM. 6605bad41bSDorjoy Chowdhury 6705bad41bSDorjoy ChowdhuryThen run the nitro-enclave VM using the following command where ``hello.eif`` is 6844d9fab1SDorjoy Chowdhuryan EIF file you would use to spawn a real AWS nitro enclave virtual machine:: 6905bad41bSDorjoy Chowdhury 7005bad41bSDorjoy Chowdhury $ qemu-system-x86_64 -M nitro-enclave,vsock=c,id=hello-world \ 7105bad41bSDorjoy Chowdhury -kernel hello-world.eif -nographic -m 4G --enable-kvm -cpu host \ 7205bad41bSDorjoy Chowdhury -chardev socket,id=c,path=/tmp/vhost4.socket 7305bad41bSDorjoy Chowdhury 7405bad41bSDorjoy ChowdhuryIn this example, the nitro-enclave VM has CID 4. If there are applications that 7505bad41bSDorjoy Chowdhuryconnect to the enclave VM, run them on the host machine after enclave VM starts. 7605bad41bSDorjoy ChowdhuryYou need to modify the applications to connect to CID 1 (instead of the enclave 7705bad41bSDorjoy ChowdhuryVM's CID) and use the forward-listen (e.g., 9001+9002) option of vhost-device-vsock 7805bad41bSDorjoy Chowdhuryto forward the ports they connect to. 79