Skip to content

Commit 84d8eb2

Browse files
authored
cmd/evm: add 256-bit field validations on transactions (t9n) (#23743)
* cmd/evm: add 256-bit field validations on transactions (t9n) * cmd/evm: validate gas*gasPrice, return intrinsic gas usage * cmd/evm: address review comment
1 parent 554b1b9 commit 84d8eb2

File tree

7 files changed

+121
-13
lines changed

7 files changed

+121
-13
lines changed

cmd/evm/internal/t8ntool/transaction.go

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,19 @@ import (
3636
)
3737

3838
type result struct {
39-
Error error
40-
Address common.Address
41-
Hash common.Hash
39+
Error error
40+
Address common.Address
41+
Hash common.Hash
42+
IntrinsicGas uint64
4243
}
4344

4445
// MarshalJSON marshals as JSON with a hash.
4546
func (r *result) MarshalJSON() ([]byte, error) {
4647
type xx struct {
47-
Error string `json:"error,omitempty"`
48-
Address *common.Address `json:"address,omitempty"`
49-
Hash *common.Hash `json:"hash,omitempty"`
48+
Error string `json:"error,omitempty"`
49+
Address *common.Address `json:"address,omitempty"`
50+
Hash *common.Hash `json:"hash,omitempty"`
51+
IntrinsicGas uint64 `json:"intrinsicGas,omitempty"`
5052
}
5153
var out xx
5254
if r.Error != nil {
@@ -58,6 +60,7 @@ func (r *result) MarshalJSON() ([]byte, error) {
5860
if r.Hash != (common.Hash{}) {
5961
out.Hash = &r.Hash
6062
}
63+
out.IntrinsicGas = r.IntrinsicGas
6164
return json.Marshal(out)
6265
}
6366

@@ -132,12 +135,36 @@ func Transaction(ctx *cli.Context) error {
132135
} else {
133136
r.Address = sender
134137
}
135-
138+
// Check intrinsic gas
136139
if gas, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.To() == nil,
137140
chainConfig.IsHomestead(new(big.Int)), chainConfig.IsIstanbul(new(big.Int))); err != nil {
138141
r.Error = err
139-
} else if tx.Gas() < gas {
140-
r.Error = fmt.Errorf("%w: have %d, want %d", core.ErrIntrinsicGas, tx.Gas(), gas)
142+
results = append(results, r)
143+
continue
144+
} else {
145+
r.IntrinsicGas = gas
146+
if tx.Gas() < gas {
147+
r.Error = fmt.Errorf("%w: have %d, want %d", core.ErrIntrinsicGas, tx.Gas(), gas)
148+
results = append(results, r)
149+
continue
150+
}
151+
}
152+
// Validate <256bit fields
153+
switch {
154+
case tx.Value().BitLen() > 256:
155+
r.Error = errors.New("value exceeds 256 bits")
156+
case tx.GasPrice().BitLen() > 256:
157+
r.Error = errors.New("gasPrice exceeds 256 bits")
158+
case tx.GasTipCap().BitLen() > 256:
159+
r.Error = errors.New("maxPriorityFeePerGas exceeds 256 bits")
160+
case tx.GasFeeCap().BitLen() > 256:
161+
r.Error = errors.New("maxFeePerGas exceeds 256 bits")
162+
case tx.GasFeeCap().Cmp(tx.GasTipCap()) < 0:
163+
r.Error = errors.New("maxFeePerGas < maxPriorityFeePerGas")
164+
case new(big.Int).Mul(tx.GasPrice(), new(big.Int).SetUint64(tx.Gas())).BitLen() > 256:
165+
r.Error = errors.New("gas * gasPrice exceeds 256 bits")
166+
case new(big.Int).Mul(tx.GasFeeCap(), new(big.Int).SetUint64(tx.Gas())).BitLen() > 256:
167+
r.Error = errors.New("gas * maxFeePerGas exceeds 256 bits")
141168
}
142169
results = append(results, r)
143170
}

cmd/evm/t8n_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,14 @@ func TestT9n(t *testing.T) {
257257
},
258258
expOut: "exp.json",
259259
},
260+
{ // Transactions with value exceeding 256 bits
261+
base: "./testdata/17",
262+
input: t9nInput{
263+
inTxs: "signed_txs.rlp",
264+
stFork: "London",
265+
},
266+
expOut: "exp.json",
267+
},
260268
} {
261269

262270
args := []string{"t9n"}

cmd/evm/testdata/15/exp2.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
[
22
{
33
"address": "0xd02d72e067e77158444ef2020ff2d325f929b363",
4-
"hash": "0xa98a24882ea90916c6a86da650fbc6b14238e46f0af04a131ce92be897507476"
4+
"hash": "0xa98a24882ea90916c6a86da650fbc6b14238e46f0af04a131ce92be897507476",
5+
"intrinsicGas": 21000
56
},
67
{
78
"address": "0xd02d72e067e77158444ef2020ff2d325f929b363",
8-
"hash": "0x36bad80acce7040c45fd32764b5c2b2d2e6f778669fb41791f73f546d56e739a"
9+
"hash": "0x36bad80acce7040c45fd32764b5c2b2d2e6f778669fb41791f73f546d56e739a",
10+
"intrinsicGas": 21000
911
}
1012
]

cmd/evm/testdata/16/exp.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
[
22
{
33
"address": "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b",
4-
"hash": "0x7cc3d1a8540a44736750f03bb4d85c0113be4b3472a71bf82241a3b261b479e6"
4+
"hash": "0x7cc3d1a8540a44736750f03bb4d85c0113be4b3472a71bf82241a3b261b479e6",
5+
"intrinsicGas": 21000
56
},
67
{
78
"error": "intrinsic gas too low: have 82, want 21000",
89
"address": "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b",
9-
"hash": "0x3b2d2609e4361562edb9169314f4c05afc6dbf5d706bf9dda5abe242ab76a22b"
10+
"hash": "0x3b2d2609e4361562edb9169314f4c05afc6dbf5d706bf9dda5abe242ab76a22b",
11+
"intrinsicGas": 21000
1012
}
1113
]

cmd/evm/testdata/17/exp.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[
2+
{
3+
"error": "value exceeds 256 bits",
4+
"address": "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b",
5+
"hash": "0xfbd91685dcbf8172f0e8c53e2ddbb4d26707840da6b51a74371f62a33868fd82",
6+
"intrinsicGas": 21000
7+
},
8+
{
9+
"error": "gasPrice exceeds 256 bits",
10+
"address": "0x1b57ccef1fe5fb73f1e64530fb4ebd9cf1655964",
11+
"hash": "0x45dc05035cada83748e4c1fe617220106b331eca054f44c2304d5654a9fb29d5",
12+
"intrinsicGas": 21000
13+
},
14+
{
15+
"error": "invalid transaction v, r, s values",
16+
"hash": "0xf06691c2a803ab7f3c81d06a0c0a896f80f311105c599fc59a9fdbc669356d35"
17+
},
18+
{
19+
"error": "invalid transaction v, r, s values",
20+
"hash": "0x84703b697ad5b0db25e4f1f98fb6b1adce85b9edb2232eeba9cedd8c6601694b"
21+
}
22+
]

cmd/evm/testdata/17/rlpdata.txt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
[
2+
[
3+
"",
4+
"d",
5+
5208,
6+
d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0,
7+
010000000000000000000000000000000000000000000000000000000000000001,
8+
"",
9+
1b,
10+
c16787a8e25e941d67691954642876c08f00996163ae7dfadbbfd6cd436f549d,
11+
6180e5626cae31590f40641fe8f63734316c4bfeb4cdfab6714198c1044d2e28,
12+
],
13+
[
14+
"",
15+
010000000000000000000000000000000000000000000000000000000000000001,
16+
5208,
17+
d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0,
18+
11,
19+
"",
20+
1b,
21+
c16787a8e25e941d67691954642876c08f00996163ae7dfadbbfd6cd436f549d,
22+
6180e5626cae31590f40641fe8f63734316c4bfeb4cdfab6714198c1044d2e28,
23+
],
24+
[
25+
"",
26+
11,
27+
5208,
28+
d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0,
29+
11,
30+
"",
31+
1b,
32+
c16787a8e25e941d67691954642876c08f00996163ae7dfadbbfd6cd436f549daa,
33+
6180e5626cae31590f40641fe8f63734316c4bfeb4cdfab6714198c1044d2e28,
34+
],
35+
[
36+
"",
37+
11,
38+
5208,
39+
d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0,
40+
11,
41+
"",
42+
1b,
43+
c16787a8e25e941d67691954642876c08f00996163ae7dfadbbfd6cd436f549d,
44+
6180e5626cae31590f40641fe8f63734316c4bfeb4cdfab6714198c1044d2e28bb,
45+
],
46+
]

cmd/evm/testdata/17/signed_txs.rlp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"0xf901c8f880806482520894d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0a1010000000000000000000000000000000000000000000000000000000000000001801ba0c16787a8e25e941d67691954642876c08f00996163ae7dfadbbfd6cd436f549da06180e5626cae31590f40641fe8f63734316c4bfeb4cdfab6714198c1044d2e28f88080a101000000000000000000000000000000000000000000000000000000000000000182520894d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d011801ba0c16787a8e25e941d67691954642876c08f00996163ae7dfadbbfd6cd436f549da06180e5626cae31590f40641fe8f63734316c4bfeb4cdfab6714198c1044d2e28f860801182520894d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d011801ba1c16787a8e25e941d67691954642876c08f00996163ae7dfadbbfd6cd436f549daaa06180e5626cae31590f40641fe8f63734316c4bfeb4cdfab6714198c1044d2e28f860801182520894d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d011801ba0c16787a8e25e941d67691954642876c08f00996163ae7dfadbbfd6cd436f549da16180e5626cae31590f40641fe8f63734316c4bfeb4cdfab6714198c1044d2e28bb"

0 commit comments

Comments
 (0)