CCF
Loading...
Searching...
No Matches
snp_ioctl5.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
6
7#include <fcntl.h>
8#include <stdint.h>
9#include <sys/ioctl.h>
10#include <sys/types.h>
11#include <unistd.h>
12
13// Based on the SEV-SNP ABI Spec document at
14// https://www.amd.com/system/files/TechDocs/56860.pdf
15
16/* linux kernel 5.15.* versions of the ioctls that talk to the PSP */
17
19{
20 constexpr auto DEVICE = "/dev/sev";
21
23 {
24 uint8_t req_msg_type;
25 uint8_t rsp_msg_type;
26 uint8_t msg_version;
27 uint16_t request_len;
28 uint64_t request_uaddr;
29 uint16_t response_len;
31 uint32_t error; /* firmware error code on failure (see psp-sev.h) */
32 };
33
34 // Table 99
54
55 // Table 20
57 {
58 uint8_t report_data[snp_attestation_report_data_size];
59 uint32_t vmpl = 0;
60 uint8_t reserved[28];
61 };
62
63 // Table 23
64#pragma pack(push, 1)
66 {
67 uint32_t status;
68 uint32_t report_size;
69 uint8_t reserved[0x20 - 0x8];
71 uint8_t padding[64];
72 // padding to the size of SEV_SNP_REPORT_RSP_BUF_SZ (i.e., 1280 bytes)
73 };
74#pragma pack(pop)
75
76 constexpr char SEV_GUEST_IOC_TYPE = 'S';
79
80 static inline bool is_sev_snp()
81 {
82 return access(DEVICE, W_OK) == 0;
83 }
84
86 {
87 AttestationReq req = {};
88 AttestationResp resp = {};
89
90 public:
92 {
93 if (report_data.data.size() <= snp_attestation_report_data_size)
94 {
95 std::copy(
96 report_data.data.begin(), report_data.data.end(), req.report_data);
97 }
98 else
99 {
100 throw std::logic_error(
101 "User-defined report data is larger than available space");
102 }
103
104 int fd = open(DEVICE, O_RDWR | O_CLOEXEC);
105 if (fd < 0)
106 {
107 throw std::logic_error(fmt::format("Failed to open \"{}\"", DEVICE));
108 }
109
110 // Documented at
111 // https://www.kernel.org/doc/html/latest/virt/coco/sev-guest.html
112 GuestRequest payload = {
114 .rsp_msg_type = MSG_REPORT_RSP,
115 .msg_version = 1,
116 .request_len = sizeof(req),
117 .request_uaddr = reinterpret_cast<uint64_t>(&req),
118 .response_len = sizeof(resp),
119 .response_uaddr = reinterpret_cast<uint64_t>(&resp),
120 .error = 0};
121
122 int rc = ioctl(fd, SEV_SNP_GUEST_MSG_REPORT, &payload);
123 if (rc < 0)
124 {
125 CCF_APP_FAIL("IOCTL call failed: {}", strerror(errno));
126 CCF_APP_FAIL("Payload error: {}", payload.error);
127 throw std::logic_error(
128 "Failed to issue ioctl SEV_SNP_GUEST_MSG_REPORT");
129 }
130 }
131
132 const snp::Attestation& get() const override
133 {
134 return resp.report;
135 }
136
137 std::vector<uint8_t> get_raw() override
138 {
139 auto quote_bytes = reinterpret_cast<uint8_t*>(&resp.report);
140 return {quote_bytes, quote_bytes + resp.report_size};
141 }
142 };
143}
Definition attestation_sev_snp.h:289
Definition snp_ioctl5.h:86
Attestation(const PlatformAttestationReportData &report_data)
Definition snp_ioctl5.h:91
std::vector< uint8_t > get_raw() override
Definition snp_ioctl5.h:137
const snp::Attestation & get() const override
Definition snp_ioctl5.h:132
#define CCF_APP_FAIL
Definition logger.h:400
Definition snp_ioctl5.h:19
constexpr auto DEVICE
Definition snp_ioctl5.h:20
constexpr int SEV_SNP_GUEST_MSG_REPORT
Definition snp_ioctl5.h:77
MsgType
Definition snp_ioctl5.h:36
@ MSG_CPUID_REQ
Definition snp_ioctl5.h:38
@ MSG_TYPE_INVALID
Definition snp_ioctl5.h:37
@ MSG_KEY_RSP
Definition snp_ioctl5.h:41
@ MSG_ABSORB_REQ
Definition snp_ioctl5.h:48
@ MSG_TYPE_MAX
Definition snp_ioctl5.h:52
@ MSG_CPUID_RSP
Definition snp_ioctl5.h:39
@ MSG_EXPORT_REQ
Definition snp_ioctl5.h:44
@ MSG_IMPORT_REQ
Definition snp_ioctl5.h:46
@ MSG_VMRK_RSP
Definition snp_ioctl5.h:51
@ MSG_VMRK_REQ
Definition snp_ioctl5.h:50
@ MSG_REPORT_RSP
Definition snp_ioctl5.h:43
@ MSG_IMPORT_RSP
Definition snp_ioctl5.h:47
@ MSG_REPORT_REQ
Definition snp_ioctl5.h:42
@ MSG_KEY_REQ
Definition snp_ioctl5.h:40
@ MSG_ABSORB_RSP
Definition snp_ioctl5.h:49
@ MSG_EXPORT_RSP
Definition snp_ioctl5.h:45
constexpr char SEV_GUEST_IOC_TYPE
Definition snp_ioctl5.h:76
Definition report_data.h:51
std::vector< uint8_t > data
Definition report_data.h:52
Definition attestation_sev_snp.h:164
Definition snp_ioctl5.h:57
uint8_t report_data[snp_attestation_report_data_size]
Definition snp_ioctl5.h:58
uint8_t reserved[28]
Definition snp_ioctl5.h:60
uint32_t vmpl
Definition snp_ioctl5.h:59
Definition snp_ioctl5.h:66
uint8_t padding[64]
Definition snp_ioctl5.h:71
uint8_t reserved[0x20 - 0x8]
Definition snp_ioctl5.h:69
uint32_t report_size
Definition snp_ioctl5.h:68
uint32_t status
Definition snp_ioctl5.h:67
struct Attestation report
Definition snp_ioctl5.h:70
Definition snp_ioctl5.h:23
uint8_t rsp_msg_type
Definition snp_ioctl5.h:25
uint16_t request_len
Definition snp_ioctl5.h:27
uint64_t request_uaddr
Definition snp_ioctl5.h:28
uint16_t response_len
Definition snp_ioctl5.h:29
uint32_t error
Definition snp_ioctl5.h:31
uint8_t req_msg_type
Definition snp_ioctl5.h:24
uint64_t response_uaddr
Definition snp_ioctl5.h:30
uint8_t msg_version
Definition snp_ioctl5.h:26