Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sev snp enhancements v1 #4

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cpuid/cpuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@ var CpuSigs = map[string]int{
"EPYC-Milan": cpuSig(25, 1, 1),
"EPYC-Milan-v1": cpuSig(25, 1, 1),
"EPYC-Milan-v2": cpuSig(25, 1, 1),
"EPYC-Genoa": cpuSig(25, 17, 0),
"EPYC-Genoa-v1": cpuSig(25, 17, 0),
}
3 changes: 2 additions & 1 deletion e2e/upstream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/edgelesssys/sev-snp-measure-go/guest"
"github.com/edgelesssys/sev-snp-measure-go/ovmf"
"github.com/edgelesssys/sev-snp-measure-go/vmmtypes"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -54,7 +55,7 @@ func TestCompatibility(t *testing.T) {
require.NoError(err, "calculating OVMF hash: %s", err)

// Documentation for guestFeatures value: https://github.com/virtee/sev-snp-measure/pull/32/files#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5R126.
digest, err := guest.LaunchDigestFromOVMF(ovmfObj, 0x21, entry.vcpus, ovmfHash)
digest, err := guest.LaunchDigestFromOVMF(ovmfObj, 0x21, entry.vcpus, ovmfHash, vmmtypes.EC2, "")
require.NoError(err, "calculating launch digest: %s", err)

assert.True(bytes.Equal(digest, entry.measurement), "expected hash %x, got %x", entry.measurement, digest)
Expand Down
23 changes: 14 additions & 9 deletions guest/guest.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,21 @@ import (
"github.com/edgelesssys/sev-snp-measure-go/ovmf"
"github.com/edgelesssys/sev-snp-measure-go/vmmtypes"
"github.com/edgelesssys/sev-snp-measure-go/vmsa"
"github.com/edgelesssys/sev-snp-measure-go/cpuid"
)

// LaunchDigestFromMetadataWrapper calculates a launch digest from a MetadataWrapper object.
func LaunchDigestFromMetadataWrapper(wrapper ovmf.MetadataWrapper, guestFeatures uint64, vcpuCount int) ([]byte, error) {
return launchDigest(wrapper.MetadataItems, wrapper.ResetEIP, guestFeatures, vcpuCount, wrapper.OVMFHash)
func LaunchDigestFromMetadataWrapper(wrapper ovmf.MetadataWrapper, guestFeatures uint64, vcpuCount int, vmmtype vmmtypes.VMMType, vcpu_type string) ([]byte, error) {
return launchDigest(wrapper.MetadataItems, wrapper.ResetEIP, guestFeatures, vcpuCount, wrapper.OVMFHash, vmmtype, vcpu_type)
}

// LaunchDigestFromOVMF calculates a launch digest from an OVMF object and an ovmfHash.
func LaunchDigestFromOVMF(ovmfObj ovmf.OVMF, guestFeatures uint64, vcpuCount int, ovmfHash []byte) ([]byte, error) {
func LaunchDigestFromOVMF(ovmfObj ovmf.OVMF, guestFeatures uint64, vcpuCount int, ovmfHash []byte, vmmtype vmmtypes.VMMType, vcpu_type string) ([]byte, error) {
resetEIP, err := ovmfObj.SevESResetEIP()
if err != nil {
return nil, fmt.Errorf("getting reset EIP: %w", err)
}
return launchDigest(ovmfObj.MetadataItems(), resetEIP, guestFeatures, vcpuCount, ovmfHash)
return launchDigest(ovmfObj.MetadataItems(), resetEIP, guestFeatures, vcpuCount, ovmfHash, vmmtype, vcpu_type)
}

func OVMFHash(ovmfObj ovmf.OVMF) ([]byte, error) {
Expand All @@ -40,16 +41,20 @@ func OVMFHash(ovmfObj ovmf.OVMF) ([]byte, error) {
}

// launchDigest calculates the launch digest from metadata and ovmfHash for a SNP guest.
func launchDigest(metadata []ovmf.MetadataSection, resetEIP uint32, guestFeatures uint64, vcpuCount int, ovmfHash []byte) ([]byte, error) {
func launchDigest(metadata []ovmf.MetadataSection, resetEIP uint32, guestFeatures uint64, vcpuCount int, ovmfHash []byte, vmmtype vmmtypes.VMMType, vcpu_type string) ([]byte, error) {
guestCtx := gctx.New(ovmfHash)

if err := snpUpdateMetadataPages(guestCtx, metadata, vmmtypes.EC2); err != nil {
if err := snpUpdateMetadataPages(guestCtx, metadata, vmmtype); err != nil {
return nil, fmt.Errorf("updating metadata pages: %w", err)
}

// Add support for flags {vcpus_family, vcpu_sig, vcpu_type} here, if relevant.
// Use cpuid pkg.
vmsaObj, err := vmsa.New(resetEIP, guestFeatures, 0, vmmtypes.EC2)
vcpu_sig, ok := cpuid.CpuSigs[vcpu_type]
if !ok {
fmt.Println("Failed to find VCPU signature for %s", vcpu_type)
vcpu_sig = 0
}

vmsaObj, err := vmsa.New(resetEIP, guestFeatures, uint64(vcpu_sig), vmmtype)
if err != nil {
return nil, fmt.Errorf("creating VMSA: %w", err)
}
Expand Down
5 changes: 3 additions & 2 deletions guest/guest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"testing"

"github.com/edgelesssys/sev-snp-measure-go/ovmf"
"github.com/edgelesssys/sev-snp-measure-go/vmmtypes"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -57,7 +58,7 @@ func TestLaunchDigestFromOVMF(t *testing.T) {
ovmfObj, err := ovmf.New(tc.ovmfPath)
require.NoError(err)

launchDigest, err := LaunchDigestFromOVMF(ovmfObj, 0x1, tc.vcpuCount, hash)
launchDigest, err := LaunchDigestFromOVMF(ovmfObj, 0x1, tc.vcpuCount, hash, vmmtypes.EC2, "")
if tc.wantErr {
assert.Error(err)
} else {
Expand Down Expand Up @@ -101,7 +102,7 @@ func TestLaunchDigestFromMetadataWrapper(t *testing.T) {
err = json.Unmarshal(data, &apiObject)
require.NoError(err)

launchDigest, err := LaunchDigestFromMetadataWrapper(apiObject, 0x1, tc.vcpuCount)
launchDigest, err := LaunchDigestFromMetadataWrapper(apiObject, 0x1, tc.vcpuCount, vmmtypes.EC2, "")
if tc.wantErr {
assert.Error(err)
} else {
Expand Down
2 changes: 1 addition & 1 deletion ovmf/ovmf.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func (m *MetadataWrapper) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"MetadataItems": m.MetadataItems,
"ResetEIP": fmt.Sprintf("0x%x", m.ResetEIP),
"OVMFHash": fmt.Sprintf("0x%s", hex.EncodeToString(m.OVMFHash)),
"OVMFHash": fmt.Sprintf("%s", hex.EncodeToString(m.OVMFHash)),
})
}

Expand Down