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
14 changes: 11 additions & 3 deletions bpf/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func NewVM(filter []Instruction) (*VM, error) {
switch ins := ins.(type) {
// Check for out-of-bounds jumps in instructions
case Jump:
if check <= int(ins.Skip) {
if uint64(check) <= uint64(ins.Skip) {
return nil, fmt.Errorf("cannot jump %d instructions; jumping past program bounds", ins.Skip)
}
case JumpIf:
Expand Down Expand Up @@ -132,9 +132,9 @@ func (v *VM) Run(in []byte) (int, error) {
case LoadScratch:
regA, regX = loadScratch(ins, regScratch, regA, regX)
case RetA:
return int(regA), nil
return returnValue(regA)
case RetConstant:
return int(ins.Val), nil
return returnValue(ins.Val)
case StoreScratch:
regScratch = storeScratch(ins, regScratch, regA, regX)
case TAX:
Expand All @@ -148,3 +148,11 @@ func (v *VM) Run(in []byte) (int, error) {

return 0, nil
}

func returnValue(value uint32) (int, error) {
n := int(value)
if n < 0 {
return 0, fmt.Errorf("return value %d overflows int", value)
}
return n, nil
}
9 changes: 6 additions & 3 deletions bpf/vm_instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,13 @@ func loadExtension(ins LoadExtension, in []byte) uint32 {
}

func loadIndirect(ins LoadIndirect, in []byte, regX uint32) (uint32, bool) {
offset := int(ins.Off) + int(regX)
offset := uint64(ins.Off) + uint64(regX)
if offset > uint64(len(in)) {
return 0, false
}
size := ins.Size

return loadCommon(in, offset, size)
return loadCommon(in, int(offset), size)
}

func loadMemShift(ins LoadMemShift, in []byte) (uint32, bool) {
Expand All @@ -139,7 +142,7 @@ func loadMemShift(ins LoadMemShift, in []byte) (uint32, bool) {
}

func inBounds(inLen int, offset int, size int) bool {
return offset+size <= inLen
return offset >= 0 && offset <= inLen && size >= 0 && size <= inLen-offset
}

func loadCommon(in []byte, offset int, size int) (uint32, bool) {
Expand Down
47 changes: 47 additions & 0 deletions bpf/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,53 @@ import (

var _ bpf.Instruction = unknown{}

func TestVMLargeJump(t *testing.T) {
if _, err := bpf.NewVM([]bpf.Instruction{bpf.Jump{Skip: 0xffffffff}, bpf.RetA{}}); err == nil {
t.Fatal("NewVM accepted a jump past the program bounds")
}
}

func TestVMLargeOffsets(t *testing.T) {
for _, load := range []bpf.Instruction{
bpf.LoadAbsolute{Off: 0x80000000, Size: 1},
bpf.LoadAbsolute{Off: 0x7fffffff, Size: 4},
bpf.LoadMemShift{Off: 0xffffffff},
bpf.LoadIndirect{Off: 0xffffffff, Size: 1},
} {
t.Run(fmt.Sprintf("%T/%v", load, load), func(t *testing.T) {
vm, err := bpf.NewVM([]bpf.Instruction{
bpf.LoadConstant{Dst: bpf.RegX, Val: 1}, load, bpf.RetConstant{Val: 1},
})
if err != nil {
t.Fatal(err)
}
if got, err := vm.Run([]byte{1, 2, 3, 4}); got != 0 || err != nil {
t.Errorf("Run = (%d, %v), want (0, nil)", got, err)
}
})
}
}

func TestVMLargeReturn(t *testing.T) {
for _, filter := range [][]bpf.Instruction{
{bpf.RetConstant{Val: 0xffffffff}},
{bpf.LoadConstant{Dst: bpf.RegA, Val: 0xffffffff}, bpf.RetA{}},
} {
vm, err := bpf.NewVM(filter)
if err != nil {
t.Fatal(err)
}
got, err := vm.Run(nil)
if uint64(^uint(0)>>1) < 0xffffffff {
if err == nil {
t.Errorf("Run = (%d, nil), want an overflow error", got)
}
} else if err != nil || uint64(got) != 0xffffffff {
t.Errorf("Run = (%d, %v), want (4294967295, nil)", got, err)
}
}
}

type unknown struct{}

func (unknown) Assemble() (bpf.RawInstruction, error) {
Expand Down