Skip to content

Commit

Permalink
map: prevent BatchCursor reuse across maps
Browse files Browse the repository at this point in the history
Signed-off-by: Lorenz Bauer <lmb@isovalent.com>
  • Loading branch information
lmb committed Dec 1, 2023
1 parent ff804e5 commit e13d7d0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
27 changes: 20 additions & 7 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"io"
"math"
"math/rand"
"os"
"path/filepath"
Expand Down Expand Up @@ -1001,20 +1000,34 @@ func (m *Map) BatchLookupAndDelete(cursor *BatchCursor, keysOut, valuesOut inter

// BatchCursor represents a starting point for a batch operation.
type BatchCursor struct {
opaque []byte // len() is max(key_size, 4) to be on the safe side
m *Map
opaque []byte
}

func (m *Map) batchLookup(cmd sys.Cmd, cursor *BatchCursor, keysOut, valuesOut interface{}, opts *BatchOptions) (int, error) {
var inBatch []byte
if cursor.opaque == nil {
cursorLen := int(m.keySize)
if cursorLen < 4 {
// * generic_map_lookup_batch requires that batch_out is key_size bytes.
// This is used by array and LPM maps.
//
// * __htab_map_lookup_and_delete_batch requires u32. This is used by the
// various hash maps.
cursor.opaque = make([]byte, int(math.Max(float64(m.keySize), 4)))
} else {
inBatch = cursor.opaque
//
// Use a minimum of 4 bytes to avoid having to distinguish between the two.
cursorLen = 4
}

inBatch := cursor.opaque
if inBatch == nil {
// This is the first lookup, allocate a buffer to hold the cursor.
cursor.opaque = make([]byte, cursorLen)
cursor.m = m
} else if cursor.m != m {
// Prevent reuse of a cursor across maps. First, it's unlikely to work.
// Second, the maps may require different cursorLen and cursor.opaque
// may therefore be too short. This could lead to the kernel clobbering
// user space memory.
return 0, errors.New("a cursor may not be reused across maps")
}

if err := haveBatchAPI(); err != nil {
Expand Down
31 changes: 31 additions & 0 deletions map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,37 @@ func TestMapBatch(t *testing.T) {
}
}

func TestMapBatchCursorReuse(t *testing.T) {
spec := &MapSpec{
Type: Array,
KeySize: 4,
ValueSize: 4,
MaxEntries: 4,
}

arr1, err := NewMap(spec)
if err != nil {
t.Fatal(err)
}
defer arr1.Close()

arr2, err := NewMap(spec)
if err != nil {
t.Fatal(err)
}
defer arr2.Close()

tmp := make([]uint32, 2)

var cursor BatchCursor
_, err = arr1.BatchLookup(&cursor, tmp, tmp, nil)
testutils.SkipIfNotSupported(t, err)
qt.Assert(t, err, qt.IsNil)

_, err = arr2.BatchLookup(&cursor, tmp, tmp, nil)
qt.Assert(t, err, qt.IsNotNil)
}

func TestMapLookupKeyTooSmall(t *testing.T) {
m := createArray(t)
defer m.Close()
Expand Down

0 comments on commit e13d7d0

Please sign in to comment.