|Author||Martin Holst Swende|
This is a proposal to add a new opcode,
CALLDEPTH opcode would return the remaining available call stack depth.
There is a limit specifying how deep contracts can call other contracts; the call stack. The limit is currently
256. If a contract invokes another contract (either via
CALLCODE), the operation will fail if the call stack depth limit has been reached.
This behaviour makes it possible to subject a contract to a “call stack attack” . In such an attack, an attacker first creates a suitable depth of the stack, e.g. by recursive calls. After this step, the attacker invokes the targeted contract. If the targeted calls another contract, that call will fail. If the return value is not properly checked to see if the call was successful, the consequences could be damaging.
Awants to be invoked regularly, and pays Ether to the invoker in every block.
- When contract
Ais invoked, it calls contracts
C, which consumes a lot of gas. After invocation, contract
Apays Ether to the caller.
- Malicious user
Xensures that the stack depth is shallow before invoking A. Both calls to
Xcan still collect the reward.
It is possible to defend against this in two ways:
- Check return value after invocation.
- Check call stack depth experimentally. A library  by Piper Merriam exists for this purpose. This method is quite costly in gas.
 a.k.a “shallow stack attack” and “stack attack”. However, to be precise, the word ‘‘stack’’ has a different meaning within the EVM, and is not to be confused with the ‘‘call stack’’.
CALLDEPTH should return the remaining call stack depth. A value of
0 means that the call stack is exhausted, and no further calls can be made.
The actual call stack depth, as well as the call stack depth limit, are present in the EVM during execution, but just not available within the EVM. The implementation should be fairly simple and would provide a cheap and way to protect against call stack attacks.
Please cite this document as:
Martin Holst Swende, "EIP-3: Addition of CALLDEPTH opcode," Ethereum Improvement Proposals, no. 3, November 2015. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-3.