EIP-3 Add the CALLDEPTH opcode (Addition of CALLDEPTH opcode)
No call decision · EVM & execution · EIP status Withdrawn · created 2015-11-19
A proposal to add a CALLDEPTH opcode that directly reports the remaining call stack depth to the EVM, letting a contract detect ahead of time that a call could fail due to a call stack attack.
Why it is needed
Ethereum imposes a limit (256) on the depth to which a contract can call other contracts, i.e. the call stack. Once this limit is reached, a CALL or CALLCODE invocation fails. An attacker can fill the call stack almost to its limit beforehand, for example through recursive calls, and then call the target contract. If the target contract then calls another contract internally, that inner call fails silently, and a contract that does not check the return value will behave unintentionally, for instance paying out a reward without knowing the call failed. Defending against this so-called call stack attack requires either checking the return value every time, or using an external library that experimentally measures the depth through recursive calls, and the latter is expensive in gas. Since the call stack depth and its limit are information that already exists inside the EVM, this proposal argues that exposing it directly as an opcode would provide a cheap and simple defense against the attack.
What changes
Today
- When the call stack limit (256) is reached, CALL/CALLCODE invocations fail silently.
- A contract that does not check the return value will not notice that a call failed.
- There is no way to query the remaining call stack depth directly from within the EVM.
- Finding out the depth requires an external library that experiments via recursive calls, which is expensive in gas.
After EIP-3
- The CALLDEPTH opcode lets you query the remaining call stack depth directly.
- If the return value is 0, you immediately know no further calls can be made.
- A contract can check the depth before calling and avoid a call stack attack.
- The depth can be checked cheaply, without an external measurement library.
Who is affected
- Smart contract developers: Defensive logic against call stack attacks can be written more cheaply and simply.
- Operators of contracts that pay rewards after a call: The risk of missing an internal call failure and paying out a reward incorrectly is reduced.
- Client (EVM) implementation teams: Logic to handle the new opcode must be added to the EVM.
In depth
Background: the call stack attack
Ethereum imposes a limit on the depth of calls between contracts, i.e. the call stack. An attacker can fill the call stack almost to its limit beforehand through recursive calls, then call the target contract. If the target contract internally calls another contract via CALL or CALLCODE, that call fails because the limit is exceeded, and if the return value is not checked, this failure goes unnoticed. The original text calls this a 'call stack attack' (also known as a shallow stack attack or stack attack), and gives as an example a contract that pays a reward after a call mistakenly paying the reward to the attacker.
Specification: the CALLDEPTH opcode
The CALLDEPTH opcode returns the remaining call stack depth. A return value of 0 means the call stack is fully exhausted and no further calls can be made. The specification ends there; specific implementation details such as gas cost or opcode number are not stated in the text.
The current call stack limit structure
According to the original text, the call stack depth limit is currently 256, and when a contract calls another contract via CALL or CALLCODE and reaches this limit, that call operation fails.
Existing defensive methods and their cost
The original text presents two existing defenses against call stack attacks. The first is checking the return value after a call, and the second is a method, such as the library created by Piper Merriam, that experimentally measures the call stack depth through recursive calls. The text explains that the second method is considerably expensive in terms of gas cost.
Rationale
The original text argues that the actual call stack depth and its limit are information that already exists during EVM execution, but cannot be queried from within the EVM. It explains that exposing this via an opcode would be a relatively simple implementation, and would provide a cheap and simple defense against call stack attacks.
Implementation status and current state
The original text's Implementation section states only 'Not implemented,' indicating that no actual implementation was done. Also, this EIP's status is Withdrawn; proposed in November 2015, it was never actually adopted by Ethereum.