This topic describes how to build a Software Guard Extensions (SGX) encrypted computing environment on a g7t, c7t, or r7t instance (vSGX instance) and run sample code to verify SGX features.

Prerequisites

An Elastic Compute Service (ECS) vSGX instance is created.
Note When you create a security-enhanced instance, you must select a dedicated image to use the security features. For more information, see Create security-enhanced instances.

Background information

Intel® SGX provides an encrypted computing environment at the physical level and ensures data security by providing hardware-based protections instead of firmware- or software-based protections. Intel® SGX uses instruction set extensions and an access control mechanism to isolate the runtime environment of SGX programs. This can protect the confidentiality and integrity of essential code and data against malware attacks. Compared with other security technologies, Intel® SGX uses the root of trust that contains only hardware. This can prevent defects caused by security vulnerabilities of software on which the root of trust is based, and improve system security.

The g7t, c7t, and r7t security-enhanced instance families provide encrypted memory based on Intel® SGX and support the SGX technology applicable to virtual machines. You can develop and run SGX programs in vSGX instances.
Important If you use keys (such as SGX sealing keys) that are bound to hardware to encrypt the data of an instance within an Intel SGX enclave, the encrypted data cannot be decrypted after the host of the instance is changed. We recommend that you perform data redundancy and backup at the application layer to ensure application reliability.

Check whether SGX is enabled

Before you build an SGX encrypted computing environment, you can use CPUID to check whether SGX is enabled. This section describes how to check whether SGX is enabled. In this example, an Alibaba Cloud Linux 2 (UEFI) image or an Alibaba Cloud Linux 3 (UEFI) image is used.

  1. Install CPUID.
    yum install -y cpuid
  2. Check whether SGX is enabled.
    cpuid -1 -l 0x7 |grep SGX
    The following figure shows that SGX is enabled. sgx-install
    Note After SGX is enabled, the SGX driver is required to run SGX programs. The dedicated images provided by Alibaba Cloud have a built-in SGX driver. If you do not use a dedicated image, install the SGX driver.
  3. Check whether the SGX driver is installed.
    ls -l /dev/{sgx_enclave,sgx_provision}
    The following figure shows that the SGX driver is installed. sgx-driver

Build an SGX encrypted computing environment

