File nitro-enclave-alive.c of Package nitro-enclave-alive

/*
 * SPDX-License-Identifier: MIT
 *
 * Nitro Enclave "I'm Alive" tool.
 *
 * Copyright (c) 2024 Alexander Graf
 *
 * Nitro Enclaves need to tell the parent when they have booted up
 * successfully. This tool performs that notification. It exits with
 * an error code if anything went wrong.
 */

#include <stdio.h>
#include <sys/socket.h>
#include <linux/vm_sockets.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdint.h>

#define ALIVE_PORT 9000
#define PARENT_CID 3
#define BUFFER_SIZE 1

int main() {
    int sock_fd;
    struct sockaddr_vm addr;
    uint8_t buffer[BUFFER_SIZE] = { 0xb7 };
    uint8_t response[BUFFER_SIZE];

    // Create a VSOCK socket
    sock_fd = socket(AF_VSOCK, SOCK_STREAM, 0);
    if (sock_fd == -1) {
        perror("socket");
        return 1;
    }

    // Configure the VSOCK address
    addr.svm_family = AF_VSOCK;
    addr.svm_cid = PARENT_CID;
    addr.svm_port = ALIVE_PORT;

    // Connect to the VSOCK server
    if (connect(sock_fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
        perror("connect");
        close(sock_fd);
        return 1;
    }

    // Send the byte 0xb7
    if (send(sock_fd, buffer, BUFFER_SIZE, 0) == -1) {
        perror("send");
        close(sock_fd);
        return 1;
    }

    // Wait for the response
    if (recv(sock_fd, response, BUFFER_SIZE, 0) == -1) {
        perror("recv");
        close(sock_fd);
        return 1;
    }

    // Check if the response is 0xb7
    if (response[0] == 0xb7) {
        printf("Notified parent that the Enclave is up\n");
    } else {
        printf("Received unexpected response: %x\n", response[0]);
        close(sock_fd);
        return 1;
    }

    close(sock_fd);
    return 0;
}
openSUSE Build Service is sponsored by