From 64da037e99aac013dec62111f7254d8b84ca66f8 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Mon, 11 Oct 2021 12:30:13 +0200 Subject: [PATCH] cmd/evm: stricter transaction validation (#23694) * cmd/evm: t9n: validate transaction intrinsic gas * cmd/evm: t9n: stricter tx validation --- cmd/evm/internal/t8ntool/transaction.go | 19 +++++++++++--- cmd/evm/t8n_test.go | 10 +++++++- cmd/evm/testdata/15/exp.json | 8 +++--- cmd/evm/testdata/16/exp.json | 11 ++++++++ cmd/evm/testdata/16/signed_txs.rlp | 1 + cmd/evm/testdata/16/unsigned_txs.json | 34 +++++++++++++++++++++++++ 6 files changed, 75 insertions(+), 8 deletions(-) create mode 100644 cmd/evm/testdata/16/exp.json create mode 100644 cmd/evm/testdata/16/signed_txs.rlp create mode 100644 cmd/evm/testdata/16/unsigned_txs.json diff --git a/cmd/evm/internal/t8ntool/transaction.go b/cmd/evm/internal/t8ntool/transaction.go index aecbad79d..b89ca9be9 100644 --- a/cmd/evm/internal/t8ntool/transaction.go +++ b/cmd/evm/internal/t8ntool/transaction.go @@ -26,6 +26,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/params" @@ -123,12 +124,22 @@ func Transaction(ctx *cli.Context) error { results = append(results, result{Error: err}) continue } - sender, err := types.Sender(signer, &tx) - if err != nil { - results = append(results, result{Error: err}) + r := result{Hash: tx.Hash()} + if sender, err := types.Sender(signer, &tx); err != nil { + r.Error = err + results = append(results, r) continue + } else { + r.Address = sender } - results = append(results, result{Address: sender, Hash: tx.Hash()}) + + if gas, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.To() == nil, + chainConfig.IsHomestead(new(big.Int)), chainConfig.IsIstanbul(new(big.Int))); err != nil { + r.Error = err + } else if tx.Gas() < gas { + r.Error = fmt.Errorf("%w: have %d, want %d", core.ErrIntrinsicGas, tx.Gas(), gas) + } + results = append(results, r) } out, err := json.MarshalIndent(results, "", " ") fmt.Println(string(out)) diff --git a/cmd/evm/t8n_test.go b/cmd/evm/t8n_test.go index ad518a91a..325e0fac6 100644 --- a/cmd/evm/t8n_test.go +++ b/cmd/evm/t8n_test.go @@ -233,7 +233,7 @@ func TestT9n(t *testing.T) { }, expOut: "exp.json", }, - { // London txs on homestead + { // London txs on London base: "./testdata/15", input: t9nInput{ inTxs: "signed_txs.rlp", @@ -249,6 +249,14 @@ func TestT9n(t *testing.T) { }, expOut: "exp3.json", }, + { // Transactions with too low gas + base: "./testdata/16", + input: t9nInput{ + inTxs: "signed_txs.rlp", + stFork: "London", + }, + expOut: "exp.json", + }, } { args := []string{"t9n"} diff --git a/cmd/evm/testdata/15/exp.json b/cmd/evm/testdata/15/exp.json index 03d970c56..1893fdfc0 100644 --- a/cmd/evm/testdata/15/exp.json +++ b/cmd/evm/testdata/15/exp.json @@ -1,8 +1,10 @@ [ { - "error": "transaction type not supported" + "error": "transaction type not supported", + "hash": "0xa98a24882ea90916c6a86da650fbc6b14238e46f0af04a131ce92be897507476" }, { - "error": "transaction type not supported" + "error": "transaction type not supported", + "hash": "0x36bad80acce7040c45fd32764b5c2b2d2e6f778669fb41791f73f546d56e739a" } -] +] \ No newline at end of file diff --git a/cmd/evm/testdata/16/exp.json b/cmd/evm/testdata/16/exp.json new file mode 100644 index 000000000..ea855aeec --- /dev/null +++ b/cmd/evm/testdata/16/exp.json @@ -0,0 +1,11 @@ +[ + { + "address": "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b", + "hash": "0x7cc3d1a8540a44736750f03bb4d85c0113be4b3472a71bf82241a3b261b479e6" + }, + { + "error": "intrinsic gas too low: have 82, want 21000", + "address": "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b", + "hash": "0x3b2d2609e4361562edb9169314f4c05afc6dbf5d706bf9dda5abe242ab76a22b" + } +] \ No newline at end of file diff --git a/cmd/evm/testdata/16/signed_txs.rlp b/cmd/evm/testdata/16/signed_txs.rlp new file mode 100644 index 000000000..952ced213 --- /dev/null +++ b/cmd/evm/testdata/16/signed_txs.rlp @@ -0,0 +1 @@ +"0xf8cab86401f8610180018252089411111111111111111111111111111111111111112080c001a0937f65ef1deece46c473b99962678fb7c38425cf303d1e8fa9717eb4b9d012b5a01940c5a5647c4940217ffde1051a5fd92ec8551e275c1787f81f50a2ad84de43b86201f85f018001529411111111111111111111111111111111111111112080c001a0241c3aec732205542a87fef8c76346741e85480bce5a42d05a9a73dac892f84ca04f52e2dfce57f3a02ed10e085e1a154edf38a726da34127c85fc53b4921759c8" \ No newline at end of file diff --git a/cmd/evm/testdata/16/unsigned_txs.json b/cmd/evm/testdata/16/unsigned_txs.json new file mode 100644 index 000000000..f61958940 --- /dev/null +++ b/cmd/evm/testdata/16/unsigned_txs.json @@ -0,0 +1,34 @@ +[ + { + "input" : "0x", + "gas" : "0x5208", + "nonce" : "0x0", + "to" : "0x1111111111111111111111111111111111111111", + "value" : "0x20", + "v" : "0x0", + "r" : "0x0", + "s" : "0x0", + "secretKey" : "0x45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8", + "chainId" : "0x1", + "type" : "0x1", + "gasPrice": "0x1", + "accessList" : [ + ] + }, + { + "input" : "0x", + "gas" : "0x52", + "nonce" : "0x0", + "to" : "0x1111111111111111111111111111111111111111", + "value" : "0x20", + "v" : "0x0", + "r" : "0x0", + "s" : "0x0", + "secretKey" : "0x45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8", + "chainId" : "0x1", + "type" : "0x1", + "gasPrice": "0x1", + "accessList" : [ + ] + } +]