-
Notifications
You must be signed in to change notification settings - Fork 17
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
efi: change how HostEnvironment overrides EFI variables #312
Changes from all commits
aaba2c5
1c78a22
35d03b3
04110a4
935869c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
package internal_test | ||
|
||
import ( | ||
"context" | ||
_ "embed" | ||
"io" | ||
"os" | ||
|
@@ -35,86 +36,26 @@ import ( | |
. "gopkg.in/check.v1" | ||
) | ||
|
||
var ( | ||
//go:embed MicrosoftKEK.crt | ||
msKEKCertPEM []byte | ||
|
||
msKEKCert []byte | ||
) | ||
|
||
func init() { | ||
msKEKCert = testutil.MustDecodePEMType("CERTIFICATE", msKEKCertPEM) | ||
} | ||
|
||
type defaultEnvSuite struct{} | ||
|
||
var _ = Suite(&defaultEnvSuite{}) | ||
|
||
type testReadVarData struct { | ||
name string | ||
guid efi.GUID | ||
} | ||
|
||
func (s *defaultEnvSuite) testReadVar(c *C, data *testReadVarData) { | ||
ownerGuid := efi.MakeGUID(0x77fa9abd, 0x0359, 0x4d32, 0xbd60, [...]uint8{0x28, 0xf4, 0xe7, 0x8f, 0x78, 0x4b}) | ||
kek := &efi.SignatureList{ | ||
Type: efi.CertX509Guid, | ||
Signatures: []*efi.SignatureData{ | ||
{ | ||
Owner: ownerGuid, | ||
Data: msKEKCert, | ||
}, | ||
}, | ||
} | ||
dbx := efitest.NewSignatureListNullSHA256(ownerGuid) | ||
vars := efitest.MakeMockVars() | ||
vars.SetSecureBoot(true) | ||
vars.SetKEK(c, efi.SignatureDatabase{kek}) | ||
vars.SetDbx(c, efi.SignatureDatabase{dbx}) | ||
|
||
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 | ||
}) | ||
defer restore() | ||
|
||
payload, attrs, err := DefaultEnv.ReadVar(data.name, data.guid) | ||
|
||
entry, exists := vars[efi.VariableDescriptor{Name: data.name, GUID: data.guid}] | ||
if !exists { | ||
c.Check(err, Equals, efi.ErrVarNotExist) | ||
} else { | ||
c.Check(err, IsNil) | ||
c.Check(attrs, Equals, entry.Attrs) | ||
c.Check(payload, DeepEquals, entry.Payload) | ||
} | ||
} | ||
|
||
func (s *defaultEnvSuite) TestReadVar1(c *C) { | ||
s.testReadVar(c, &testReadVarData{ | ||
name: "SecureBoot", | ||
guid: efi.GlobalVariable}) | ||
} | ||
type testKey struct{} | ||
|
||
func (s *defaultEnvSuite) TestReadVar2(c *C) { | ||
s.testReadVar(c, &testReadVarData{ | ||
name: "KEK", | ||
guid: efi.GlobalVariable}) | ||
} | ||
func (s *defaultEnvSuite) TestVarContext(c *C) { | ||
ctx := DefaultEnv.VarContext(context.WithValue(context.Background(), testKey{}, int64(10))) | ||
c.Assert(ctx, NotNil) | ||
|
||
func (s *defaultEnvSuite) TestReadVar3(c *C) { | ||
s.testReadVar(c, &testReadVarData{ | ||
name: "dbx", | ||
guid: efi.ImageSecurityDatabaseGuid}) | ||
} | ||
expected := efi.WithDefaultVarsBackend(context.Background()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe a good idea to pass something with an attached value already and check it can be retrieved? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added this to the test |
||
c.Check(ctx.Value(efi.VarsBackendKey{}), Equals, expected.Value(efi.VarsBackendKey{})) | ||
|
||
func (s *defaultEnvSuite) TestReadVarNotExist(c *C) { | ||
s.testReadVar(c, &testReadVarData{ | ||
name: "SecureBoot", | ||
guid: efi.ImageSecurityDatabaseGuid}) | ||
// Make sure that the returned context has the right parent by testing the | ||
// value we attached to it. | ||
testVal := ctx.Value(testKey{}) | ||
c.Assert(testVal, NotNil) | ||
testVali64, ok := testVal.(int64) | ||
c.Assert(ok, testutil.IsTrue) | ||
c.Check(testVali64, Equals, int64(10)) | ||
} | ||
|
||
func (s *defaultEnvSuite) testReadEventLog(c *C, opts *efitest.LogOptions) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
|
||
/* | ||
* Copyright (C) 2024 Canonical Ltd | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 3 as | ||
* published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package internal | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/canonical/tcglog-parser" | ||
) | ||
|
||
// HostEnvironment is an interface that abstracts out an EFI environment, so that | ||
// consumers of the API can provide a custom mechanism to read EFI variables or parse | ||
// the TCG event log. This needs to be kept in sync with [efi.HostEnvironment]. | ||
type HostEnvironment interface { | ||
// VarContext returns a copy of parent containing a VarsBackend, keyed by efi.VarsBackendKey, | ||
// for interacting with EFI variables via go-efilib. This context can be passed to any | ||
// go-efilib function that interacts with EFI variables. Right now, go-efilib doesn't | ||
// support any other uses of the context such as cancelation or deadlines. The efivarfs | ||
// backend will support this eventually for variable writes because it currently implements | ||
// a retry loop that has a potential to race with other processes. Cancelation and / or | ||
// deadlines for sections of code that performs multiple reads using efivarfs may be useful | ||
// in the future because the kernel rate-limits reads per-user. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same about parent |
||
VarContext(parent context.Context) context.Context | ||
|
||
// ReadEventLog reads the TCG event log | ||
ReadEventLog() (*tcglog.Log, error) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should the doc say something about parent?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated the documentation now.