Skip to content

Commit 52a4392

Browse files
authored
Handle potential overflow in getData (#13890)
Fixes erigontech/security#1
1 parent ac0de74 commit 52a4392

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

core/vm/common.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ func getData(data []byte, start uint64, size uint64) []byte {
6060
if start > length {
6161
start = length
6262
}
63-
end := start + size
64-
if end > length {
63+
end, overflow := math.SafeAdd(start, size)
64+
if end > length || overflow {
6565
end = length
6666
}
6767
return common.RightPadBytes(data[start:end], int(size))

core/vm/contracts_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ import (
2727
"testing"
2828
"time"
2929

30+
"github.com/stretchr/testify/assert"
31+
3032
libcommon "github.com/erigontech/erigon-lib/common"
33+
"github.com/erigontech/erigon-lib/common/hexutil"
34+
"github.com/erigontech/erigon-lib/common/math"
3135
)
3236

3337
// precompiledTest defines the input/output pairs for precompiled contract tests.
@@ -260,6 +264,15 @@ func TestPrecompiledModExpOOG(t *testing.T) {
260264
}
261265
}
262266

267+
func TestModExpPrecompilePotentialOutOfRange(t *testing.T) {
268+
modExpContract := PrecompiledContractsCancun[libcommon.BytesToAddress([]byte{0x05})]
269+
hexString := "0x0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000ffffffffffffffff0000000000000000000000000000000000000000000000000000000000000000ee"
270+
input := hexutil.MustDecode(hexString)
271+
maxGas := uint64(math.MaxUint64)
272+
_, _, err := RunPrecompiledContract(modExpContract, input, maxGas)
273+
assert.NoError(t, err)
274+
}
275+
263276
// Tests the sample inputs from the elliptic curve scalar multiplication EIP 213.
264277
func TestPrecompiledBn256ScalarMul(t *testing.T) { testJson("bn256ScalarMul", "07", t) }
265278
func BenchmarkPrecompiledBn256ScalarMul(b *testing.B) { benchJson("bn256ScalarMul", "07", b) }

0 commit comments

Comments
 (0)