-
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: Make use of the grub's prefix for detection #268
Merged
chrisccoulson
merged 5 commits into
canonical:master
from
chrisccoulson:efi-use-grub-prefix-for-detection
Dec 12, 2023
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
395e488
efi: Make use of the grub's prefix for detection
chrisccoulson a0a435a
efi: add tests for grubHasPrefix
chrisccoulson 8cace2b
efi: add tests for grubImageHandle.Prefix
chrisccoulson 421f90a
efi: fix an incorrect calculation and add some documentation
chrisccoulson 8e2219e
efi: add some documentation and increase test coverage
chrisccoulson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
|
||
/* | ||
* Copyright (C) 2023 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 efi | ||
|
||
import ( | ||
"encoding/binary" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"math" | ||
|
||
pe "github.com/snapcore/secboot/internal/pe1.14" | ||
) | ||
|
||
const grubObjTypePrefix uint32 = 3 | ||
|
||
type grubModuleHeader struct { | ||
Type uint32 | ||
Size uint32 | ||
} | ||
|
||
const grubModuleMagic uint32 = 0x676d696d | ||
|
||
type grubModuleInfo32 struct { | ||
Magic uint32 | ||
Offset uint32 | ||
Size uint32 | ||
} | ||
|
||
type grubModuleInfo64 struct { | ||
Magic uint32 | ||
Padding uint32 | ||
Offset uint64 | ||
Size uint64 | ||
} | ||
|
||
type grubModule struct { | ||
Type uint32 | ||
*io.SectionReader | ||
} | ||
|
||
type grubImageHandle interface { | ||
peImageHandle | ||
|
||
Prefix() (string, error) | ||
} | ||
|
||
type grubImageHandleImpl struct { | ||
peImageHandle | ||
} | ||
|
||
// newGrubImageHandle returns a new grubImageHandle for the supplied peImageHandle. | ||
var newGrubImageHandle = func(image peImageHandle) grubImageHandle { | ||
return &grubImageHandleImpl{peImageHandle: image} | ||
} | ||
|
||
func (h *grubImageHandleImpl) mods() ([]grubModule, error) { | ||
section := h.OpenSection("mods") | ||
if section == nil { | ||
return nil, errors.New("no mods section") | ||
} | ||
|
||
var r *io.SectionReader | ||
switch h.Machine() { | ||
case pe.IMAGE_FILE_MACHINE_AMD64, pe.IMAGE_FILE_MACHINE_ARM64, pe.IMAGE_FILE_MACHINE_RISCV64: | ||
var info grubModuleInfo64 | ||
if err := binary.Read(section, binary.LittleEndian, &info); err != nil { | ||
return nil, fmt.Errorf("cannot obtain modules info: %w", err) | ||
} | ||
if info.Magic != grubModuleMagic { | ||
return nil, errors.New("invalid modules magic") | ||
} | ||
if info.Size > math.MaxInt64 { | ||
return nil, errors.New("invalid modules offset") | ||
} | ||
if info.Size > math.MaxInt64 || info.Size < uint64(binary.Size(info)) { | ||
return nil, errors.New("invalid modules size") | ||
} | ||
r = io.NewSectionReader(section, int64(info.Offset), int64(info.Size)-int64(binary.Size(info))) | ||
case pe.IMAGE_FILE_MACHINE_ARM, pe.IMAGE_FILE_MACHINE_I386, pe.IMAGE_FILE_MACHINE_RISCV32: | ||
var info grubModuleInfo32 | ||
if err := binary.Read(section, binary.LittleEndian, &info); err != nil { | ||
return nil, fmt.Errorf("cannot obtain modules info: %w", err) | ||
} | ||
if info.Magic != grubModuleMagic { | ||
return nil, errors.New("invalid module magic") | ||
} | ||
if info.Size < uint32(binary.Size(info.Size)) { | ||
return nil, errors.New("invalid modules size") | ||
} | ||
r = io.NewSectionReader(section, int64(info.Offset), int64(info.Size)-int64(binary.Size(info))) | ||
default: | ||
return nil, fmt.Errorf("unrecognized machine: %d", h.Machine()) | ||
} | ||
|
||
var mods []grubModule | ||
|
||
for { | ||
var hdr grubModuleHeader | ||
if err := binary.Read(r, binary.LittleEndian, &hdr); err != nil { | ||
if err == io.EOF { | ||
break | ||
} | ||
return nil, fmt.Errorf("cannot obtain module header: %w", err) | ||
} | ||
|
||
offset, _ := r.Seek(0, io.SeekCurrent) | ||
size := int64(hdr.Size) - int64(binary.Size(hdr)) | ||
mods = append(mods, grubModule{ | ||
Type: hdr.Type, | ||
SectionReader: io.NewSectionReader(r, offset, size), | ||
}) | ||
|
||
if _, err := r.Seek(size, io.SeekCurrent); err != nil { | ||
return nil, fmt.Errorf("cannot seek to next module: %w", err) | ||
} | ||
} | ||
|
||
return mods, nil | ||
} | ||
|
||
func (h *grubImageHandleImpl) Prefix() (string, error) { | ||
mods, err := h.mods() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
for _, mod := range mods { | ||
if mod.Type != grubObjTypePrefix { | ||
continue | ||
} | ||
|
||
prefix, err := io.ReadAll(newCstringReader(mod)) | ||
if err != nil { | ||
return "", fmt.Errorf("cannot obtain prefix: %w", err) | ||
} | ||
return string(prefix), nil | ||
} | ||
|
||
return "", nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -163,7 +163,7 @@ func makeMicrosoftUEFICASecureBootNamespaceRules() *secureBootNamespaceRules { | |
), | ||
imageMatchesAll( | ||
imageSectionExists("mods"), | ||
imageSignedByOrganization("Canonical Ltd."), | ||
grubHasPrefix("/EFI/ubuntu"), | ||
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. do we still need all the changes from the snakeoil PR? 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. We do |
||
), | ||
), | ||
newGrubLoadHandlerConstructor(grubChainloaderUsesShimProtocol).New, | ||
|
@@ -209,14 +209,22 @@ func makeFallbackImageRules() *imageRules { | |
imageSectionExists(".vendor_cert"), | ||
newShimLoadHandler, | ||
), | ||
// Ubuntu grub | ||
newImageRule( | ||
"grub", | ||
imageMatchesAll( | ||
imageSectionExists("mods"), | ||
grubHasPrefix("/EFI/ubuntu"), | ||
), | ||
newGrubLoadHandlerConstructor(grubChainloaderUsesShimProtocol).New, | ||
), | ||
// Grub | ||
newImageRule( | ||
"grub", | ||
imageSectionExists("mods"), | ||
newGrubLoadHandler, | ||
), | ||
// TODO: add rules for Ubuntu Core UKI and Ubuntu grub that are not part of | ||
// the MS UEFI CA? | ||
// TODO: add rules for Ubuntu Core UKIs that are not part of the MS UEFI CA | ||
// | ||
// Catch-all for unrecognized leaf images | ||
newImageRule( | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
info.Offset
?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.
This was addressed with 421f90a