Skip to content

Commit

Permalink
Merge pull request #295 from chrisccoulson/efi-add-kernel-config-profile
Browse files Browse the repository at this point in the history
efi: add WithKernelConfigProfile option to AddPCRProfile.

This replaces the separate calls to AddSystemdStubProfile and
tpm2.AddSnapModelProfile, which are now deprecated and will be
removed in a follow-up PR.
  • Loading branch information
chrisccoulson authored Apr 15, 2024
2 parents fbea4da + 427c0f7 commit 21595ba
Show file tree
Hide file tree
Showing 15 changed files with 469 additions and 109 deletions.
1 change: 1 addition & 0 deletions efi/efi.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ package efi
const (
bootManagerCodePCR = 4 // Boot Manager Code and Boot Attempts PCR
secureBootPCR = 7 // Secure Boot Policy Measurements PCR
kernelConfigPCR = 12
)
11 changes: 9 additions & 2 deletions efi/efi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,29 @@ type mockPcrBranchEvent struct {

type mockPcrBranchContext struct {
PcrProfileContext
params LoadParams
vars VarReadWriter
fc *FwContext
sc *ShimContext
events []*mockPcrBranchEvent
}

func newMockPcrBranchContext(pc PcrProfileContext, vars VarReadWriter) *mockPcrBranchContext {
func newMockPcrBranchContext(pc PcrProfileContext, params *LoadParams, vars VarReadWriter) *mockPcrBranchContext {
if params == nil {
params = new(LoadParams)
}
return &mockPcrBranchContext{
PcrProfileContext: pc,
params: *params,
vars: vars,
fc: new(FwContext),
sc: new(ShimContext),
}
}

func (*mockPcrBranchContext) Params() *LoadParams { return nil }
func (c *mockPcrBranchContext) Params() *LoadParams {
return &c.params
}

func (c *mockPcrBranchContext) Vars() VarReadWriter {
return c.vars
Expand Down
1 change: 1 addition & 0 deletions efi/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
const (
BootManagerCodeProfile = bootManagerCodeProfile
GrubChainloaderUsesShimProtocol = grubChainloaderUsesShimProtocol
KernelConfigProfile = kernelConfigProfile
SecureBootPolicyProfile = secureBootPolicyProfile
ShimFixVariableAuthorityEventsMatchSpec = shimFixVariableAuthorityEventsMatchSpec
ShimHasSbatRevocationManagement = shimHasSbatRevocationManagement
Expand Down
3 changes: 3 additions & 0 deletions efi/fw_load_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ func (h *fwLoadHandler) MeasureImageStart(ctx pcrBranchContext) error {
if ctx.Flags()&bootManagerCodeProfile > 0 {
h.measureBootManagerCodePreOS(ctx)
}
if ctx.Flags()&kernelConfigProfile > 0 {
ctx.ResetPCR(kernelConfigPCR)
}

return nil
}
Expand Down
12 changes: 6 additions & 6 deletions efi/fw_load_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (s *fwLoadHandlerSuite) testMeasureImageStart(c *C, data *testFwMeasureImag
collector := NewRootVarsCollector(efitest.NewMockHostEnvironment(data.vars, nil))
ctx := newMockPcrBranchContext(&mockPcrProfileContext{
alg: data.alg,
flags: data.flags}, collector.Next())
flags: data.flags}, nil, collector.Next())

handler := NewFwLoadHandler(efitest.NewLog(c, data.logOptions))
c.Check(handler.MeasureImageStart(ctx), IsNil)
Expand Down Expand Up @@ -211,7 +211,7 @@ func (s *fwLoadHandlerSuite) TestMeasureImageStartErrBadLog1(c *C) {
collector := NewRootVarsCollector(efitest.NewMockHostEnvironment(makeMockVars(c, withMsSecureBootConfig()), nil))
ctx := newMockPcrBranchContext(&mockPcrProfileContext{
alg: tpm2.HashAlgorithmSHA256,
flags: SecureBootPolicyProfile}, collector.Next())
flags: SecureBootPolicyProfile}, nil, collector.Next())

log := efitest.NewLog(c, &efitest.LogOptions{Algorithms: []tpm2.HashAlgorithmId{tpm2.HashAlgorithmSHA256, tpm2.HashAlgorithmSHA1}})
for i, event := range log.Events {
Expand All @@ -234,7 +234,7 @@ func (s *fwLoadHandlerSuite) TestMeasureImageStartErrBadLog2(c *C) {
collector := NewRootVarsCollector(efitest.NewMockHostEnvironment(makeMockVars(c, withMsSecureBootConfig()), nil))
ctx := newMockPcrBranchContext(&mockPcrProfileContext{
alg: tpm2.HashAlgorithmSHA256,
flags: SecureBootPolicyProfile}, collector.Next())
flags: SecureBootPolicyProfile}, nil, collector.Next())

log := efitest.NewLog(c, &efitest.LogOptions{Algorithms: []tpm2.HashAlgorithmId{tpm2.HashAlgorithmSHA256, tpm2.HashAlgorithmSHA1}})
for i, event := range log.Events {
Expand All @@ -260,7 +260,7 @@ func (s *fwLoadHandlerSuite) TestMeasureImageStartErrBadLog3(c *C) {
collector := NewRootVarsCollector(efitest.NewMockHostEnvironment(makeMockVars(c, withMsSecureBootConfig()), nil))
ctx := newMockPcrBranchContext(&mockPcrProfileContext{
alg: tpm2.HashAlgorithmSHA256,
flags: SecureBootPolicyProfile}, collector.Next())
flags: SecureBootPolicyProfile}, nil, collector.Next())

log := efitest.NewLog(c, &efitest.LogOptions{Algorithms: []tpm2.HashAlgorithmId{tpm2.HashAlgorithmSHA256, tpm2.HashAlgorithmSHA1}})
for i, event := range log.Events {
Expand All @@ -286,7 +286,7 @@ func (s *fwLoadHandlerSuite) TestMeasureImageStartErrBadLog4(c *C) {
collector := NewRootVarsCollector(efitest.NewMockHostEnvironment(makeMockVars(c, withMsSecureBootConfig()), nil))
ctx := newMockPcrBranchContext(&mockPcrProfileContext{
alg: tpm2.HashAlgorithmSHA256,
flags: SecureBootPolicyProfile}, collector.Next())
flags: SecureBootPolicyProfile}, nil, collector.Next())

log := efitest.NewLog(c, &efitest.LogOptions{Algorithms: []tpm2.HashAlgorithmId{tpm2.HashAlgorithmSHA256, tpm2.HashAlgorithmSHA1}})
for i, event := range log.Events {
Expand Down Expand Up @@ -323,7 +323,7 @@ func (s *fwLoadHandlerSuite) testMeasureImageLoad(c *C, data *testFwMeasureImage
alg: data.alg,
flags: data.flags,
handlers: s,
}, nil)
}, nil, nil)
if data.fc != nil {
ctx.fc = data.fc
}
Expand Down
8 changes: 4 additions & 4 deletions efi/grub_load_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (s *grubLoadHandlerSuite) TestMeasureImageLoadUbuntuUsesShim15_7(c *C) {
alg: tpm2.HashAlgorithmSHA256,
flags: SecureBootPolicyProfile,
handlers: s,
}, nil)
}, nil, nil)
ctx.FwContext().Db = &SecureBootDB{
Name: Db,
Contents: msDb(c),
Expand Down Expand Up @@ -73,7 +73,7 @@ func (s *grubLoadHandlerSuite) TestMeasureImageLoadUbuntuUsesShim15_6(c *C) {
alg: tpm2.HashAlgorithmSHA256,
flags: SecureBootPolicyProfile,
handlers: s,
}, nil)
}, nil, nil)
ctx.FwContext().Db = &SecureBootDB{
Name: Db,
Contents: msDb(c),
Expand Down Expand Up @@ -102,7 +102,7 @@ func (s *grubLoadHandlerSuite) TestMeasureImageLoadNoShim(c *C) {
alg: tpm2.HashAlgorithmSHA256,
flags: SecureBootPolicyProfile,
handlers: s,
}, nil)
}, nil, nil)
ctx.FwContext().Db = &SecureBootDB{
Name: Db,
Contents: append(msDb(c), efitest.NewSignatureListX509(c, canonicalCACert, testOwnerGuid)),
Expand Down Expand Up @@ -131,7 +131,7 @@ func (s *grubLoadHandlerSuite) TestMeasureImageLoadNoShimError(c *C) {
alg: tpm2.HashAlgorithmSHA256,
flags: SecureBootPolicyProfile,
handlers: s,
}, nil)
}, nil, nil)
ctx.FwContext().Db = &SecureBootDB{
Name: Db,
Contents: msDb(c),
Expand Down
12 changes: 12 additions & 0 deletions efi/pcr_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@ func WithBootManagerCodeProfile() PCRProfileOption {
return pcrProfileSetFlagsOption(bootManagerCodeProfile)
}

// WithKernelConfigProfile adds the kernel config profile. This binds a policy to a
// set of externally supplied commandlines. On Ubuntu Core, this also binds a policy
// to a set of model assertions and the initrd phase of the boot.
//
// Kernel commandlines can be injected into the profile with [KernelCommandlineParams].
// Snap models can be injected into the profile with [SnapModelParams]. Note that a model
// assertion is mandatory for profiles that include a UKI for Ubuntu Core.
func WithKernelConfigProfile() PCRProfileOption {
return pcrProfileSetFlagsOption(kernelConfigProfile)
}

// AddPCRProfile adds a profile defined by the supplied options to the supplied
// secboot_tpm2.PCRProtectionProfileBranch, using the specified digest algorithm
// for the PCR digest. The generated profile is defined by the supplied load
Expand All @@ -154,6 +165,7 @@ type pcrProfileFlags int
const (
secureBootPolicyProfile pcrProfileFlags = 1 << iota
bootManagerCodeProfile
kernelConfigProfile
)

type pcrProfileGenerator struct {
Expand Down
Loading

0 comments on commit 21595ba

Please sign in to comment.