Skip to content
Open
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
19 changes: 15 additions & 4 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,12 +597,15 @@ func (t *Table) block(idx int, useCache bool) (*Block, error) {
}

// Read meta data related to block.
if len(blk.data) < 4 {
return nil, errors.New("invalid block size: data is too small to contain checksum length")
}
readPos := len(blk.data) - 4 // First read checksum length.
blk.chkLen = int(y.BytesToU32(blk.data[readPos : readPos+4]))

// Checksum length greater than block size could happen if the table was compressed and
// Checksum length greater than remaining block size could happen if the table was compressed and
// it was opened with an incorrect compression algorithm (or the data was corrupted).
if blk.chkLen > len(blk.data) {
if blk.chkLen < 0 || blk.chkLen > readPos {
return nil, errors.New("invalid checksum length. Either the data is " +
"corrupted or the table options are incorrectly set")
}
Expand All @@ -611,10 +614,18 @@ func (t *Table) block(idx int, useCache bool) (*Block, error) {
readPos -= blk.chkLen
blk.checksum = blk.data[readPos : readPos+blk.chkLen]
// Move back and read numEntries in the block.
if readPos < 4 {
return nil, errors.New("invalid block size: data is too small to contain numEntries")
}
readPos -= 4
numEntries := int(y.BytesToU32(blk.data[readPos : readPos+4]))
entriesIndexStart := readPos - (numEntries * 4)
entriesIndexEnd := entriesIndexStart + numEntries*4
entriesSize := numEntries * 4
if numEntries < 0 || entriesSize/4 != numEntries || readPos < entriesSize {
return nil, errors.New("invalid number of entries in block. Either the data is " +
"corrupted or the table options are incorrectly set")
}
entriesIndexStart := readPos - entriesSize
entriesIndexEnd := readPos

blk.entryOffsets = y.BytesToU32Slice(blk.data[entriesIndexStart:entriesIndexEnd])

Expand Down
51 changes: 51 additions & 0 deletions table/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/cespare/xxhash/v2"
"github.com/stretchr/testify/require"

"github.com/dgraph-io/badger/v4/fb"
"github.com/dgraph-io/badger/v4/options"
"github.com/dgraph-io/badger/v4/y"
"github.com/dgraph-io/ristretto/v2"
Expand Down Expand Up @@ -933,3 +934,53 @@ func TestInMemoryTableFilename(t *testing.T) {
}
require.Equal(t, IDToFilename(777), tbl.Filename())
}

func TestTableBlockCorruptedBoundsNoPanic(t *testing.T) {
testCases := []struct {
name string
corrupt func(data []byte, blkOffset, blkLen int)
}{
{
name: "chkLen exceeds readPos but within blkLen",
corrupt: func(data []byte, blkOffset, blkLen int) {
// Set chkLen = blkLen - 2 so readPos (blkLen - 4) - chkLen < 0
copy(data[blkOffset+blkLen-4:blkOffset+blkLen], y.U32ToBytes(uint32(blkLen-2)))
},
},
{
name: "numEntries exceeds remaining readPos",
corrupt: func(data []byte, blkOffset, blkLen int) {
// Set chkLen = 0
copy(data[blkOffset+blkLen-4:blkOffset+blkLen], y.U32ToBytes(0))
// Set numEntries = 0x3FFFFFFF so entriesIndexStart < 0
copy(data[blkOffset+blkLen-8:blkOffset+blkLen-4], y.U32ToBytes(0x3FFFFFFF))
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
opts := getTestTableOptions()
opts.Compression = options.None
src := buildTestTable(t, "key", 500, opts)

data := make([]byte, len(src.Data))
copy(data, src.Data)
srcID := src.ID()
require.NoError(t, src.DecrRef())

tbl, err := OpenInMemoryTable(data, srcID, &opts)
require.NoError(t, err)
defer func() { require.NoError(t, tbl.DecrRef()) }()

var ko fb.BlockOffset
require.True(t, tbl.offsets(&ko, 0))
tc.corrupt(data, int(ko.Offset()), int(ko.Len()))

require.NotPanics(t, func() {
_, err = tbl.block(0, false)
})
require.Error(t, err)
})
}
}