| 1 |
package gpu |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/sha256" |
| 5 |
"fmt" |
| 6 |
|
| 7 |
"encoding/base64" |
| 8 |
|
| 9 |
"cos.googlesource.com/cos/tools.git/src/cmd/cos_gpu_installer/deviceinfo" |
| 10 |
"github.com/NVIDIA/go-nvml/pkg/nvml" |
| 11 |
"github.com/confidentsecurity/go-nvtrust/pkg/gonvtrust/gpu" |
| 12 |
|
| 13 |
attestationpb "github.com/GoogleCloudPlatform/confidential-space/server/proto/gen/attestation" |
| 14 |
"github.com/google/go-tpm-tools/proto/attest" |
| 15 |
) |
| 16 |
|
| 17 |
type attestationType int |
| 18 |
|
| 19 |
const ( |
| 20 |
// UNSUPPORTED indicates unsupported GPU attestation type like PPCIE. |
| 21 |
UNSUPPORTED attestationType = iota |
| 22 |
// SPT indicates Nvidia's single GPU passthrough attestation |
| 23 |
SPT |
| 24 |
// MPT indicates Nvidia's multi GPU secure passthrough attestation |
| 25 |
MPT |
| 26 |
) |
| 27 |
|
| 28 |
// Stub this func for testing purpose. |
| 29 |
var getGpuTypeInfo = deviceinfo.GetGPUTypeInfo |
| 30 |
|
| 31 |
// Attester defines the interface for GPU attestation. |
| 32 |
type Attester interface { |
| 33 |
Attest(nonce []byte) (any, error) |
| 34 |
EnableReadyState() error |
| 35 |
} |
| 36 |
|
| 37 |
// NvidiaAttester is responsible for collecting GPU attestation. |
| 38 |
type NvidiaAttester struct{} |
| 39 |
|
| 40 |
// NewNvidiaAttester returns a new NvidiaAttester if installGpuDriver is true, otherwise nil. |
| 41 |
func NewNvidiaAttester(installGpuDriver bool) Attester { |
| 42 |
if !installGpuDriver { |
| 43 |
return nil |
| 44 |
} |
| 45 |
return &NvidiaAttester{} |
| 46 |
} |
| 47 |
|
| 48 |
// Attest returns a GPU attestation. |
| 49 |
func (a *NvidiaAttester) Attest(nonce []byte) (any, error) { |
| 50 |
if a == nil { |
| 51 |
return nil, fmt.Errorf("nil Nvidia attester") |
| 52 |
} |
| 53 |
gpuAttestation, err := a.collectAttestationEvidence(&gpu.DefaultNVMLHandler{}, nonce) |
| 54 |
if err != nil { |
| 55 |
return nil, err |
| 56 |
} |
| 57 |
return gpuAttestation, nil |
| 58 |
} |
| 59 |
|
| 60 |
// EnableReadyState checks the Confidential Computing mode and transitions the GPU to a READY state if CC is enabled. |
| 61 |
func (a *NvidiaAttester) EnableReadyState() error { |
| 62 |
if a == nil { |
| 63 |
return fmt.Errorf("nil Nvidia attester") |
| 64 |
} |
| 65 |
|
| 66 |
ccModeCmd := NvidiaSmiOutputFunc("conf-compute", "-f") |
| 67 |
devToolsCmd := NvidiaSmiOutputFunc("conf-compute", "-d") |
| 68 |
|
| 69 |
ccEnabled, err := QueryCCMode(ccModeCmd, devToolsCmd) |
| 70 |
if err != nil { |
| 71 |
return fmt.Errorf("failed to check confidential compute mode status: %v", err) |
| 72 |
} |
| 73 |
|
| 74 |
// Explicitly need to set the GPU state to READY for GPUs with confidential compute mode ON. |
| 75 |
if ccEnabled == attest.GPUDeviceCCMode_ON { |
| 76 |
setGPUStateCmd := NvidiaSmiOutputFunc("conf-compute", "-srs", "1") |
| 77 |
if err := setGPUStateToReady(setGPUStateCmd); err != nil { |
| 78 |
return fmt.Errorf("failed to set the GPU state to ready: %v", err) |
| 79 |
} |
| 80 |
} |
| 81 |
return nil |
| 82 |
} |
| 83 |
|
| 84 |
// collectAttestationEvidence assumes CC GPU devices are in place w/ driver support |
| 85 |
// and will try to collect raw attestation evidence and convert it to known data models. |
| 86 |
func (a *NvidiaAttester) collectAttestationEvidence(handler gpu.NvmlHandler, nonce []byte) (*attestationpb.NvidiaAttestationReport, error) { |
| 87 |
gpuAdmin, err := gpu.NewNvmlGPUAdmin(handler) |
| 88 |
if err != nil { |
| 89 |
return nil, fmt.Errorf("failed to create GPU admin: %v", err) |
| 90 |
} |
| 91 |
defer gpuAdmin.Shutdown() |
| 92 |
|
| 93 |
nvNonce := sha256.Sum256(nonce) |
| 94 |
deviceInfos, err := gpuAdmin.CollectEvidence(nvNonce[:]) |
| 95 |
if err != nil { |
| 96 |
return nil, fmt.Errorf("failed to collect GPU evidence: %v", err) |
| 97 |
} |
| 98 |
|
| 99 |
var gpuInfos []*attestationpb.GpuInfo |
| 100 |
for i, deviceInfo := range deviceInfos { |
| 101 |
device, ret := handler.DeviceGetHandleByIndex(i) |
| 102 |
if ret != nvml.SUCCESS { |
| 103 |
return nil, fmt.Errorf("failed to get GPU device: %v", nvml.ErrorString(ret)) |
| 104 |
} |
| 105 |
uuid, ret := device.GetUUID() |
| 106 |
if ret != nvml.SUCCESS { |
| 107 |
return nil, fmt.Errorf("failed to get GPU device UUID: %v", nvml.ErrorString(ret)) |
| 108 |
} |
| 109 |
|
| 110 |
vbiosVersion, ret := device.GetVbiosVersion() |
| 111 |
if ret != nvml.SUCCESS { |
| 112 |
return nil, fmt.Errorf("failed to get GPU VBIOS version: %v", nvml.ErrorString(ret)) |
| 113 |
} |
| 114 |
|
| 115 |
driverVersion, ret := handler.SystemGetDriverVersion() |
| 116 |
if ret != nvml.SUCCESS { |
| 117 |
return nil, fmt.Errorf("failed to get GPU driver version: %v", nvml.ErrorString(ret)) |
| 118 |
} |
| 119 |
|
| 120 |
base64PEM, err := deviceInfo.Certificate().EncodeBase64() |
| 121 |
if err != nil { |
| 122 |
return nil, fmt.Errorf("failed to encode GPU certificate chain: %v", err) |
| 123 |
} |
| 124 |
|
| 125 |
attestationCertChainData, err := base64.StdEncoding.DecodeString(base64PEM) |
| 126 |
if err != nil { |
| 127 |
return nil, fmt.Errorf("failed to decode GPU certificate chain: %v", err) |
| 128 |
} |
| 129 |
|
| 130 |
gpuInfo := &attestationpb.GpuInfo{ |
| 131 |
Uuid: uuid, |
| 132 |
DriverVersion: driverVersion, |
| 133 |
VbiosVersion: vbiosVersion, |
| 134 |
GpuArchitectureType: convertGPUArchToPB(deviceInfo.Arch()), |
| 135 |
AttestationReport: deviceInfo.AttestationReport(), |
| 136 |
AttestationCertificateChain: attestationCertChainData, |
| 137 |
} |
| 138 |
gpuInfos = append(gpuInfos, gpuInfo) |
| 139 |
} |
| 140 |
|
| 141 |
switch determineAttestationType(gpuInfos) { |
| 142 |
case SPT: |
| 143 |
return &attestationpb.NvidiaAttestationReport{ |
| 144 |
CcFeature: &attestationpb.NvidiaAttestationReport_Spt{ |
| 145 |
Spt: &attestationpb.NvidiaAttestationReport_SinglePassthroughAttestation{ |
| 146 |
GpuQuote: gpuInfos[0], |
| 147 |
}, |
| 148 |
}, |
| 149 |
Nonce: nvNonce[:], |
| 150 |
}, nil |
| 151 |
case MPT: |
| 152 |
return &attestationpb.NvidiaAttestationReport{ |
| 153 |
CcFeature: &attestationpb.NvidiaAttestationReport_Mpt{ |
| 154 |
Mpt: &attestationpb.NvidiaAttestationReport_MultiGpuSecurePassthroughAttestation{ |
| 155 |
GpuQuotes: gpuInfos, |
| 156 |
}, |
| 157 |
}, |
| 158 |
Nonce: nvNonce[:], |
| 159 |
}, nil |
| 160 |
default: |
| 161 |
return nil, fmt.Errorf("unsupported GPU attestation") |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
// determineAttesationType auto-detects the GPU attestation type. |
| 166 |
// The current implementations "guess" the attestation type. |
| 167 |
// Further improvement should be made to parse GPU attesation report to get the actual attestation type. |
| 168 |
func determineAttestationType(gpuInfos []*attestationpb.GpuInfo) attestationType { |
| 169 |
gpuType, _ := getGpuTypeInfo() |
| 170 |
if gpuType != deviceinfo.H100 && gpuType != deviceinfo.B200 { |
| 171 |
return UNSUPPORTED |
| 172 |
} |
| 173 |
if gpuType == deviceinfo.B200 && len(gpuInfos) > 1 { |
| 174 |
return MPT |
| 175 |
} |
| 176 |
return SPT |
| 177 |
} |
| 178 |
|
| 179 |
func convertGPUArchToPB(arch string) attestationpb.GpuArchitectureType { |
| 180 |
switch arch { |
| 181 |
case "HOPPER": |
| 182 |
return attestationpb.GpuArchitectureType_GPU_ARCHITECTURE_TYPE_HOPPER |
| 183 |
case "BLACKWELL": |
| 184 |
return attestationpb.GpuArchitectureType_GPU_ARCHITECTURE_TYPE_BLACKWELL |
| 185 |
default: |
| 186 |
return attestationpb.GpuArchitectureType_GPU_ARCHITECTURE_TYPE_UNSPECIFIED |
| 187 |
} |
| 188 |
} |