File: client_linux.go

package info (click to toggle)
golang-github-google-go-sev-guest 0.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 820 kB
  • sloc: asm: 9; makefile: 3
file content (288 lines) | stat: -rw-r--r-- 9,027 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build linux || freebsd || openbsd || netbsd

// Package client provides an interface to the AMD SEV-SNP guest device commands.
package client

import (
	"flag"
	"fmt"
	"strconv"
	"time"

	"github.com/google/go-configfs-tsm/configfs/linuxtsm"
	"github.com/google/go-configfs-tsm/report"
	"github.com/google/go-sev-guest/abi"
	labi "github.com/google/go-sev-guest/client/linuxabi"
	spb "github.com/google/go-sev-guest/proto/sevsnp"
	"golang.org/x/sys/unix"
)

const (
	// defaultSevGuestDevicePath is the platform's usual device path to the SEV guest.
	defaultSevGuestDevicePath = "/dev/sev-guest"
	installURL                = "https://github.com/google/go-sev-guest/blob/main/INSTALL.md"
)

// These flags should not be needed for long term health of the project as the Linux kernel
// catches up with throttling-awareness.
var (
	throttleDuration = flag.Duration("self_throttle_duration", 2*time.Second, "Rate-limit library-initiated device commands to this duration")
	burstMax         = flag.Int("self_throttle_burst", 1, "Rate-limit library-initiated device commands to this many commands per duration")
	defaultVMPL      = flag.String("default_vmpl", "", "Default VMPL to use for attestation (empty for driver default)")
)

// LinuxDevice implements the Device interface with Linux ioctls.
type LinuxDevice struct {
	fd      int
	lastCmd time.Time
	burst   int
}

// Open opens the SEV-SNP guest device from a given path
func (d *LinuxDevice) Open(path string) error {
	fd, err := unix.Open(path, unix.O_RDWR, 0)
	if err != nil {
		d.fd = -1
		return fmt.Errorf("could not open AMD SEV guest device at %s (see %s): %v", path, installURL, err)
	}
	d.fd = fd
	return nil
}

// OpenDevice opens the SEV-SNP guest device.
func OpenDevice() (*LinuxDevice, error) {
	result := &LinuxDevice{}
	path := *sevGuestPath
	if UseDefaultSevGuest() {
		path = defaultSevGuestDevicePath
	}
	if err := result.Open(path); err != nil {
		return nil, err
	}
	return result, nil
}

// Close closes the SEV-SNP guest device.
func (d *LinuxDevice) Close() error {
	if d.fd == -1 { // Not open
		return nil
	}
	if err := unix.Close(d.fd); err != nil {
		return err
	}
	// Prevent double-close.
	d.fd = -1
	return nil
}

// Ioctl sends a command with its wrapped request and response values to the Linux device.
func (d *LinuxDevice) Ioctl(command uintptr, req any) (uintptr, error) {
	// TODO(Issue #40): Remove the workaround to the ENOTTY lockout when throttled
	// in Linux 6.1 by throttling ourselves first.
	if d.burst == 0 {
		sinceLast := time.Since(d.lastCmd)
		// Self-throttle for tests without guest OS throttle detection
		if sinceLast < *throttleDuration {
			time.Sleep(*throttleDuration - sinceLast)
		}
	}
	switch sreq := req.(type) {
	case *labi.SnpUserGuestRequest:
		abi := sreq.ABI()
		result, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(d.fd), command, uintptr(abi.Pointer()))
		abi.Finish(sreq)
		d.burst = (d.burst + 1) % *burstMax
		if d.burst == 0 {
			d.lastCmd = time.Now()
		}

		// TODO(Issue #5): remove the work around for the kernel bug that writes
		// uninitialized memory back on non-EIO.
		if errno != unix.EIO {
			sreq.FwErr = 0
		}
		if errno != 0 {
			return 0, errno
		}
		return result, nil
	}
	return 0, fmt.Errorf("unexpected request value: %v", req)
}

// Product returns the current CPU's associated AMD SEV product information.
func (d *LinuxDevice) Product() *spb.SevProduct {
	return abi.SevProduct()
}

// LinuxIoctlQuoteProvider implements the QuoteProvider interface to fetch
// attestation quote via the deprecated /dev/sev-guest ioctl.
type LinuxIoctlQuoteProvider struct{}

// IsSupported checks if TSM client can be created to use /dev/sev-guest ioctl.
func (p *LinuxIoctlQuoteProvider) IsSupported() bool {
	d, err := OpenDevice()
	if err != nil {
		return false
	}
	d.Close()
	return true
}

// GetRawQuoteAtLevel returns byte format attestation plus certificate table via /dev/sev-guest ioctl.
func (p *LinuxIoctlQuoteProvider) GetRawQuoteAtLevel(reportData [64]byte, level uint) ([]uint8, error) {
	d, err := OpenDevice()
	if err != nil {
		return nil, err
	}
	defer d.Close()
	// If there are no certificates, then just return the raw report.
	length, err := queryCertificateLength(d, int(level))
	if err != nil {
		return GetRawReportAtVmpl(d, reportData, int(level))
	}
	certs := make([]byte, length)
	report, _, err := getExtendedReportIn(d, reportData, int(level), certs)
	if err != nil {
		return nil, err
	}
	// Mix the platform info in with the auxblob.
	extended, err := abi.ExtendedPlatformCertTable(certs)
	if err != nil {
		return nil, fmt.Errorf("invalid certificate table: %v", err)
	}
	return append(report, extended...), nil
}

