From 926104ecdc0071754ce41229b544c127c356297a Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Fri, 16 Feb 2024 21:31:19 +0100 Subject: [PATCH] fix: check for eip activation in SSTORE --- core/evm.go | 1 + core/vm/errors.go | 1 + core/vm/evm.go | 1 + core/vm/instructions.go | 8 ++++++-- core/vm/runtime/env.go | 1 + core/vm/runtime/runtime.go | 1 + 6 files changed, 11 insertions(+), 2 deletions(-) diff --git a/core/evm.go b/core/evm.go index 73f6d7bc20..e2a1061824 100644 --- a/core/evm.go +++ b/core/evm.go @@ -86,6 +86,7 @@ func NewEVMTxContext(msg *Message) vm.TxContext { if msg.BlobGasFeeCap != nil { ctx.BlobFeeCap = new(big.Int).Set(msg.BlobGasFeeCap) } + ctx.Is5806 = msg.Delegate return ctx } diff --git a/core/vm/errors.go b/core/vm/errors.go index fbbf19e178..002400f508 100644 --- a/core/vm/errors.go +++ b/core/vm/errors.go @@ -33,6 +33,7 @@ var ( ErrMaxCodeSizeExceeded = errors.New("max code size exceeded") ErrInvalidJump = errors.New("invalid jump destination") ErrWriteProtection = errors.New("write protection") + ErrEip5806Write = errors.New("write attempt in eip5806 transaction") ErrReturnDataOutOfBounds = errors.New("return data out of bounds") ErrGasUintOverflow = errors.New("gas uint64 overflow") ErrInvalidCode = errors.New("invalid code: must not begin with 0xef") diff --git a/core/vm/evm.go b/core/vm/evm.go index 16cc854908..9219c7ad71 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -85,6 +85,7 @@ type TxContext struct { GasPrice *big.Int // Provides information for GASPRICE (and is used to zero the basefee if NoBaseFee is set) BlobHashes []common.Hash // Provides information for BLOBHASH BlobFeeCap *big.Int // Is used to zero the blobbasefee if NoBaseFee is set + Is5806 bool // Indicates if this is an eip-5806 transaction } // EVM is the Ethereum Virtual Machine base object and provides diff --git a/core/vm/instructions.go b/core/vm/instructions.go index ee0bce7c74..e5926e9adc 100644 --- a/core/vm/instructions.go +++ b/core/vm/instructions.go @@ -518,8 +518,12 @@ func opSload(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]by } func opSstore(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { - // if scope is an account with no code (EOA delegation through EIP-5806) sstore is protected - if interpreter.readOnly || scope.Contract.CodeHash == (common.Hash{}) { + if interpreter.evm.TxContext.Is5806 { + if scope.Contract.CodeHash == types.EmptyCodeHash || scope.Contract.CodeHash == (common.Hash{}) { + return nil, ErrEip5806Write + } + } + if interpreter.readOnly { return nil, ErrWriteProtection } loc := scope.Stack.pop() diff --git a/core/vm/runtime/env.go b/core/vm/runtime/env.go index 34335b8e9e..c5cb4bb11d 100644 --- a/core/vm/runtime/env.go +++ b/core/vm/runtime/env.go @@ -27,6 +27,7 @@ func NewEnv(cfg *Config) *vm.EVM { GasPrice: cfg.GasPrice, BlobHashes: cfg.BlobHashes, BlobFeeCap: cfg.BlobFeeCap, + Is5806: cfg.Is5806, } blockContext := vm.BlockContext{ CanTransfer: core.CanTransfer, diff --git a/core/vm/runtime/runtime.go b/core/vm/runtime/runtime.go index 46f2bb5d5f..0bfa5f4ee1 100644 --- a/core/vm/runtime/runtime.go +++ b/core/vm/runtime/runtime.go @@ -49,6 +49,7 @@ type Config struct { BlobHashes []common.Hash BlobFeeCap *big.Int Random *common.Hash + Is5806 bool State *state.StateDB GetHashFn func(n uint64) common.Hash