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
29 changes: 12 additions & 17 deletions filesystem/ext4/inode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
75 changes: 74 additions & 1 deletion filesystem/ext4/inode_test.go
Original file line number Diff line number Diff line change
@@ -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)|0o644)
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 {
Expand Down
Loading