Skip to content

Commit

Permalink
prevent static call write to ipgraph (piplabs#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
kingster-will authored Dec 9, 2024
1 parent f650370 commit 6c176f1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
7 changes: 7 additions & 0 deletions core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ type EVM struct {
// available gas is calculated in gasCall* according to the 63/64 rule and later
// applied in opCall*.
callGasTemp uint64
// currentPrecompileCallType identifies the current type of the precompile call
// (CALL, CALLCODE, DELEGATECALL, STATICCALL)
currentPrecompileCallType OpCode
}

// NewEVM returns a new EVM. The returned EVM is not thread safe and should
Expand Down Expand Up @@ -229,6 +232,7 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas
evm.Context.Transfer(evm.StateDB, caller.Address(), addr, value)

if isPrecompile {
evm.currentPrecompileCallType = CALL
ret, gas, err = RunPrecompiledContract(evm, p, input, gas, evm.Config.Tracer)
log.Info("Call", "precompile", true, "ret", ret, "gas", gas, "err", err)
} else {
Expand Down Expand Up @@ -299,6 +303,7 @@ func (evm *EVM) CallCode(caller ContractRef, addr common.Address, input []byte,

// It is allowed to call precompiles, even via delegatecall
if p, isPrecompile := evm.precompile(addr); isPrecompile {
evm.currentPrecompileCallType = CALLCODE
ret, gas, err = RunPrecompiledContract(evm, p, input, gas, evm.Config.Tracer)
} else {
addrCopy := addr
Expand Down Expand Up @@ -350,6 +355,7 @@ func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []by

// It is allowed to call precompiles, even via delegatecall
if p, isPrecompile := evm.precompile(addr); isPrecompile {
evm.currentPrecompileCallType = DELEGATECALL
ret, gas, err = RunPrecompiledContract(evm, p, input, gas, evm.Config.Tracer)
} else {
addrCopy := addr
Expand Down Expand Up @@ -404,6 +410,7 @@ func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte
evm.StateDB.AddBalance(addr, new(uint256.Int), tracing.BalanceChangeTouchAccount)

if p, isPrecompile := evm.precompile(addr); isPrecompile {
evm.currentPrecompileCallType = STATICCALL
ret, gas, err = RunPrecompiledContract(evm, p, input, gas, evm.Config.Tracer)
} else {
// At this point, we use a copy of address. If we don't, the go compiler will
Expand Down
9 changes: 9 additions & 0 deletions core/vm/ipgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ func (c *ipGraph) addParentIp(input []byte, evm *EVM, ipGraphAddress common.Addr
}

log.Info("addParentIp", "input", input)

if evm.currentPrecompileCallType != CALL {
return nil, fmt.Errorf("addParentIp can only be called with CALL, not %v", evm.currentPrecompileCallType)
}

if len(input) < 96 {
return nil, fmt.Errorf("input too short for addParentIp")
}
Expand Down Expand Up @@ -328,6 +333,10 @@ func (c *ipGraph) setRoyalty(input []byte, evm *EVM, ipGraphAddress common.Addre
}

log.Info("setRoyalty", "ipGraphAddress", ipGraphAddress, "input", input)
if evm.currentPrecompileCallType != CALL {
return nil, fmt.Errorf("setRoyalty can only be called with CALL, not %v", evm.currentPrecompileCallType)
}

if len(input) < 96 {
return nil, fmt.Errorf("input too short for setRoyalty")
}
Expand Down

0 comments on commit 6c176f1

Please sign in to comment.