feat: restrict modexp input length (#580)

* feat: restrict `modexp` inputs length

* mv core/vm/testdata/precompiles/modexp.json core/vm/testdata/precompiles/fail-modexp.json

* mv core/vm/testdata/precompiles/modexp_eip2565.json core/vm/testdata/precompiles/fail-modexp_eip2565.json
This commit is contained in:
HAOYUatHZ 2023-11-24 13:50:20 +08:00 committed by GitHub
parent 9fc7318213
commit 563e36e542
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 4 deletions

View file

@ -36,6 +36,7 @@ import (
var (
errPrecompileDisabled = errors.New("sha256, ripemd160, blake2f precompiles temporarily disabled")
errModexpUnsupportedInput = errors.New("modexp temporarily only accepts inputs of 32 bytes (256 bits) or less")
)
// PrecompiledContract is the basic interface for native Go contracts. The implementation
@ -427,9 +428,19 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
func (c *bigModExp) Run(input []byte) ([]byte, error) {
var (
baseLen = new(big.Int).SetBytes(getData(input, 0, 32)).Uint64()
expLen = new(big.Int).SetBytes(getData(input, 32, 32)).Uint64()
modLen = new(big.Int).SetBytes(getData(input, 64, 32)).Uint64()
baseLenBigInt = new(big.Int).SetBytes(getData(input, 0, 32))
expLenBigInt = new(big.Int).SetBytes(getData(input, 32, 32))
modLenBigInt = new(big.Int).SetBytes(getData(input, 64, 32))
)
// Check that all inputs are `u256` (32 - bytes) or less, revert otherwise
var lenLimit = new(big.Int).SetInt64(32)
if baseLenBigInt.Cmp(lenLimit) > 0 || expLenBigInt.Cmp(lenLimit) > 0 || modLenBigInt.Cmp(lenLimit) > 0 {
return nil, errModexpUnsupportedInput
}
var (
baseLen = baseLenBigInt.Uint64()
expLen = expLenBigInt.Uint64()
modLen = modLenBigInt.Uint64()
)
if len(input) > 96 {
input = input[96:]