Skip to content

Commit

Permalink
Merge pull request bnb-chain#8 from Ankr-network/audit-fixes
Browse files Browse the repository at this point in the history
Certik Audit Fixes
  • Loading branch information
hedwig0x authored May 16, 2022
2 parents 052a3b8 + c2c73a1 commit 328fad0
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 61 deletions.
24 changes: 0 additions & 24 deletions common/systemcontract/abi.go

This file was deleted.

33 changes: 0 additions & 33 deletions common/systemcontract/abi/IEvmHooks.json

This file was deleted.

1 change: 1 addition & 0 deletions common/systemcontract/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var systemContracts = map[common.Address]bool{
common.HexToAddress(TokenHubContract): false,
common.HexToAddress(RelayerIncentivizeContract): false,
common.HexToAddress(CrossChainContract): false,
common.HexToAddress(TokenManagerContract): false,
// BAS smart contracts
common.HexToAddress(StakingPoolContract): true,
common.HexToAddress(GovernanceContract): true,
Expand Down
5 changes: 3 additions & 2 deletions core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func (evm *EVM) precompile(addr, caller common.Address) (PrecompiledContract, bo
evmHook := systemcontract2.CreateEvmHook(addr, systemcontract2.EvmHookContext{
CallerAddress: caller,
StateDb: evm.StateDB,
Evm: evm,
ChainConfig: evm.chainConfig,
ChainRules: evm.chainRules,
})
Expand Down Expand Up @@ -569,8 +570,8 @@ func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.I
}

// CreateWithAddress creates a new contract using code as deployment code.
func (evm *EVM) CreateWithAddress(caller ContractRef, code []byte, gas uint64, value *big.Int, contractAddr common.Address) (ret []byte, leftOverGas uint64, err error) {
ret, _, leftOverGas, err = evm.create(caller, &codeAndHash{code: code}, gas, value, contractAddr, STOP)
func (evm *EVM) CreateWithAddress(caller common.Address, code []byte, gas uint64, value *big.Int, contractAddr common.Address) (ret []byte, leftOverGas uint64, err error) {
ret, _, leftOverGas, err = evm.create(AccountRef(caller), &codeAndHash{code: code}, gas, value, contractAddr, STOP)
return
}

Expand Down
6 changes: 6 additions & 0 deletions core/vm/systemcontract/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package systemcontract
import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/params"
"math/big"
)

type EvmHook interface {
Expand All @@ -17,9 +18,14 @@ type StateDB interface {
GetCodeSize(common.Address) int
}

type EVM interface {
CreateWithAddress(caller common.Address, code []byte, gas uint64, value *big.Int, contractAddr common.Address) (ret []byte, leftOverGas uint64, err error)
}

type EvmHookContext struct {
CallerAddress common.Address
StateDb StateDB
Evm EVM
ChainConfig *params.ChainConfig
ChainRules params.Rules
}
24 changes: 23 additions & 1 deletion core/vm/systemcontract/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/systemcontract"
"math/big"
)

type evmHookRuntimeUpgrade struct {
Expand All @@ -23,6 +25,10 @@ var (
addressType = mustNewType("address")
bytesType = mustNewType("bytes")
// input args
deployToMethod = abi.NewMethod("deployTo(address,bytes)", "deployTo", abi.Function, "", false, false, abi.Arguments{
abi.Argument{Type: addressType}, // system contract address
abi.Argument{Type: bytesType}, // new byte code
}, abi.Arguments{})
upgradeToMethod = abi.NewMethod("upgradeTo(address,bytes)", "upgradeTo", abi.Function, "", false, false, abi.Arguments{
abi.Argument{Type: addressType}, // system contract address
abi.Argument{Type: bytesType}, // new byte code
Expand All @@ -41,7 +47,7 @@ func matchesMethod(input []byte, method abi.Method) []interface{} {
return values
}

var runtimeUpgradeContract = common.HexToAddress("0x0000000000000000000000000000000000007004")
var runtimeUpgradeContract = common.HexToAddress(systemcontract.RuntimeUpgradeContract)

func (sc *evmHookRuntimeUpgrade) Run(input []byte) ([]byte, error) {
if !sc.context.ChainRules.HasRuntimeUpgrade {
Expand All @@ -52,6 +58,22 @@ func (sc *evmHookRuntimeUpgrade) Run(input []byte) ([]byte, error) {
return nil, errInvalidCaller
}
// if matches upgrade to method
if values := matchesMethod(input, deployToMethod); values != nil {
contractAddress, ok := values[0].(common.Address)
if !ok {
return nil, errFailedToUnpack
}
deployerByteCode, ok := values[1].([]byte)
if !ok {
return nil, errFailedToUnpack
}
byteCode, _, err := sc.context.Evm.CreateWithAddress(contractAddress, deployerByteCode, 0, big.NewInt(0), contractAddress)
if err != nil {
return nil, err
}
sc.context.StateDb.SetCode(contractAddress, byteCode)
return nil, nil
}
if values := matchesMethod(input, upgradeToMethod); values != nil {
contractAddress, ok := values[0].(common.Address)
if !ok {
Expand Down
2 changes: 1 addition & 1 deletion genesis

0 comments on commit 328fad0

Please sign in to comment.