Skip to content

Commit

Permalink
efi: fix an incorrect calculation and add some documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisccoulson committed Dec 1, 2023
1 parent 8cace2b commit 3ab72af
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
27 changes: 18 additions & 9 deletions efi/grub.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,37 @@ import (
pe "github.com/snapcore/secboot/internal/pe1.14"
)

// grubObjTypePrefix is the module type for a module containing the
// grub prefix.
const grubObjTypePrefix uint32 = 3

// grubModuleHeader is an individual module header
type grubModuleHeader struct {
Type uint32
Size uint32
Type uint32 // the module type
Size uint32 // the module size, including the header
}

// grubModuleMagic represent the bytes that the mods section starts with.
const grubModuleMagic uint32 = 0x676d696d

// grubModuleInfo32 is the header at the start of the mods section, for
// 32 bit builds.
type grubModuleInfo32 struct {
Magic uint32
Offset uint32
Size uint32
Offset uint32 // the offset of the first module, from the start of this header
Size uint32 // the size of all modules, including this header
}

// grubModuleInfo64 is the header at the start of the mods section, for
// 64 bit builds.
type grubModuleInfo64 struct {
Magic uint32
Padding uint32
Offset uint64
Size uint64
Offset uint64 // the offset of the first module, from the start of this header
Size uint64 // the size of all modules, including this header
}

// grubModule represents a grub module
type grubModule struct {
Type uint32
*io.SectionReader
Expand Down Expand Up @@ -90,13 +99,13 @@ func (h *grubImageHandleImpl) mods() ([]grubModule, error) {
if info.Magic != grubModuleMagic {
return nil, errors.New("invalid modules magic")
}
if info.Size > math.MaxInt64 {
if info.Offset > 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)))
r = io.NewSectionReader(section, int64(info.Offset), int64(info.Size)-int64(info.Offset))
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 {
Expand All @@ -108,7 +117,7 @@ func (h *grubImageHandleImpl) mods() ([]grubModule, error) {
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)))
r = io.NewSectionReader(section, int64(info.Offset), int64(info.Size)-int64(info.Offset))
default:
return nil, fmt.Errorf("unrecognized machine: %d", h.Machine())
}
Expand Down
2 changes: 1 addition & 1 deletion efi/image_rules_defs.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func makeFallbackImageRules() *imageRules {
),
// Ubuntu grub
newImageRule(
"grub",
"Ubuntu grub",
imageMatchesAll(
imageSectionExists("mods"),
grubHasPrefix("/EFI/ubuntu"),
Expand Down

0 comments on commit 3ab72af

Please sign in to comment.