Before you develop SGX programs, you must install the SGX runtime and SDK on a vSGX instance and configure the remote attestation service. We recommend that you use dedicated images provided by Alibaba Cloud for a better user experience. Dedicated images are equipped with the SGX driver and provide TEE SDK that is fully compatible with Intel® SGX SDK. This section describes how to build an SGX encrypted computing environment. In this example, an Alibaba Cloud Linux 2 (UEFI) image or an Alibaba Cloud Linux 3 (UEFI) image is used. If you use Ubuntu images, CentOS images, or other Linux images, install the SGX driver and Platform SoftWare (PSW). For more information, see Intel® SGX Software Installation Guide.

  1. Optional:Install the Alibaba Cloud SGX runtime.
    Note When you create a vSGX instance in the ECS console, the Alibaba Cloud SGX runtime is automatically installed. You can skip this step and install Alibaba Cloud TEE SDK.
    1. Enable the Alibaba Cloud experimental repository for the vSGX instance.
      Note This operation is required only for vSGX instances that run Alibaba Cloud Linux 2 images.
      rpmkeys --import http://mirrors.cloud.aliyuncs.com/epel/RPM-GPG-KEY-EPEL-7 && \
      yum install -y alinux-release-experimentals
    2. Import the YUM software repository for Alibaba Cloud encrypted computing.
      • The public URLs of the repository are in the following format: https://enclave-[Region-ID].oss-[Region-ID].aliyuncs.com/repo/alinux/enclave-expr.repo.
      • The internal URLs of the repository are in the following format: https://enclave-[Region-ID].oss-[Region-ID]-internal.aliyuncs.com/repo/alinux/enclave-expr.repo.
      Replace [Region-ID] in the preceding URLs with the region ID of the vSGX instance. The following example shows the internal URL of a vSGX instance in the China (Hangzhou) region:
      yum install -y yum-utils && \
      yum-config-manager --add-repo \
      https://enclave-cn-hangzhou.oss-cn-hangzhou-internal.aliyuncs.com/repo/alinux/enclave-expr.repo
    3. Install the Alibaba Cloud SGX runtime.
      yum install -y libsgx-ae-le libsgx-ae-pce libsgx-ae-qe3 libsgx-ae-qve \
      libsgx-aesm-ecdsa-plugin libsgx-aesm-launch-plugin libsgx-aesm-pce-plugin \
      libsgx-aesm-quote-ex-plugin libsgx-dcap-default-qpl libsgx-dcap-ql \
      libsgx-dcap-quote-verify libsgx-enclave-common libsgx-launch libsgx-pce-logic \
      libsgx-qe3-logic libsgx-quote-ex libsgx-ra-network libsgx-ra-uefi \
      libsgx-uae-service libsgx-urts sgx-ra-service sgx-aesm-service
      Note SGX Architectural Enclave Service Manager (AESM) is used to manage services such as enclave start, key configuration, and remote attestation. The default installation path of SGX AESM is /opt/intel/sgx-aesm-service.
  2. Install Alibaba Cloud TEE SDK.
    yum install -y sgxsdk
    Alibaba Cloud TEE SDK is fully compatible with Intel® SGX SDK. After Alibaba Cloud TEE SDK is installed, you can refer to Intel® SGX Developer Reference to develop SGX programs.
    Note The default installation path of Intel® SGX SDK in Alibaba Cloud TEE SDK is /opt/alibaba/teesdk/intel/sgxsdk/.
  3. Configure the Alibaba Cloud SGX remote attestation service.
    The Alibaba Cloud SGX remote attestation service is fully compatible with the Intel SGX Elliptic Curve Digital Signature Algorithm (ECDSA)-based remote attestation service and Intel® SGX SDK. Therefore, vSGX instances provided by Alibaba Cloud can gain trust from remote providers and producers by using remote attestation. For more information, see Intel® SGX ECDSA remote attestation service. The Alibaba Cloud SGX remote attestation service provides the following information for SGX SDK:
    • SGX certificates
    • Revocation list: a list of revoked SGX certificates
    • Trusted computing base information: information about the root of trust
    Note Intel Ice Lake supports only remote attestation based on Intel Software Guard Extensions Data Center Attestation Primitives (Intel SGX DCAP), and does not support remote attestation based on Intel Enhanced Privacy ID (EPID). You must adapt applications before you can use the remote attestation feature. For more information about remote attestation, see Strengthen Enclave Trust with Attestation.
    The Alibaba Cloud SGX remote attestation service is deployed on a per-region basis. You can access this service deployed in the region where the vSGX instance is located for optimal stability. After Alibaba Cloud TEE SDK is installed, the default configuration file /etc/sgx_default_qcnl.conf is automatically generated for the remote attestation service. You must use one of the following methods to adapt the file to the Alibaba Cloud SGX remote attestation service in the region in which the vSGX instance is located.
    Note The following table lists the regions where the Alibaba Cloud SGX remote attestation service is supported.
    Region Region ID
    China (Qingdao) cn-qingdao
    China (Beijing) cn-beijing
    China (Zhangjiakou) cn-zhangjiakou
    China (Ulanqab) cn-wulanchabu
    China (Hangzhou) cn-hangzhou
    China (Shanghai) cn-shanghai
    China (Shenzhen) cn-shenzhen
    China (Heyuan) cn-heyuan
    China (Guangzhou) cn-guangzhou
    China (Chengdu) cn-chengdu
    China (Hong Kong) cn-hongkong
    Singapore (Singapore) ap-southeast-1
    • Method 1: Have the /etc/sgx_default_qcnl.conf file configured. This is the recommended method.
      Run the following command to automatically configure the /etc/sgx_default_qcnl.conf file. For more information about the command, see View instance metadata.
      # Query the region of the instance.
      token=$(curl -s -X PUT -H "X-aliyun-ecs-metadata-token-ttl-seconds: 5" "http://100.100.100.200/latest/api/token")
      region_id=$(curl -s -H "X-aliyun-ecs-metadata-token: $token" http://100.100.100.200/latest/meta-data/region-id)
      
      # Specify the URL of the PCCS caching service for the region in which the instance is located. 
      PCCS_URL=https://sgx-dcap-server-vpc.${region_id}.aliyuncs.com/sgx/certification/v3/
      cat > /etc/sgx_default_qcnl.conf << EOF
      # PCCS server address
      PCCS_URL=${PCCS_URL}
      # To accept insecure HTTPS cert, set this option to FALSE
      USE_SECURE_CERT=TRUE
      EOF
    • Method 2: Manually modify the /etc/sgx_default_qcnl.conf file
      • If the vSGX instance is assigned a public IP address, change the configurations in /etc/sgx_default_qcnl.conf to the following content. Replace [Region-ID] with the region ID of the vSGX instance.
        # PCCS server address
        PCCS_URL=https://sgx-dcap-server.[Region-ID].aliyuncs.com/sgx/certification/v3/
        # To accept insecure HTTPS cert, set this option to FALSE
        USE_SECURE_CERT=TRUE
      • If the vSGX instance is in a virtual private cloud (VPC) and has only internal IP addresses, change the configurations in /etc/sgx_default_qcnl.conf to the following content. Replace [Region-ID] with the region ID of the vSGX instance.
        # PCCS server address
        PCCS_URL=https://sgx-dcap-server-vpc.[Region-ID].aliyuncs.com/sgx/certification/v3/
        # To accept insecure HTTPS cert, set this option to FALSE
        USE_SECURE_CERT=TRUE

Example 1 of verifying SGX features: Start an enclave

Alibaba Cloud TEE SDK provides SGX sample code to verify SGX features. By default, the code is stored in the /opt/alibaba/teesdk/intel/sgxsdk/SampleCode directory.

This section describes an example of how to start an enclave to verify whether the installed SGX SDK works normally. If the enclave is started, the SDK works normally. In this example, the sample code file named SampleEnclave is used.

  1. Install a compiler.
    • If the Alibaba Cloud Linux 2 (UEFI) image is used, install devtoolset.
      1. Install devtoolset.
        yum install -y devtoolset-9
      2. Configure the environment variable related to devtoolset.
        source /opt/rh/devtoolset-9/enable
    • If the Alibaba Cloud Linux 3 (UEFI) image is used, install Development Tools.
      yum groupinstall -y "Development Tools"
  2. Set the environment variable related to SGX SDK.
    source /opt/alibaba/teesdk/intel/sgxsdk/environment
  3. Compile the sample code in SampleEnclave.
    1. Go to the SampleEnclave directory.
      cd /opt/alibaba/teesdk/intel/sgxsdk/SampleCode/SampleEnclave
    2. Compile SampleEnclave.
      make
  4. Run the compiled executable file.
    ./app

Example 2 of verifying SGX features: SGX remote attestation

Alibaba Cloud TEE SDK provides SGX sample code to verify SGX features. By default, the code is stored in the /opt/alibaba/teesdk/intel/sgxsdk/SampleCode directory.

This section describes an example of the SGX remote attestation service. The expected result is that a quote is generated and verified (QuoteGenerationSample and QuoteVerificationSample). The example involves the challenged party (SGX programs that run in the vSGX instance) and the challenging party (the party that wants to verify whether the SGX programs are trusted). In this example, the sample code file named QuoteGenerationSample is used by the challenged party to generate a quote, and the sample code file named QuoteVerificationSample is used by the challenging party to verify the quote

  1. Install a compiler.
    • If the Alibaba Cloud Linux 2 (UEFI) image is used, install devtoolset.
      1. Install devtoolset.
        yum install -y devtoolset-9
      2. Configure the environment variable related to devtoolset.
        source /opt/rh/devtoolset-9/enable
    • If the Alibaba Cloud Linux 3 (UEFI) image is used, install Development Tools.
      yum groupinstall -y "Development Tools"
  2. Set the environment variable related to SGX SDK.
    source /opt/alibaba/teesdk/intel/sgxsdk/environment
  3. Install the dependency package of SGX remote attestation.
    yum install -y libsgx-dcap-ql-devel libsgx-dcap-quote-verify-devel
  4. Compile the sample code in QuoteGenerationSample used by the challenged party.
    1. Go to the QuoteGenerationSample directory.
      cd /opt/alibaba/teesdk/intel/sgxsdk/SampleCode/QuoteGenerationSample
    2. Compile QuoteGenerationSample.
      make
  5. Run the compiled executable file to generate Quote.
    ./app
  6. Compile the sample code in QuoteVerificationSample used by the challenging party.
    1. Go to the QuoteVerificationSample directory.
      cd /opt/alibaba/teesdk/intel/sgxsdk/SampleCode/QuoteVerificationSample
    2. Compile QuoteVerificationSample.
      make
  7. Sign the QuoteVerificationSample enclave.
    To release an official version of an enclave, you must provide the signature key to sign the enclave.
    sgx_sign sign -key Enclave/Enclave_private_sample.pem -enclave enclave.so -out enclave.signed.so -config Enclave/Enclave.config.xml
  8. Run the compiled executable file to verify the quote.
    ./app

Known issues

In some cases, memory leaks occur in the SGX driver that comes with Alibaba Cloud Linux 2 in the kernel of the 4.19.91-23.al7.x86_64 version. This issue is fixed in the latest version. We recommend that you upgrade the kernel to the latest version. If you want to continue using this kernel version, we recommend that you run the following commands to install patches to avoid this issue:
yum install -y alinux-release-experimentals && \
yum install -y kernel-hotfix-5577959-23.al7.x86_64