Submitted by berndartmueller
The minievm cosmos precompile allows a Solidity contract to dispatch a Cosmos SDK message, which is executed after the EVM call is successfully executed.
This is done by calling the cosmos precompile with the execute_cosmos or execute_cosmos_with_options function selector and passing the encoded message. This will wrap the message with ExecuteRequest and append it to the messages slice in the context with the key types.CONTEXT_KEY_EXECUTE_REQUESTS.
minievm:x/evm/precompiles/cosmos/contract.go#L287-L294
Then, after the EVM call finished successfully in EVMCallWithTracer(), it executes the scheduled messages. The same is done in EVMCreateWithTracer().
The issue is that dispatched messages are not removed from the queue in the scenario where they have been dispatched by an external Solidity that reverts but where a try/catch contains the error. In this case, the messages are still executed.
It seems this is because prepareSDKContext(..) sets the empty cosmos messages queue to the outer context, while any internal EVM that creates a new statedb snapshot will not result in a new context with its own, empty types.CONTEXT_KEY_EXECUTE_REQUESTS queue. When the cosmos precompile retrieves the queue from the context with ctx.Value(types.CONTEXT_KEY_EXECUTE_REQUESTS), the context is traversed up to the outer context which holds the key/value (pointer).
Overall, this is very problematic as it leads to unexpected behavior. For example, the external call reverts on purpose due to an invariant being violated, but the dispatched message that is still executed might be a sensitive operation (e.g., IBC token transfer) that should only be executed in the successful case. This can have severe consequences, including funds being locked or lost.
The following Poc amends the Counter Solidity contract by adding a new external function nested(..), which dispatches a nested MsgCall EVM Cosmos SDK messages, followed by a revert().
The nested(..) function is called in the recursive(..) function, wrapped in a try/catch block so that the revert is caught and does not propagate.
After applying the git diff, re-compile the Solidity contracts with make contracts-gen.
Then, copy and paste the following test case in x/evm/keeper/context_test.go. It will show that the dispatched message is still executed, by verifying that the logs contain the recursive_called event twice.
Consider making sure that the ExecuteRequests that have been dispatched by an EVM call that reverted, are properly removed and not executed.
beer-1 (Initia) confirmed and commented:
Initia mitigated:
Status: Mitigation confirmed. Full details in reports from 0xAlix2, berndartmueller and Franfran.
