Skip to content

Commit ebe6ae7

Browse files
authored
Stricter input legth checking in precompiles getValidator/fractionMulExps/transfer (#2038)
* fractionMulExp: stricter input length check See #615 * transfer: stricter input length checking See #615 * getValidator: stricter input length checking See #615 * Only change precompile checks after GFork
1 parent 865c73e commit ebe6ae7

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

core/vm/contracts.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ func (c *transfer) Run(input []byte, caller common.Address, evm *EVM) ([]byte, e
698698
// to: 32 bytes representing the address of the recipient
699699
// value: 32 bytes, a 256 bit integer representing the amount of Celo Gold to transfer
700700
// 3 arguments x 32 bytes each = 96 bytes total input
701-
if len(input) < 96 {
701+
if (evm.chainRules.IsGFork && len(input) != 96) || len(input) <= 96 {
702702
return nil, ErrInputLength
703703
}
704704

@@ -781,7 +781,7 @@ func (c *fractionMulExp) Run(input []byte, caller common.Address, evm *EVM) ([]b
781781
// decimals: 32 bytes, 256 bit integer, places of precision
782782
//
783783
// 6 args x 32 bytes each = 192 bytes total input length
784-
if len(input) < 192 {
784+
if (evm.chainRules.IsGFork && len(input) != 192) || len(input) < 192 {
785785
return nil, ErrInputLength
786786
}
787787

@@ -1015,7 +1015,7 @@ func (c *getValidator) Run(input []byte, caller common.Address, evm *EVM) ([]byt
10151015
// input is comprised of two arguments:
10161016
// index: 32 byte integer representing the index of the validator to get
10171017
// blockNumber: 32 byte integer representing the block number to access
1018-
if len(input) < 64 {
1018+
if (evm.chainRules.IsGFork && len(input) != 64) || len(input) < 64 {
10191019
return nil, ErrInputLength
10201020
}
10211021

core/vm/contracts_test.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,14 @@ func BenchmarkPrecompiledEd25519Verify(b *testing.B) { benchJSON("ed25519Verify"
499499

500500
// Tests sample inputs for fractionMulExp
501501
// NOTE: This currently only verifies that inputs of invalid length are rejected
502-
func TestPrecompiledFractionMulExp(t *testing.T) { testJSON("fractionMulExp", "fc", t) }
502+
func TestPrecompiledFractionMulExp(t *testing.T) {
503+
// Post GFork behaviour
504+
mockEVM.chainRules.IsGFork = true
505+
testJSON("fractionMulExp", "fc", t)
506+
// Pre GFork behaviour
507+
mockEVM.chainRules.IsGFork = false
508+
testJSON("fractionMulExpOld", "fc", t)
509+
}
503510

504511
// Tests sample inputs for proofOfPossession
505512
// NOTE: This currently only verifies that inputs of invalid length are rejected

core/vm/testdata/precompiles/fractionMulExp.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
},
77
{
88
"Input": "0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000150000000000000000000000000000000000000000000000000000000000000f100000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000400000000",
9-
"Expected": "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002710",
10-
"Name": "input_too_long"
9+
"Expected": "invalid input length",
10+
"Name": "input_too_long",
11+
"ErrorExpected": true
1112
},
1213
{
1314
"Input": "",
1415
"Expected": "invalid input length",
1516
"Name": "empty_input",
1617
"ErrorExpected": true
1718
}
18-
]
19+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"Input": "0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000150000000000000000000000000000000000000000000000000000000000000f100000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000400000000",
4+
"Expected": "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002710",
5+
"Name": "input_too_long"
6+
}
7+
]

0 commit comments

Comments
 (0)