From 7721ec62e3362f9bfa01317d2f7958d4956172e8 Mon Sep 17 00:00:00 2001 From: Matthew Mattox Date: Mon, 16 Nov 2026 11:00:44 -0600 Subject: [PATCH] fix(snapshot): keep a descriptor's birth bytes when it has no birth flag Without FlagHasTargetBirth, decoding skipped bytes 26-27 and 32-35 and re-encoding wrote zeros there, so every rewrite of an older descriptor changed bytes it never read. Decoding now always reads them into TargetBirth, which BelongsTo uses only with the flag, so a descriptor re-encodes to the bytes it was read from. FuzzDecodeDescriptor now checks exactly that, byte for byte, in place of comparing decoded fields with the checksum ignored. Seed 8ac4626f92ad713e fails the new check without this fix. Co-Authored-By: Claude Opus 5.5 --- internal/snapshot/snapshot.go | 6 ++---- internal/snapshot/snapshot_fuzz_test.go | 14 ++++---------- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/internal/snapshot/snapshot.go b/internal/snapshot/snapshot.go index 690178c1..5919cb68 100644 --- a/internal/snapshot/snapshot.go +++ b/internal/snapshot/snapshot.go @@ -179,7 +179,7 @@ type Descriptor struct { State State // Current state Flags Flags // Snapshot flags DataInode uint32 // Hidden inode holding the captured extents - TargetBirth uint64 // Birth generation of the snapshotted file, 48 bits + TargetBirth uint64 // Birth generation of the snapshotted file, 48 bits; without FlagHasTargetBirth, the bytes an older version left there TotalBlocks uint64 // Number of blocks at snapshot time TotalSize uint64 // File size at snapshot time CreateTime int64 // Creation timestamp @@ -902,12 +902,10 @@ func decodeDescriptor(sectorData []byte, descIdx uint64) (*Descriptor, error) { CreateTime: int64(binary.LittleEndian.Uint64(data[52:60])), DeleteTime: int64(binary.LittleEndian.Uint64(data[60:68])), RefCountJobID: binary.LittleEndian.Uint64(data[68:76]), + TargetBirth: uint64(binary.LittleEndian.Uint16(data[26:28]))<<32 | uint64(binary.LittleEndian.Uint32(data[32:36])), Checksum: stored, } copy(desc.Name[:], data[76:124]) - if desc.Flags&FlagHasTargetBirth != 0 { - desc.TargetBirth = uint64(binary.LittleEndian.Uint16(data[26:28]))<<32 | uint64(binary.LittleEndian.Uint32(data[32:36])) - } return desc, nil } diff --git a/internal/snapshot/snapshot_fuzz_test.go b/internal/snapshot/snapshot_fuzz_test.go index 9ab2a7e6..f88152c6 100644 --- a/internal/snapshot/snapshot_fuzz_test.go +++ b/internal/snapshot/snapshot_fuzz_test.go @@ -60,21 +60,15 @@ func FuzzDecodeDescriptor(f *testing.F) { func checkDescriptorDecode(t *testing.T, sector []byte, idx uint64) { desc, err := decodeDescriptor(sector, idx) - if err != nil { + if err != nil || desc.Magic == 0 { return } _ = desc.GetName() out := make([]byte, BytesPerSector) encodeDescriptor(out, 0, desc) - again, err := decodeDescriptor(out, 0) - if err != nil { - t.Fatalf("decoding a re-encoded descriptor: %v", err) - } - // Bytes only some formats use, such as the birth generation without - // FlagHasTargetBirth, re-encode as zero and change the checksum. - again.Checksum = desc.Checksum - if *again != *desc { - t.Fatalf("descriptor changed after an encode/decode round trip: %+v != %+v", again, desc) + in := sector[idx*DescriptorSize : (idx+1)*DescriptorSize] + if !bytes.Equal(out[:DescriptorSize], in) { + t.Fatalf("re-encoding a descriptor changed its bytes:\nread %x\nwrote %x", in, out[:DescriptorSize]) } }