Skip to content

Commit

Permalink
testutil: Add locking to some tpm2.TPMDevice implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisccoulson committed Aug 4, 2024
1 parent 60cdf72 commit 8b94daf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 14 additions & 1 deletion testutil/tpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"path/filepath"
"strconv"
"strings"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -643,6 +644,7 @@ func NewTransportT(t *testing.T, features TPMFeatureFlags) *Transport {
// safe to use from a single goroutine, and this device returns multiple
// pointers to the same transport.
type TransportBackedDevice struct {
mu sync.Mutex
transport *Transport
closable bool
opened int
Expand Down Expand Up @@ -679,6 +681,9 @@ func NewTransportBackedDevice(transport *Transport, closable bool) *TransportBac
// NumberOpen returns the number of currently open transports opened from
// this device. This will decrement when a transport is closed.
func (d *TransportBackedDevice) NumberOpen() int {
d.mu.Lock()
defer d.mu.Unlock()

return d.opened
}

Expand All @@ -693,7 +698,8 @@ func (t *duplicateTransport) Close() error {
if t.closed {
return errors.New("transport already closed")
}
// No locking becuase these should all be used on the same goroutine
t.device.mu.Lock()
defer t.device.mu.Unlock()
t.device.opened -= 1
t.closed = true

Expand All @@ -713,6 +719,9 @@ func (t *duplicateTransport) Unwrap() tpm2.Transport {
// means each call to this returns transports that generally have to be used on the
// same gorountine.
func (d *TransportBackedDevice) Open() (tpm2.Transport, error) {
d.mu.Lock()
defer d.mu.Unlock()

d.opened += 1
return &duplicateTransport{
Transport: d.transport,
Expand All @@ -725,6 +734,7 @@ func (*TransportBackedDevice) String() string {
}

type transportPassthroughDevice struct {
mu sync.Mutex
transport *Transport
}

Expand All @@ -736,6 +746,9 @@ func NewTransportPassthroughDevice(transport *Transport) tpm2.TPMDevice {
}

func (d *transportPassthroughDevice) Open() (tpm2.Transport, error) {
d.mu.Lock()
defer d.mu.Unlock()

transport := d.transport
d.transport = nil
if transport == nil {
Expand Down
2 changes: 2 additions & 0 deletions types_constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,8 @@ const (
PropertyNVBufferMax Property = 0x12c // TPM_PT_NV_BUFFER_MAX
PropertyModes Property = 0x12d // TPM_PT_MODES
PropertyMaxCapBuffer Property = 0x12e // TPM_PT_MAX_CAP_BUFFER
PropertyFirmwareSVN Property = 0x12f // TPM_PT_FIRMWARE_SVN
PropertyFirmwareMaxSVN Property = 0x130 // TPM_PT_FIRMWARE_MAX_SVN

PropertyFixed Property = PropertyFamilyIndicator
)
Expand Down

0 comments on commit 8b94daf

Please sign in to comment.