Skip to content

Commit

Permalink
efi: update go-efilib to v0.9.6
Browse files Browse the repository at this point in the history
This adds a new function for mocking the variable backend, which will
be required in a future PR which uses new functions from go-efilib that
wrap around ReadVariable but where we want to be able to override
the HostEnvironment in production code.
  • Loading branch information
chrisccoulson committed May 30, 2024
1 parent 91a8370 commit 5714b41
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 18 deletions.
3 changes: 1 addition & 2 deletions efi/default_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,12 @@ import (

var (
eventLogPath = "/sys/kernel/security/tpm0/binary_bios_measurements" // Path of the TCG event log for the default TPM, in binary form
readVar = efi.ReadVariable
)

type defaultEnvImpl struct{}

func (e defaultEnvImpl) ReadVar(name string, guid efi.GUID) ([]byte, efi.VariableAttributes, error) {
return readVar(name, guid)
return efi.ReadVariable(name, guid)
}

func (e defaultEnvImpl) ReadEventLog() (*tcglog.Log, error) {
Expand Down
30 changes: 23 additions & 7 deletions efi/default_env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package efi_test

import (
"errors"
"io"
"os"
"path/filepath"
Expand All @@ -33,6 +34,27 @@ import (
. "gopkg.in/check.v1"
)

// TODO: make efitest.MockVars implement efi.VarsBackend in a future PR.
type mockVarsBackend struct {
vars efitest.MockVars
}

func (v *mockVarsBackend) Get(name string, guid efi.GUID) (efi.VariableAttributes, []byte, error) {
entry, exists := v.vars[efi.VariableDescriptor{Name: name, GUID: guid}]
if !exists {
return 0, nil, efi.ErrVarNotExist
}
return entry.Attrs, entry.Payload, nil
}

func (v *mockVarsBackend) Set(name string, guid efi.GUID, attrs efi.VariableAttributes, data []byte) error {
return errors.New("not implemented")
}

func (v *mockVarsBackend) List() ([]efi.VariableDescriptor, error) {
return nil, errors.New("not implemented")
}

type defaultEnvSuite struct{}

var _ = Suite(&defaultEnvSuite{})
Expand All @@ -44,13 +66,7 @@ type testReadVarData struct {

func (s *defaultEnvSuite) testReadVar(c *C, data *testReadVarData) {
vars := makeMockVars(c, withMsSecureBootConfig())
restore := MockReadVar(func(name string, guid efi.GUID) ([]byte, efi.VariableAttributes, error) {
entry, exists := vars[efi.VariableDescriptor{Name: name, GUID: guid}]
if !exists {
return nil, 0, efi.ErrVarNotExist
}
return entry.Payload, entry.Attrs, nil
})
restore := efi.MockVarsBackend(&mockVarsBackend{vars: vars})
defer restore()

payload, attrs, err := DefaultEnv.ReadVar(data.name, data.guid)
Expand Down
8 changes: 0 additions & 8 deletions efi/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,6 @@ func MockOpenPeImage(fn func(Image) (peImageHandle, error)) (restore func()) {
}
}

func MockReadVar(fn func(string, efi.GUID) ([]byte, efi.VariableAttributes, error)) (restore func()) {
origReadVar := readVar
readVar = fn
return func() {
readVar = origReadVar
}
}

func MockSnapdenvTesting(testing bool) (restore func()) {
orig := snapdenvTesting
snapdenvTesting = func() bool { return testing }
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/snapcore/secboot
go 1.18

require (
github.com/canonical/go-efilib v0.9.5
github.com/canonical/go-efilib v0.9.6
github.com/canonical/go-sp800.108-kdf v0.0.0-20210315104021-ead800bbf9a0
github.com/canonical/go-sp800.90a-drbg v0.0.0-20210314144037-6eeb1040d6c3
github.com/canonical/go-tpm2 v1.3.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/canonical/go-efilib v0.9.4 h1:cD6oNSWeQSgeSeJZMCxhGEW4GoLSxFhIJ12Hg3v
github.com/canonical/go-efilib v0.9.4/go.mod h1:tHjv3Mni7hEpNSUNd1KJEV/AZJsFSH6LX/EQ0I75AZE=
github.com/canonical/go-efilib v0.9.5 h1:zRpWG4z61GiYsEmFYvXYuj+8xV2eJ200YY5Ht9EjrRU=
github.com/canonical/go-efilib v0.9.5/go.mod h1:tHjv3Mni7hEpNSUNd1KJEV/AZJsFSH6LX/EQ0I75AZE=
github.com/canonical/go-efilib v0.9.6 h1:nHoHBqw+wJln/T9hRy+soKOezsOkPGz1PYIhv3Anmqc=
github.com/canonical/go-efilib v0.9.6/go.mod h1:n0Ttsy1JuHAvqaFbZBs6PAzoiiJdfkHsAmDOEbexYEQ=
github.com/canonical/go-sp800.108-kdf v0.0.0-20210314145419-a3359f2d21b9/go.mod h1:Zrs3YjJr+w51u0R/dyLh/oWt/EcBVdLPCVFYC4daW5s=
github.com/canonical/go-sp800.108-kdf v0.0.0-20210315104021-ead800bbf9a0 h1:ZE2XMRFHcwlib3uU9is37+pKkkMloVoEPWmgQ6GK1yo=
github.com/canonical/go-sp800.108-kdf v0.0.0-20210315104021-ead800bbf9a0/go.mod h1:Zrs3YjJr+w51u0R/dyLh/oWt/EcBVdLPCVFYC4daW5s=
Expand Down

0 comments on commit 5714b41

Please sign in to comment.