// GetRawQuote returns byte format attestation plus certificate table via /dev/sev-guest ioctl.
func (p *LinuxIoctlQuoteProvider) GetRawQuote(reportData [64]byte) ([]uint8, error) {
	if *defaultVMPL == "" {
		return p.GetRawQuoteAtLevel(reportData, 0)
	}
	vmpl, err := strconv.ParseUint(*defaultVMPL, 10, 32)
	if err != nil {
		return nil, fmt.Errorf("bad default_vmpl: %q", *defaultVMPL)
	}
	return p.GetRawQuoteAtLevel(reportData, uint(vmpl))
}

// Product returns the current CPU's associated AMD SEV product information.
//
// Deprecated: Use ExtraPlatformInfoGUID from the cert table.
func (*LinuxIoctlQuoteProvider) Product() *spb.SevProduct {
	return abi.SevProduct()
}

// LinuxConfigFsQuoteProvider implements the QuoteProvider interface to fetch
// attestation quote via ConfigFS.
type LinuxConfigFsQuoteProvider struct{}

// IsSupported checks if TSM client can be created to use ConfigFS system.
func (p *LinuxConfigFsQuoteProvider) IsSupported() bool {
	c, err := linuxtsm.MakeClient()
	if err != nil {
		return false
	}
	r, err := report.Create(c, &report.Request{})
	if err != nil {
		return false
	}
	provider, err := r.ReadOption("provider")
	return err == nil && string(provider) == "sev_guest\n"
}

// GetRawQuoteAtLevel returns byte format attestation plus certificate table via ConfigFS.
func (p *LinuxConfigFsQuoteProvider) GetRawQuoteAtLevel(reportData [64]byte, level uint) ([]uint8, error) {
	req := &report.Request{
		InBlob:     reportData[:],
		GetAuxBlob: true,
		Privilege: &report.Privilege{
			Level: level,
		},
	}
	resp, err := linuxtsm.GetReport(req)
	if err != nil {
		return nil, err
	}
	// Mix the platform info in with the auxblob.
	extended, err := abi.ExtendedPlatformCertTable(resp.AuxBlob)
	if err != nil {
		return nil, fmt.Errorf("invalid certificate table: %v", err)
	}
	return append(resp.OutBlob, extended...), nil
}

// GetRawQuote returns byte format attestation plus certificate table via ConfigFS.
func (p *LinuxConfigFsQuoteProvider) GetRawQuote(reportData [64]byte) ([]uint8, error) {
	req := &report.Request{
		InBlob:     reportData[:],
		GetAuxBlob: true,
	}
	if *defaultVMPL != "" {
		vmpl, err := strconv.ParseUint(*defaultVMPL, 10, 32)
		if err != nil {
			return nil, fmt.Errorf("bad default_vmpl: %q", *defaultVMPL)
		}
		req.Privilege = &report.Privilege{
			Level: uint(vmpl),
		}
	}
	resp, err := linuxtsm.GetReport(req)
	if err != nil {
		return nil, err
	}
	// Mix the platform info in with the auxblob.
	extended, err := abi.ExtendedPlatformCertTable(resp.AuxBlob)
	if err != nil {
		return nil, fmt.Errorf("invalid certificate table: %v", err)
	}
	return append(resp.OutBlob, extended...), nil
}

// Product returns the current CPU's associated AMD SEV product information.
//
// Deprecated: Use ExtraPlatformInfoGUID from the cert table.
func (*LinuxConfigFsQuoteProvider) Product() *spb.SevProduct {
	return abi.SevProduct()
}

// GetQuoteProvider returns a supported SEV-SNP QuoteProvider.
func GetQuoteProvider() (QuoteProvider, error) {
	var provider QuoteProvider
	provider = &LinuxConfigFsQuoteProvider{}
	if provider.IsSupported() {
		return provider, nil
	}
	provider = &LinuxIoctlQuoteProvider{}
	if provider.IsSupported() {
		return provider, nil
	}
	return nil, fmt.Errorf("no supported SEV-SNP QuoteProvider found")
}

// GetLeveledQuoteProvider returns a supported SEV-SNP LeveledQuoteProvider.
func GetLeveledQuoteProvider() (LeveledQuoteProvider, error) {
	var provider LeveledQuoteProvider
	provider = &LinuxConfigFsQuoteProvider{}
	if provider.IsSupported() {
		return provider, nil
	}
	provider = &LinuxIoctlQuoteProvider{}
	if provider.IsSupported() {
		return provider, nil
	}
	return nil, fmt.Errorf("no supported SEV-SNP LeveledQuoteProvider found")
}