Skip to content

Commit a9a19c4

Browse files
jwasingerfjl
andauthored
core/vm: fix EIP-7823 modexp input length check (ethereum#32363)
The order of the checks was wrong which would have allowed a call to modexp with `baseLen == 0 && modLen == 0` post fusaka. Also handles an edge case where base/mod/exp length >= 2**64 --------- Co-authored-by: Felix Lange <[email protected]>
1 parent 1693a48 commit a9a19c4

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

core/vm/contracts.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -501,23 +501,28 @@ func (c *bigModExp) RequiredGas(input []byte) uint64 {
501501

502502
func (c *bigModExp) Run(input []byte) ([]byte, error) {
503503
var (
504-
baseLen = new(big.Int).SetBytes(getData(input, 0, 32)).Uint64()
505-
expLen = new(big.Int).SetBytes(getData(input, 32, 32)).Uint64()
506-
modLen = new(big.Int).SetBytes(getData(input, 64, 32)).Uint64()
504+
baseLenBig = new(big.Int).SetBytes(getData(input, 0, 32))
505+
expLenBig = new(big.Int).SetBytes(getData(input, 32, 32))
506+
modLenBig = new(big.Int).SetBytes(getData(input, 64, 32))
507+
baseLen = baseLenBig.Uint64()
508+
expLen = expLenBig.Uint64()
509+
modLen = modLenBig.Uint64()
510+
inputLenOverflow = max(baseLenBig.BitLen(), expLenBig.BitLen(), modLenBig.BitLen()) > 64
507511
)
508512
if len(input) > 96 {
509513
input = input[96:]
510514
} else {
511515
input = input[:0]
512516
}
517+
518+
// enforce size cap for inputs
519+
if c.eip7823 && (inputLenOverflow || max(baseLen, expLen, modLen) > 1024) {
520+
return nil, errors.New("one or more of base/exponent/modulus length exceeded 1024 bytes")
521+
}
513522
// Handle a special case when both the base and mod length is zero
514523
if baseLen == 0 && modLen == 0 {
515524
return []byte{}, nil
516525
}
517-
// enforce size cap for inputs
518-
if c.eip7823 && max(baseLen, expLen, modLen) > 1024 {
519-
return nil, errors.New("one or more of base/exponent/modulus length exceeded 1024 bytes")
520-
}
521526
// Retrieve the operands and execute the exponentiation
522527
var (
523528
base = new(big.Int).SetBytes(getData(input, 0, baseLen))

0 commit comments

Comments
 (0)