Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions filesystem/ext4/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ var (
gdtRELines = []testGDTLineHandler{
{regexp.MustCompile(`^Group (\d+): \(Blocks (\d+)-(\d+)\) csum 0x([0-9a-f]+) \[(.*)]`), func(gd *groupDescriptor, matches []string) error {
// group number
number, err := strconv.ParseUint(matches[1], 10, 16)
number, err := strconv.ParseUint(matches[1], 10, 32)
if err != nil {
return fmt.Errorf("failed to parse group number: %v", err)
}
gd.number = uint16(number)
gd.number = uint32(number)
// parse the flags
flags := strings.Split(matches[5], ",")
for _, flag := range flags {
Expand Down
2 changes: 1 addition & 1 deletion filesystem/ext4/ext4.go
Original file line number Diff line number Diff line change
Expand Up @@ -3284,7 +3284,7 @@ func buildGroupDescriptorsFromSuperblock(sb *superblock) groupDescriptors {

for g := 0; g < groups; g++ {
var d groupDescriptor
d.number = uint16(g)
d.number = uint32(g)
d.size = descSize

firstBlockOfGroup := uint64(sb.firstDataBlock) + uint64(g)*blocksPerGroup
Expand Down
18 changes: 8 additions & 10 deletions filesystem/ext4/groupdescriptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type groupDescriptor struct {
inodeBitmapChecksum uint32
unusedInodes uint32
size uint16
number uint16
number uint32
}

func (gd *groupDescriptor) equal(other *groupDescriptor) bool {
Expand Down Expand Up @@ -176,7 +176,7 @@ func groupDescriptorFromBytes(b []byte, gdSize uint16, number int, checksumType
checksumInput = b[0x0:0x40]
}

gdNumber := uint16(number)
gdNumber := uint32(number)

gd := groupDescriptor{
size: gdSize,
Expand Down Expand Up @@ -298,18 +298,16 @@ func (f *blockGroupFlags) toInt() uint16 {
}

// groupDescriptorChecksum calculate the checksum for a block group descriptor
// NOTE: we are assuming that the block group number is uint64, but we do not know that to be true
//
// it might be uint32 or uint64, and it might be in BigEndian as opposed to LittleEndian
// just have to start with this and see
// we do know that the maximum number of block groups in 32-bit mode is 2^19, which must be uint32
// and in 64-bit mode it is 2^51 which must be uint64
// So we start with uint32 = [4]byte{} for regular mode and [8]byte{} for mod32
func groupDescriptorChecksum(b []byte, hashSeed uint32, groupNumber uint16, checksumType gdtChecksumType) uint16 {
// according to this [1], the hard limit for the maximum nuber of block groups is 2^32, therefore
// we use uint32 as block group number.
//
// [1]: https://www.kernel.org/doc/html/latest/filesystems/ext4/blockgroup.html?utm_source=chatgpt.com#meta-block-groups
func groupDescriptorChecksum(b []byte, hashSeed, groupNumber uint32, checksumType gdtChecksumType) uint16 {
var checksum uint16

numBytes := make([]byte, 4)
binary.LittleEndian.PutUint16(numBytes, groupNumber)
binary.LittleEndian.PutUint32(numBytes, groupNumber)
switch checksumType {
case gdtChecksumNone:
checksum = 0
Expand Down
52 changes: 52 additions & 0 deletions filesystem/ext4/groupdescriptors_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ext4

import (
"bytes"
"encoding/binary"
"fmt"
"os"
"testing"
Expand Down Expand Up @@ -100,3 +102,53 @@ func TestGroupDescriptorsToBytes(t *testing.T) {
t.Errorf("groupDescriptors.toBytes() mismatched, actual then expected\n%s", diffString)
}
}

func TestGroupDescriptorChecksumLargeGroupNumbers(t *testing.T) {
const seed uint32 = 0x12345678

tests := []struct {
group uint32
checksum uint16
}{
{65535, 0x2068},
{65536, 0x8eba},
{65537, 0xaa81},
{0x80000000, 0x29e5},
{0xfffffffe, 0xc348},
{0xffffffff, 0xe773},
}

for _, tt := range tests {
t.Run(fmt.Sprintf("group_%d", tt.group), func(t *testing.T) {
// Synthetic 64-byte EXT4 group descriptor.
b := make([]byte, groupDescriptorSize64Bit)

binary.LittleEndian.PutUint32(b[0x00:], 42)
binary.LittleEndian.PutUint32(b[0x04:], 100)
binary.LittleEndian.PutUint16(b[0x1e:], tt.checksum)

gd, err := groupDescriptorFromBytes(
b,
groupDescriptorSize64Bit,
int(tt.group),
gdtChecksumMetadata,
seed,
)
if err != nil {
t.Fatalf("failed to parse group %d: %v", tt.group, err)
}

if gd.number != tt.group {
t.Errorf("group number = %d, want %d",
gd.number, tt.group)
}

// Verify that serialization preserves the checksum.
got := gd.toBytes(gdtChecksumMetadata, seed)
if !bytes.Equal(got, b) {
t.Errorf("group %d: serialized descriptor mismatch\n"+
"got: %x\nwant: %x", tt.group, got, b)
}
})
}
}
Loading