From e47771e0b005fcfc977d4070b4e8a24ebc142cd8 Mon Sep 17 00:00:00 2001 From: Laura Date: Sun, 22 Nov 2026 00:03:15 +0100 Subject: [PATCH 1/2] fix(ext4): only validate inode checksums when metadata_csum is enabled --- filesystem/ext4/inode.go | 29 ++++++-------- filesystem/ext4/inode_test.go | 75 ++++++++++++++++++++++++++++++++++- 2 files changed, 86 insertions(+), 18 deletions(-) diff --git a/filesystem/ext4/inode.go b/filesystem/ext4/inode.go index 21cc17e5..addc04e6 100644 --- a/filesystem/ext4/inode.go +++ b/filesystem/ext4/inode.go @@ -232,18 +232,6 @@ func inodeFromBytes(b []byte, sb *superblock, number uint32) (*inode, error) { // only work with the amount of data that is the size of the inode, even if more was passed in b = b[:sb.inodeSize] - // checksum before using the data - checksumBytes := make([]byte, 4) - - // checksum before using the data - copy(checksumBytes[0:2], b[0x7c:0x7e]) - copy(checksumBytes[2:4], b[0x82:0x84]) - // zero out checksum fields before calculating the checksum - b[0x7c] = 0 - b[0x7d] = 0 - b[0x82] = 0 - b[0x83] = 0 - // block count, reserved block count and free blocks depends on whether the fs is 64-bit or not owner := make([]byte, 4) fileSize := make([]byte, 8) @@ -386,11 +374,18 @@ func inodeFromBytes(b []byte, sb *superblock, number uint32) (*inode, error) { blockPointers: blockPointers, linkTarget: linkTarget, } - checksum := binary.LittleEndian.Uint32(checksumBytes) - actualChecksum := inodeChecksum(b, sb.checksumSeed, number, i.nfsFileVersion) - - if actualChecksum != checksum { - return nil, fmt.Errorf("checksum mismatch, on-disk %x vs calculated %x", checksum, actualChecksum) + // Linux does not maintain inode checksums when metadata_csum is disabled. + if sb.features.metadataChecksums { + checksum := uint32(binary.LittleEndian.Uint16(b[0x7c:0x7e])) | uint32(binary.LittleEndian.Uint16(b[0x82:0x84]))<<16 + // Zero out checksum fields before calculating the checksum. + b[0x7c] = 0 + b[0x7d] = 0 + b[0x82] = 0 + b[0x83] = 0 + actualChecksum := inodeChecksum(b, sb.checksumSeed, number, i.nfsFileVersion) + if actualChecksum != checksum { + return nil, fmt.Errorf("checksum mismatch, on-disk %x vs calculated %x", checksum, actualChecksum) + } } return &i, nil diff --git a/filesystem/ext4/inode_test.go b/filesystem/ext4/inode_test.go index 4a88d5d7..eb5dd558 100644 --- a/filesystem/ext4/inode_test.go +++ b/filesystem/ext4/inode_test.go @@ -1,6 +1,79 @@ package ext4 -import "testing" +import ( + "encoding/binary" + "hash/crc32" + "strings" + "testing" +) + +func TestInodeFromBytesChecksum(t *testing.T) { + tests := []struct { + name string + metadataChecksums bool + storedChecksum bool + modified bool + wantErr bool + }{ + {name: "disabled with zero checksum"}, + {name: "disabled with stale checksum", storedChecksum: true, modified: true}, + {name: "enabled with valid checksum", metadataChecksums: true, storedChecksum: true}, + {name: "enabled with zero checksum", metadataChecksums: true, wantErr: true}, + {name: "enabled with stale checksum", metadataChecksums: true, storedChecksum: true, modified: true, wantErr: true}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + sb := &superblock{ + inodeSize: 256, + checksumSeed: 0x12345678, + features: featureFlags{ + metadataChecksums: tt.metadataChecksums, + }, + } + data := make([]byte, sb.inodeSize) + binary.LittleEndian.PutUint16(data[0x00:0x02], uint16(fileTypeRegularFile)|0644) + binary.LittleEndian.PutUint32(data[0x04:0x08], 42) + binary.LittleEndian.PutUint32(data[0x64:0x68], 17) + binary.LittleEndian.PutUint16(data[0x80:0x82], 32) + + if tt.storedChecksum { + var prefix [8]byte + binary.LittleEndian.PutUint32(prefix[:4], 12) + binary.LittleEndian.PutUint32(prefix[4:], 17) + // ext4 uses CRC32C without the standard library's initial/final complement. + table := crc32.MakeTable(crc32.Castagnoli) + checksum := crc32.Update(^sb.checksumSeed, table, prefix[:]) + checksum = ^crc32.Update(checksum, table, data) + binary.LittleEndian.PutUint16(data[0x7c:0x7e], uint16(checksum)) + binary.LittleEndian.PutUint16(data[0x82:0x84], uint16(checksum>>16)) + } + + // Linux updates inode fields without maintaining checksums when metadata_csum is disabled. + if tt.modified { + binary.LittleEndian.PutUint32(data[0x04:0x08], 43) + } + + in, err := inodeFromBytes(data, sb, 12) + if tt.wantErr { + if err == nil || !strings.Contains(err.Error(), "checksum mismatch") { + t.Fatalf("expected checksum mismatch, got %v", err) + } + return + } + if err != nil { + t.Fatalf("inodeFromBytes: %v", err) + } + wantSize := uint64(42) + if tt.modified { + wantSize = 43 + } + if in.size != wantSize { + t.Errorf("size = %d, want %d", in.size, wantSize) + } + }) + } +} func TestInodeDeviceNumber(t *testing.T) { tests := []struct { From 8228daed5b1946bb78daca69ac4c39baa736cfae Mon Sep 17 00:00:00 2001 From: Laura Date: Sun, 22 Nov 2026 18:46:02 +0100 Subject: [PATCH 2/2] fix(ext4): use new octal literal style --- filesystem/ext4/inode_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/filesystem/ext4/inode_test.go b/filesystem/ext4/inode_test.go index eb5dd558..d057aa12 100644 --- a/filesystem/ext4/inode_test.go +++ b/filesystem/ext4/inode_test.go @@ -32,7 +32,7 @@ func TestInodeFromBytesChecksum(t *testing.T) { }, } data := make([]byte, sb.inodeSize) - binary.LittleEndian.PutUint16(data[0x00:0x02], uint16(fileTypeRegularFile)|0644) + binary.LittleEndian.PutUint16(data[0x00:0x02], uint16(fileTypeRegularFile)|0o644) binary.LittleEndian.PutUint32(data[0x04:0x08], 42) binary.LittleEndian.PutUint32(data[0x64:0x68], 17) binary.LittleEndian.PutUint16(data[0x80:0x82], 32)