#224·goleveldb

panic in db.NewIterator when Limit > Start

Author: divanCreated Jun 25, 2018Updated Nov 2, 2023
Labelsbug

There is a possible case where the invalid input to (*DB).NewIterator() causes a panic: runtime error: slice bounds out of range.

Here is a test that reproduces the issue – it simulates two table files with

table_test.go

go
package leveldb

import (
	"encoding/binary"
	"testing"

	"github.com/syndtr/goleveldb/leveldb/comparer"
	"github.com/syndtr/goleveldb/leveldb/storage"
	"github.com/syndtr/goleveldb/leveldb/util"
)

func TestIndexIterator(t *testing.T) {
	// just some dummy values not relevant for test
	fd := storage.FileDesc{
		Type: storage.TypeTable,
		Num:  1,
	}

	// simulate two table files with imin and imax fields
	min1, max1 := tsToBytes(1500000000), tsToBytes(1600000000)
	min2, max2 := tsToBytes(1200000000), tsToBytes(1300000000)
	var tf tFiles = []*tFile{
		newTableFile(fd, 1024, min1, max1),
		newTableFile(fd, 1024, min2, max2),
	}

	lower, upper := uint32(1500000001), uint32(1000000000) // <-- 1000000000 is lower than any imin and lower than 'lower'
	slice := &util.Range{Start: tsToBytes(lower), Limit: tsToBytes(upper)}

	// use default comparer which is bytesComparer
	icmp := &iComparer{comparer.DefaultComparer}

	// panic
	tf.newIndexIterator(nil, icmp, slice, nil)
}

func tsToBytes(ts uint32) []byte {
	var zero [32]byte
	ret := make([]byte, 32+4)
	binary.BigEndian.PutUint32(ret, ts)
	copy(ret[4:], zero[:])
	return ret
}

Result:

$ go test -test.run IndexIterator .
--- FAIL: TestIndexIterator (0.00s)
panic: runtime error: slice bounds out of range [recovered]
	panic: runtime error: slice bounds out of range

goroutine 6 [running]:
testing.tRunner.func1(0xc4201622d0)
	/usr/local/go/src/testing/testing.go:742 +0x29d
panic(0x1427a00, 0x17a28f0)
	/usr/local/go/src/runtime/panic.go:505 +0x229
github.com/syndtr/goleveldb/leveldb.tFiles.newIndexIterator(0xc420057540, 0x2, 0x2, 0x0, 0xc420057550, 0xc42001f5c0, 0x0, 0x1, 0x10e6bfd)
	/Users/divan/src/github.com/syndtr/goleveldb/leveldb/table.go:241 +0x310
github.com/syndtr/goleveldb/leveldb.TestIndexIterator(0xc4201622d0)
	/Users/divan/src/github.com/syndtr/goleveldb/leveldb/table_test.go:34 +0x68f
testing.tRunner(0xc4201622d0, 0x14c73f0)
	/usr/local/go/src/testing/testing.go:777 +0xd0
created by testing.(*T).Run
	/usr/local/go/src/testing/testing.go:824 +0x2e0
FAIL	github.com/syndtr/goleveldb/leveldb	0.023s

I'm not sure what's the policy regarding input data, and there is a clear workaround just to validate Start and Limit before creating iterator, but we've caught this bug in production and it's not easy to reproduce on the real end-to-end tests.

The code in question is in table.go, line 231:

go
// Creates iterator index from tables.
func (tf tFiles) newIndexIterator(tops *tOps, icmp *iComparer, slice *util.Range, ro *opt.ReadOptions) iterator.IteratorIndexer {
	if slice != nil {
		var start, limit int
		if slice.Start != nil {
			start = tf.searchMax(icmp, internalKey(slice.Start))
		}
		if slice.Limit != nil {
			limit = tf.searchMin(icmp, internalKey(slice.Limit))
		} else {
			limit = tf.Len()
		}
		tf = tf[start:limit]
	}
...

In the scenario reproduced in test, limit is 0, while start is 2, and that's a clearly an invalid indexes, and Go runtime panics. This piece of code looks like it attempts to validate indexes, and seems like this is an edge case that is not covered by this validation.

What would be the solution for this problem? Should we improve validation here?