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

Align policy hash verification between SNP and TDX #901

Merged
merged 4 commits into from
Oct 2, 2024
Merged
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
13 changes: 0 additions & 13 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,6 @@ jobs:
runner: TDX
self-hosted: true
test_name: [servicemesh, openssl, policy, workloadsecret, volumestatefulset]
exclude:
# We don't have policies on K3s-qemu yet, so there's no point in
# running those tests.
- platform:
name: K3s-QEMU-SNP
runner: SNP
self-hosted: true
test_name: policy
- platform:
name: K3s-QEMU-TDX
runner: TDX
self-hosted: true
test_name: policy
fail-fast: false
name: "${{ matrix.platform.name }} / ${{ matrix.test_name }}"
runs-on: ${{ matrix.platform.runner }}
Expand Down
6 changes: 5 additions & 1 deletion cli/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ func validatorsFromManifest(m *manifest.Manifest, log *slog.Logger, hostData []b
return nil, fmt.Errorf("getting SNP validate options: %w", err)
}
for _, opt := range opts {
validators = append(validators, snp.NewValidator(opt.VerifyOpts, opt.ValidateOpts, []manifest.HexString{manifest.NewHexString(hostData)},
opt.ValidateOpts.HostData = hostData
validators = append(validators, snp.NewValidator(opt.VerifyOpts, opt.ValidateOpts,
logger.NewWithAttrs(logger.NewNamed(log, "validator"), map[string]string{"tee-type": "snp"}),
))
}
Expand All @@ -108,7 +109,10 @@ func validatorsFromManifest(m *manifest.Manifest, log *slog.Logger, hostData []b
if err != nil {
return nil, fmt.Errorf("generating TDX validation options: %w", err)
}
var mrConfigID [48]byte
copy(mrConfigID[:], hostData)
for _, opt := range tdxOpts {
opt.TdQuoteBodyOptions.MrConfigID = mrConfigID[:]
validators = append(validators, tdx.NewValidator(&tdx.StaticValidateOptsGenerator{Opts: opt}, logger.NewWithAttrs(logger.NewNamed(log, "validator"), map[string]string{"tee-type": "tdx"})))
}

Expand Down
8 changes: 1 addition & 7 deletions coordinator/internal/authority/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/edgelesssys/contrast/internal/attestation/snp"
"github.com/edgelesssys/contrast/internal/attestation/tdx"
"github.com/edgelesssys/contrast/internal/logger"
"github.com/edgelesssys/contrast/internal/manifest"
"github.com/edgelesssys/contrast/internal/memstore"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
Expand Down Expand Up @@ -83,13 +82,8 @@ func (c *Credentials) ServerHandshake(rawConn net.Conn) (net.Conn, credentials.A
return nil, nil, fmt.Errorf("generating SNP validation options: %w", err)
}

var allowedHostDataEntries []manifest.HexString
for entry := range state.Manifest.Policies {
allowedHostDataEntries = append(allowedHostDataEntries, entry)
}

for _, opt := range opts {
validator := snp.NewValidatorWithReportSetter(opt.VerifyOpts, opt.ValidateOpts, allowedHostDataEntries,
validator := snp.NewValidatorWithReportSetter(opt.VerifyOpts, opt.ValidateOpts,
logger.NewWithAttrs(logger.NewNamed(c.logger, "validator"), map[string]string{"tee-type": "snp"}),
&authInfo)
validators = append(validators, validator)
Expand Down
4 changes: 3 additions & 1 deletion coordinator/meshapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import (
grpcprometheus "github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus"
"github.com/prometheus/client_golang/prometheus"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/status"
)

type meshAPIServer struct {
Expand Down Expand Up @@ -108,7 +110,7 @@ func (i *meshAPIServer) NewMeshCert(ctx context.Context, _ *meshapi.NewMeshCertR
hostData := manifest.NewHexString(report.HostData())
entry, ok := state.Manifest.Policies[hostData]
if !ok {
return nil, fmt.Errorf("report data %s not found in manifest", hostData)
return nil, status.Errorf(codes.PermissionDenied, "policy hash %s not found in manifest", hostData)
Freax13 marked this conversation as resolved.
Show resolved Hide resolved
}
dnsNames := entry.SANs

Expand Down
21 changes: 13 additions & 8 deletions e2e/policy/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,20 @@ func getFailures(ctx context.Context, t *testing.T, ct *contrasttest.ContrastTes
// parse the logs
metrics, err := (&expfmt.TextParser{}).TextToMetricFamilies(strings.NewReader(metricsString))
require.NoError(err)
failures := -1
for k, v := range metrics {
if k == "contrast_meshapi_attestation_failures_total" {
failures = int(v.GetMetric()[0].GetCounter().GetValue())
const metricName = "contrast_grpc_server_handled_total"
metricFamily, ok := metrics[metricName]
require.True(ok, "metric family %q not found", metricName)
failures := 0
for _, metric := range metricFamily.GetMetric() {
for _, labelPair := range metric.GetLabel() {
if labelPair.Name == nil || *labelPair.Name != "grpc_code" {
continue
}
if labelPair.Value == nil || *labelPair.Value != "PermissionDenied" {
break
}
failures += int(metric.GetCounter().GetValue())
}
}
if failures == -1 {
// metric not found
t.Error("metric \"contrast_meshapi_attestation_failures_total\" not found")
}
return failures
}
39 changes: 13 additions & 26 deletions internal/attestation/snp/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import (
"encoding/hex"
"fmt"
"log/slog"
"slices"

"github.com/edgelesssys/contrast/internal/attestation"
"github.com/edgelesssys/contrast/internal/attestation/reportdata"
"github.com/edgelesssys/contrast/internal/manifest"
"github.com/edgelesssys/contrast/internal/oid"
"github.com/google/go-sev-guest/abi"
"github.com/google/go-sev-guest/proto/sevsnp"
Expand All @@ -24,33 +22,30 @@ import (

// Validator validates attestation statements.
type Validator struct {
verifyOpts *verify.Options
validateOpts *validate.Options
allowedHostDataEntries []manifest.HexString // Allowed host data entries in the report. If any of these is present, the report is considered valid.
reportSetter attestation.ReportSetter
logger *slog.Logger
verifyOpts *verify.Options
validateOpts *validate.Options
reportSetter attestation.ReportSetter
logger *slog.Logger
}

// NewValidator returns a new Validator.
func NewValidator(VerifyOpts *verify.Options, ValidateOpts *validate.Options, allowedHostDataEntries []manifest.HexString, log *slog.Logger) *Validator {
func NewValidator(VerifyOpts *verify.Options, ValidateOpts *validate.Options, log *slog.Logger) *Validator {
return &Validator{
verifyOpts: VerifyOpts,
validateOpts: ValidateOpts,
allowedHostDataEntries: allowedHostDataEntries,
logger: log,
verifyOpts: VerifyOpts,
validateOpts: ValidateOpts,
logger: log,
}
}

// NewValidatorWithReportSetter returns a new Validator with a report setter.
func NewValidatorWithReportSetter(VerifyOpts *verify.Options, ValidateOpts *validate.Options, allowedHostDataEntries []manifest.HexString,
func NewValidatorWithReportSetter(VerifyOpts *verify.Options, ValidateOpts *validate.Options,
log *slog.Logger, reportSetter attestation.ReportSetter,
) *Validator {
return &Validator{
verifyOpts: VerifyOpts,
validateOpts: ValidateOpts,
allowedHostDataEntries: allowedHostDataEntries,
reportSetter: reportSetter,
logger: log,
verifyOpts: VerifyOpts,
validateOpts: ValidateOpts,
reportSetter: reportSetter,
logger: log,
}
}

Expand Down Expand Up @@ -95,14 +90,6 @@ func (v *Validator) Validate(attDocRaw []byte, nonce []byte, peerPublicKey []byt
}
v.logger.Info("Successfully validated report data")

// Validate the host data.

if !slices.ContainsFunc(v.allowedHostDataEntries, func(entry manifest.HexString) bool {
return manifest.NewHexString(attestationData.Report.HostData) == entry
}) {
return fmt.Errorf("host data not allowed (found: %v allowed: %v)", attestationData.Report.HostData, v.allowedHostDataEntries)
}

if v.reportSetter != nil {
report := snpReport{report: attestationData.Report}
v.reportSetter.SetReport(report)
Expand Down
Loading