Skip to content

Commit

Permalink
Merge pull request #27 from ava-labs/prevent-prohibited-addresses
Browse files Browse the repository at this point in the history
Prevent Prohibited Sender/Creation
  • Loading branch information
aaronbuchwald authored Feb 26, 2022
2 parents 1ce1a6e + 50f6233 commit 67a5927
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 18 deletions.
8 changes: 2 additions & 6 deletions chain/subnet_evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/subnet-evm/constants"
"github.com/ava-labs/subnet-evm/core"
"github.com/ava-labs/subnet-evm/core/state"
"github.com/ava-labs/subnet-evm/core/types"
Expand All @@ -19,11 +20,6 @@ import (
"github.com/ethereum/go-ethereum/log"
)

var BlackholeAddr = common.Address{
1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
}

type (
Tx = types.Transaction
Block = types.Block
Expand All @@ -47,7 +43,7 @@ func NewETHChain(config *eth.Config, nodecfg *node.Config, chainDB ethdb.Databas
chain := &ETHChain{backend: backend}
if config.Miner.Etherbase == (common.Address{}) { // used for testing
log.Warn("Etherbase not set. Falling back to blackhole address.")
backend.SetEtherbase(BlackholeAddr)
backend.SetEtherbase(constants.BlackholeAddr)
} else {
backend.SetEtherbase(config.Miner.Etherbase)
}
Expand Down
13 changes: 13 additions & 0 deletions constants/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// (c) 2021-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package constants

import "github.com/ethereum/go-ethereum/common"

var (
BlackholeAddr = common.Address{
1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
}
)
6 changes: 3 additions & 3 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,9 @@ func (st *StateTransition) preCheck() error {
return fmt.Errorf("%w: address %v, codehash: %s", ErrSenderNoEOA,
st.msg.From().Hex(), codeHash)
}
// Make sure the sender is not the Blackhole
if st.msg.From() == st.evm.Context.Coinbase {
return fmt.Errorf("%w: address %v", vm.ErrNoSenderBlackhole, st.msg.From())
// Make sure the sender is not prohibited
if vm.IsProhibited(st.msg.From()) {
return fmt.Errorf("%w: address %v", vm.ErrAddrProhibited, st.msg.From())
}
}
// Make sure that transaction gasFeeCap is greater than the baseFee (post london)
Expand Down
2 changes: 1 addition & 1 deletion core/vm/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var (
ErrGasUintOverflow = errors.New("gas uint64 overflow")
ErrInvalidCode = errors.New("invalid code: must not begin with 0xef")
ErrNonceUintOverflow = errors.New("nonce uint64 overflow")
ErrNoSenderBlackhole = errors.New("blackhole address cannot be used as sender")
ErrAddrProhibited = errors.New("prohibited address cannot be sender or created contract address")

// errStopToken is an internal token indicating interpreter loop termination,
// never returned to outside callers.
Expand Down
22 changes: 20 additions & 2 deletions core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"sync/atomic"
"time"

"github.com/ava-labs/subnet-evm/constants"
"github.com/ava-labs/subnet-evm/params"
"github.com/ava-labs/subnet-evm/precompile"
"github.com/ethereum/go-ethereum/common"
Expand All @@ -40,6 +41,23 @@ import (
"github.com/holiman/uint256"
)

var prohibitedAddresses = map[common.Address]struct{}{
constants.BlackholeAddr: {},
}

func init() {
for _, addr := range precompile.PrecompileAddresses {
prohibitedAddresses[addr] = struct{}{}
}
}

// IsProhibited returns true if [addr] is in the prohibited list of addresses which should
// not be allowed as an EOA or newly created contract address.
func IsProhibited(addr common.Address) bool {
_, ok := prohibitedAddresses[addr]
return ok
}

// emptyCodeHash is used by create to ensure deployment is disallowed to already
// deployed contract addresses (relevant after the account abstraction).
var emptyCodeHash = crypto.Keccak256Hash(nil)
Expand Down Expand Up @@ -446,8 +464,8 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64,
}
// If there is any collision with the Blackhole address, return an error instead
// of allowing the contract to be created.
if address == evm.Context.Coinbase {
return nil, common.Address{}, gas, ErrNoSenderBlackhole
if IsProhibited(address) {
return nil, common.Address{}, gas, ErrAddrProhibited
}
nonce := evm.StateDB.GetNonce(caller.Address())
if nonce+1 < nonce {
Expand Down
7 changes: 3 additions & 4 deletions plugin/evm/block_verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ import (

"github.com/ethereum/go-ethereum/common"

"github.com/ava-labs/subnet-evm/constants"
"github.com/ava-labs/subnet-evm/core/types"
"github.com/ava-labs/subnet-evm/params"
"github.com/ava-labs/subnet-evm/trie"

subnetEVM "github.com/ava-labs/subnet-evm/chain"
)

var (
Expand Down Expand Up @@ -84,7 +83,7 @@ func (v blockValidatorLegacy) SyntacticVerify(b *Block) error {
return errUncleHashMismatch
}
// Coinbase must be zero on C-Chain
if b.ethBlock.Coinbase() != subnetEVM.BlackholeAddr {
if b.ethBlock.Coinbase() != constants.BlackholeAddr {
return errInvalidBlock
}
// Block must not have any uncles
Expand Down Expand Up @@ -182,7 +181,7 @@ func (blockValidatorSubnetEVM) SyntacticVerify(b *Block) error {
return errUncleHashMismatch
}
// Coinbase must be zero, if AllowFeeRecipients is not enabled
if !b.vm.chainConfig.AllowFeeRecipients && b.ethBlock.Coinbase() != subnetEVM.BlackholeAddr {
if !b.vm.chainConfig.AllowFeeRecipients && b.ethBlock.Coinbase() != constants.BlackholeAddr {
return errInvalidBlock
}
// Block must not have any uncles
Expand Down
4 changes: 2 additions & 2 deletions plugin/evm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"sync"
"time"

schain "github.com/ava-labs/subnet-evm/chain"
subnetEVM "github.com/ava-labs/subnet-evm/chain"
"github.com/ava-labs/subnet-evm/constants"
"github.com/ava-labs/subnet-evm/core"
"github.com/ava-labs/subnet-evm/core/types"
"github.com/ava-labs/subnet-evm/eth/ethconfig"
Expand Down Expand Up @@ -254,7 +254,7 @@ func (vm *VM) Initialize(
ethConfig.SnapshotVerify = vm.config.SnapshotVerify

// Handle custom fee recipient
ethConfig.Miner.Etherbase = schain.BlackholeAddr
ethConfig.Miner.Etherbase = constants.BlackholeAddr
switch {
case common.IsHexAddress(vm.config.FeeRecipient):
if g.Config.AllowFeeRecipients {
Expand Down
5 changes: 5 additions & 0 deletions precompile/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ const (
var (
ModifyAllowListAddress = common.HexToAddress("0x0200000000000000000000000000000000000000")
ReadAllowListAddress = common.HexToAddress("0x0200000000000000000000000000000000000001")

PrecompileAddresses = []common.Address{
ModifyAllowListAddress,
ReadAllowListAddress,
}
)

0 comments on commit 67a5927

Please sign in to comment.