vm: limit POW, fix #2060

Calculating pow(pow(2, 255), 0xffffffff) takes unknown amount of time. See
also neo-project/neo-vm#422.
This commit is contained in:
Roman Khimov 2021-07-14 10:00:25 +03:00
parent 2b7abd20e7
commit 15be763bb3
2 changed files with 2 additions and 3 deletions

View file

@ -918,7 +918,7 @@ func (v *VM) execute(ctx *Context, op opcode.Opcode, parameter []byte) (err erro
case opcode.POW: case opcode.POW:
exp := v.estack.Pop().BigInt() exp := v.estack.Pop().BigInt()
a := v.estack.Pop().BigInt() a := v.estack.Pop().BigInt()
if ei := exp.Int64(); !exp.IsInt64() || ei > math.MaxInt32 || ei < 0 { if ei := exp.Int64(); !exp.IsInt64() || ei > maxSHLArg || ei < 0 {
panic("invalid exponent") panic("invalid exponent")
} }
v.estack.PushVal(new(big.Int).Exp(a, exp, nil)) v.estack.PushVal(new(big.Int).Exp(a, exp, nil))

View file

@ -6,7 +6,6 @@ import (
"encoding/hex" "encoding/hex"
"errors" "errors"
"fmt" "fmt"
"math"
"math/big" "math/big"
"math/rand" "math/rand"
"testing" "testing"
@ -719,7 +718,7 @@ func TestPOW(t *testing.T) {
t.Run("good, negative, odd", getTestFuncForVM(prog, -8, -2, 3)) t.Run("good, negative, odd", getTestFuncForVM(prog, -8, -2, 3))
t.Run("zero", getTestFuncForVM(prog, 1, 3, 0)) t.Run("zero", getTestFuncForVM(prog, 1, 3, 0))
t.Run("negative exponent", getTestFuncForVM(prog, nil, 3, -1)) t.Run("negative exponent", getTestFuncForVM(prog, nil, 3, -1))
t.Run("too big exponent", getTestFuncForVM(prog, nil, 1, math.MaxInt32+1)) t.Run("too big exponent", getTestFuncForVM(prog, nil, 1, maxSHLArg+1))
} }
func TestSQRT(t *testing.T) { func TestSQRT(t *testing.T) {