From b702027dd5c1f6e5a8573d1adecb15ac533de4dc Mon Sep 17 00:00:00 2001 From: Ondrej Hruby Date: Fri, 20 Nov 2026 15:43:59 +0100 Subject: [PATCH] fix(ext4): use uint32 for block group numbers Fix group descriptor checksum verification for EXT4 filesystems with more than 65536 block groups. Previously, group numbers were truncated to uint16, causing checksum mismatches starting at group 65536. Use uint32 consistently and add regression tests covering the uint16 overflow boundary and the full uint32 range. --- filesystem/ext4/common_test.go | 4 +- filesystem/ext4/ext4.go | 2 +- filesystem/ext4/groupdescriptors.go | 18 ++++---- filesystem/ext4/groupdescriptors_test.go | 52 ++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 13 deletions(-) diff --git a/filesystem/ext4/common_test.go b/filesystem/ext4/common_test.go index 0f63ce4a..bc6444a1 100644 --- a/filesystem/ext4/common_test.go +++ b/filesystem/ext4/common_test.go @@ -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 { diff --git a/filesystem/ext4/ext4.go b/filesystem/ext4/ext4.go index c2efa8d6..9358a252 100644 --- a/filesystem/ext4/ext4.go +++ b/filesystem/ext4/ext4.go @@ -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 diff --git a/filesystem/ext4/groupdescriptors.go b/filesystem/ext4/groupdescriptors.go index 3d668133..458596d6 100644 --- a/filesystem/ext4/groupdescriptors.go +++ b/filesystem/ext4/groupdescriptors.go @@ -57,7 +57,7 @@ type groupDescriptor struct { inodeBitmapChecksum uint32 unusedInodes uint32 size uint16 - number uint16 + number uint32 } func (gd *groupDescriptor) equal(other *groupDescriptor) bool { @@ -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, @@ -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 diff --git a/filesystem/ext4/groupdescriptors_test.go b/filesystem/ext4/groupdescriptors_test.go index 27c09fb7..576deae5 100644 --- a/filesystem/ext4/groupdescriptors_test.go +++ b/filesystem/ext4/groupdescriptors_test.go @@ -1,6 +1,8 @@ package ext4 import ( + "bytes" + "encoding/binary" "fmt" "os" "testing" @@ -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) + } + }) + } +}