xref: /openbmc/qemu/docs/system/i386/nitro-enclave.rst (revision c94bee4cd6693c1c65ba43bb8970cf909dec378b)
1*05bad41bSDorjoy Chowdhury'nitro-enclave' virtual machine (``nitro-enclave``)
2*05bad41bSDorjoy Chowdhury===================================================
3*05bad41bSDorjoy Chowdhury
4*05bad41bSDorjoy Chowdhury``nitro-enclave`` is a machine type which emulates an *AWS nitro enclave*
5*05bad41bSDorjoy Chowdhuryvirtual machine. `AWS nitro enclaves`_ is an Amazon EC2 feature that allows
6*05bad41bSDorjoy Chowdhurycreating isolated execution environments, called enclaves, from Amazon EC2
7*05bad41bSDorjoy Chowdhuryinstances which are used for processing highly sensitive data. Enclaves have
8*05bad41bSDorjoy Chowdhuryno persistent storage and no external networking. The enclave VMs are based
9*05bad41bSDorjoy Chowdhuryon Firecracker microvm with a vhost-vsock device for communication with the
10*05bad41bSDorjoy Chowdhuryparent EC2 instance that spawned it and a Nitro Secure Module (NSM) device
11*05bad41bSDorjoy Chowdhuryfor cryptographic attestation. The parent instance VM always has CID 3 while
12*05bad41bSDorjoy Chowdhurythe enclave VM gets a dynamic CID. Enclaves use an EIF (`Enclave Image Format`_)
13*05bad41bSDorjoy Chowdhuryfile which contains the necessary kernel, cmdline and ramdisk(s) to boot.
14*05bad41bSDorjoy Chowdhury
15*05bad41bSDorjoy ChowdhuryIn QEMU, ``nitro-enclave`` is a machine type based on ``microvm`` similar to how
16*05bad41bSDorjoy ChowdhuryAWS nitro enclaves are based on `Firecracker`_ microvm. This is useful for
17*05bad41bSDorjoy Chowdhurylocal testing of EIF files using QEMU instead of running real AWS Nitro Enclaves
18*05bad41bSDorjoy Chowdhurywhich can be difficult for debugging due to its roots in security. The vsock
19*05bad41bSDorjoy Chowdhurydevice emulation is done using vhost-user-vsock which means another process that
20*05bad41bSDorjoy Chowdhurycan do the userspace emulation, like `vhost-device-vsock`_ from rust-vmm crate,
21*05bad41bSDorjoy Chowdhurymust be run alongside nitro-enclave for the vsock communication to work.
22*05bad41bSDorjoy Chowdhury
23*05bad41bSDorjoy Chowdhury``libcbor`` and ``gnutls`` are required dependencies for nitro-enclave machine
24*05bad41bSDorjoy Chowdhurysupport to be added when building QEMU from source.
25*05bad41bSDorjoy Chowdhury
26*05bad41bSDorjoy Chowdhury.. _AWS nitro enclaves: https://docs.aws.amazon.com/enclaves/latest/user/nitro-enclave.html
27*05bad41bSDorjoy Chowdhury.. _Enclave Image Format: https://github.com/aws/aws-nitro-enclaves-image-format
28*05bad41bSDorjoy Chowdhury.. _vhost-device-vsock: https://github.com/rust-vmm/vhost-device/tree/main/vhost-device-vsock
29*05bad41bSDorjoy Chowdhury.. _Firecracker: https://firecracker-microvm.github.io
30*05bad41bSDorjoy Chowdhury
31*05bad41bSDorjoy ChowdhuryUsing the nitro-enclave machine type
32*05bad41bSDorjoy Chowdhury------------------------------------
33*05bad41bSDorjoy Chowdhury
34*05bad41bSDorjoy ChowdhuryMachine-specific options
35*05bad41bSDorjoy Chowdhury~~~~~~~~~~~~~~~~~~~~~~~~
36*05bad41bSDorjoy Chowdhury
37*05bad41bSDorjoy ChowdhuryIt supports the following machine-specific options:
38*05bad41bSDorjoy Chowdhury
39*05bad41bSDorjoy Chowdhury- nitro-enclave.vsock=string (required) (Id of the chardev from '-chardev' option that vhost-user-vsock device will use)
40*05bad41bSDorjoy Chowdhury- nitro-enclave.id=string (optional) (Set enclave identifier)
41*05bad41bSDorjoy Chowdhury- nitro-enclave.parent-role=string (optional) (Set parent instance IAM role ARN)
42*05bad41bSDorjoy Chowdhury- nitro-enclave.parent-id=string (optional) (Set parent instance identifier)
43*05bad41bSDorjoy Chowdhury
44*05bad41bSDorjoy Chowdhury
45*05bad41bSDorjoy ChowdhuryRunning a nitro-enclave VM
46*05bad41bSDorjoy Chowdhury~~~~~~~~~~~~~~~~~~~~~~~~~~
47*05bad41bSDorjoy Chowdhury
48*05bad41bSDorjoy ChowdhuryFirst, run `vhost-device-vsock`__ (or a similar tool that supports vhost-user-vsock).
49*05bad41bSDorjoy ChowdhuryThe forward-cid option below with value 1 forwards all connections from the enclave
50*05bad41bSDorjoy ChowdhuryVM to the host machine and the forward-listen (port numbers separated by '+') is used
51*05bad41bSDorjoy Chowdhuryfor forwarding connections from the host machine to the enclave VM.
52*05bad41bSDorjoy Chowdhury
53*05bad41bSDorjoy Chowdhury__ https://github.com/rust-vmm/vhost-device/tree/main/vhost-device-vsock#using-the-vsock-backend
54*05bad41bSDorjoy Chowdhury
55*05bad41bSDorjoy Chowdhury  $ vhost-device-vsock \
56*05bad41bSDorjoy Chowdhury     --vm guest-cid=4,forward-cid=1,forward-listen=9001+9002,socket=/tmp/vhost4.socket
57*05bad41bSDorjoy Chowdhury
58*05bad41bSDorjoy ChowdhuryNow run the necessary applications on the host machine so that the nitro-enclave VM
59*05bad41bSDorjoy Chowdhuryapplications' vsock communication works. For example, the nitro-enclave VM's init
60*05bad41bSDorjoy Chowdhuryprocess connects to CID 3 and sends a single byte hello heartbeat (0xB7) to let the
61*05bad41bSDorjoy Chowdhuryparent VM know that it booted expecting a heartbeat (0xB7) response. So you must run
62*05bad41bSDorjoy Chowdhurya AF_VSOCK server on the host machine that listens on port 9000 and sends the heartbeat
63*05bad41bSDorjoy Chowdhuryafter it receives the heartbeat for enclave VM to boot successfully. You should run all
64*05bad41bSDorjoy Chowdhurythe applications on the host machine that would typically be running in the parent EC2
65*05bad41bSDorjoy ChowdhuryVM for successful communication with the enclave VM.
66*05bad41bSDorjoy Chowdhury
67*05bad41bSDorjoy ChowdhuryThen run the nitro-enclave VM using the following command where ``hello.eif`` is
68*05bad41bSDorjoy Chowdhuryan EIF file you would use to spawn a real AWS nitro enclave virtual machine:
69*05bad41bSDorjoy Chowdhury
70*05bad41bSDorjoy Chowdhury  $ qemu-system-x86_64 -M nitro-enclave,vsock=c,id=hello-world \
71*05bad41bSDorjoy Chowdhury     -kernel hello-world.eif -nographic -m 4G --enable-kvm -cpu host \
72*05bad41bSDorjoy Chowdhury     -chardev socket,id=c,path=/tmp/vhost4.socket
73*05bad41bSDorjoy Chowdhury
74*05bad41bSDorjoy ChowdhuryIn this example, the nitro-enclave VM has CID 4. If there are applications that
75*05bad41bSDorjoy Chowdhuryconnect to the enclave VM, run them on the host machine after enclave VM starts.
76*05bad41bSDorjoy ChowdhuryYou need to modify the applications to connect to CID 1 (instead of the enclave
77*05bad41bSDorjoy ChowdhuryVM's CID) and use the forward-listen (e.g., 9001+9002) option of vhost-device-vsock
78*05bad41bSDorjoy Chowdhuryto forward the ports they connect to.
79