Skip to content

Commit 8d2c341

Browse files
carltravelerlaizy
authored andcommitted
add wasm magic check when preexecute neo contract deploy (#1101)
1 parent b1bd061 commit 8d2c341

File tree

2 files changed

+76
-4
lines changed

2 files changed

+76
-4
lines changed

core/store/ledgerstore/ledger_store.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package ledgerstore
2020

2121
import (
22+
"bytes"
2223
"crypto/sha256"
2324
"fmt"
2425
types2 "github.com/ontio/ontology/vm/neovm/types"
@@ -960,10 +961,6 @@ func (this *LedgerStoreImp) GetRawHeaderByHash(blockHash common.Uint256) (*types
960961
//GetHeaderByHash return the block header by block height
961962
func (this *LedgerStoreImp) GetHeaderByHeight(height uint32) (*types.Header, error) {
962963
blockHash := this.GetBlockHash(height)
963-
var empty common.Uint256
964-
if blockHash == empty {
965-
return nil, nil
966-
}
967964
return this.GetHeaderByHash(blockHash)
968965
}
969966

@@ -1097,6 +1094,14 @@ func (this *LedgerStoreImp) PreExecuteContract(tx *types.Transaction) (*sstate.P
10971094
if err != nil {
10981095
return stf, err
10991096
}
1097+
} else {
1098+
wasmMagicversion := []byte{0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00}
1099+
1100+
if len(deploy.GetRawCode()) >= len(wasmMagicversion) {
1101+
if bytes.Compare(wasmMagicversion, deploy.GetRawCode()[:8]) == 0 {
1102+
return stf, errors.NewErr("this code is wasm binary. can not deployed as neo contract")
1103+
}
1104+
}
11001105
}
11011106

11021107
return &sstate.PreExecResult{State: event.CONTRACT_STATE_SUCCESS, Gas: gasTable[neovm.CONTRACT_CREATE_NAME] + calcGasByCodeLen(len(deploy.GetRawCode()), gasTable[neovm.UINT_DEPLOY_CODE_LEN_NAME]), Result: nil}, nil
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (C) 2018 The ontology Authors
3+
* This file is part of The ontology library.
4+
*
5+
* The ontology is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* The ontology is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with The ontology. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
package test
20+
21+
import (
22+
"os"
23+
"testing"
24+
25+
"github.com/ontio/ontology/account"
26+
"github.com/ontio/ontology/cmd/utils"
27+
"github.com/ontio/ontology/core/payload"
28+
"github.com/ontio/ontology/core/store/ledgerstore"
29+
"github.com/stretchr/testify/assert"
30+
)
31+
32+
func TestPreExecuteContractWasmDeploy(t *testing.T) {
33+
acct := account.NewAccount("")
34+
testLedgerStore, err := ledgerstore.NewLedgerStore("test/ledgerfortmp", 0)
35+
/** file: test_create.wat
36+
(module
37+
(type (;0;) (func))
38+
(type (;1;) (func (param i32 i32)))
39+
(import "env" "ontio_return" (func (;0;) (type 1)))
40+
(func (;1;) (type 0)
41+
i32.const 0
42+
i64.const 2222
43+
i64.store
44+
i32.const 0
45+
i32.const 8
46+
call 0
47+
)
48+
(memory (;0;) 1)
49+
(export "invoke" (func 1)))
50+
**/
51+
code := []byte{0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x09, 0x02, 0x60, 0x00, 0x00, 0x60, 0x02, 0x7f, 0x7f, 0x00, 0x02, 0x14, 0x01, 0x03, 0x65, 0x6e, 0x76, 0x0c, 0x6f, 0x6e, 0x74, 0x69, 0x6f, 0x5f, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x00, 0x01, 0x03, 0x02, 0x01, 0x00, 0x05, 0x03, 0x01, 0x00, 0x01, 0x07, 0x0a, 0x01, 0x06, 0x69, 0x6e, 0x76, 0x6f, 0x6b, 0x65, 0x00, 0x01, 0x0a, 0x12, 0x01, 0x10, 0x00, 0x41, 0x00, 0x42, 0xae, 0x11, 0x37, 0x03, 0x00, 0x41, 0x00, 0x41, 0x08, 0x10, 0x00, 0x0b}
52+
mutable, _ := utils.NewDeployCodeTransaction(0, 100000000, code, payload.NEOVM_TYPE, "name", "version",
53+
"author", "email", "desc")
54+
_ = utils.SignTransaction(acct, mutable)
55+
tx, err := mutable.IntoImmutable()
56+
_, err = testLedgerStore.PreExecuteContract(tx)
57+
assert.EqualError(t, err, "this code is wasm binary. can not deployed as neo contract")
58+
59+
mutable, _ = utils.NewDeployCodeTransaction(0, 100000000, code, payload.WASMVM_TYPE, "name", "version",
60+
"author", "email", "desc")
61+
_ = utils.SignTransaction(acct, mutable)
62+
tx, err = mutable.IntoImmutable()
63+
_, err = testLedgerStore.PreExecuteContract(tx)
64+
assert.Nil(t, err)
65+
66+
_ = os.RemoveAll("./test")
67+
}

0 commit comments

Comments
 (0)