EIP-5283 Semaphore for reentrancy prevention (Semaphore for Reentrancy Protection)

No call decision · Security & privacy · EIP status Stagnant · created 2022-07-17

A proposal to add a precompile (0x0A) that checks the call stack instead of storage to prevent contract reentrancy. Since it doesn't use storage, it doesn't conflict with future parallel execution designs.

Why it is needed

The classic way to prevent reentrancy attacks is a reentrancy guard, which writes to and reads from a single storage cell inside the contract to determine whether reentrancy is occurring. Because this approach reads and writes the same storage cell on every call, it conflicts with parallel execution designs that aim to run the Ethereum Virtual Machine (EVM) across multiple threads simultaneously. The most successful parallelization approach so far detects conflicts at the storage-cell level, and the reentrancy guard's read-write pattern is exactly what blocks that parallel execution. Because it's difficult to automatically analyze and parallelize already-deployed contracts, there is a need for a way for future contracts to prevent reentrancy without touching storage.

What changes

Today

After EIP-5283

Who is affected

In depth

Specification: 0x0A precompile

After the activation block (TBD), a Semaphore precompile is created at address 0x0A. When this contract is called, if the caller's address already appears one or more times on the call stack, it behaves as if the first instruction were REVERT, and the CALL returns 0. Otherwise it executes no code and returns 1. The execution gas cost is fixed at 100 gas regardless of the call's outcome.

Rationale for the address choice

0x0A was chosen because it is the next free address within the precompile address range defined by EIP-1352.

Sample usage code

In Solidity, this is used inside a nonReentrant modifier by calling staticcall(1000, SemaphoreAddress, 0, 0, 0, 0) via inline assembly and executing revert(0, 0) if it fails. In the test cases, calling second() directly in a transaction does not revert, but if first() internally calls this.second() (that is, the same contract address appears twice on the stack), it reverts.

Already deployed contracts cannot be parallelized

To parallelize contracts that use existing storage-based reentrancy guards, the VM would need to automatically detect which storage variables serve as reentrancy guards and prove that they behave correctly. This requires static code analysis and is hard to implement within consensus, since the computational cost of detection and proof can be high and some functions are not protected by a guard, which complicates the proof. So instead of parallelizing already-deployed contracts, this proposal aims to help future contracts be designed so they can be parallelized.

Alternatives considered

The original text reviews three alternatives together. First, transient storage instructions like TLOAD/TSTORE persist only within a transaction and are not written to world state, enabling fine-grained parallelization. Second, an SSTORE_COUNT instruction that returns the number of SSTORE executions also helps with parallelization, but since it returns the count of SSTORE executions rather than the reentrancy count, it is harder to use correctly. Third, a LOCKCALL instruction similar to STATICALL but which only blocks storage writes from the caller contract would be cheaper, but some functions cannot be left without a guard. All of these alternatives require new opcodes, which is a drawback since it requires modifying compilers, debuggers, and static analysis tools, so adding a new opcode is avoided when a precompile can achieve the same thing at the same cost.

Gas cost calculation

The 100 gas cost assumes a worst case where the call stack is nearly full (around 400 addresses). Since the stack always resides in RAM and scanning it is fast, the cost could be lower once implemented and benchmarked in geth. Since the precompile call itself currently costs 700 gas, using Semaphore as a reentrancy check costs 800 gas in total. This is more expensive than the roughly 200 gas cost of a storage-based guard after EIP-1283, which discourages its use, but if the precompile call cost is lowered, it could drop to around 140 gas, making it cheaper than a storage-based guard. This proposal therefore recommends being introduced together with a reduction in precompile call cost.

Requires a hard fork, status is Stagnant

This change requires a hard fork and all full nodes must be updated. The Security Considerations section only states "Needs discussion," so the security discussion has not yet concluded. The current status is Stagnant, meaning discussion has stalled and neither an activation block nor inclusion has been decided. This call's notes contain no related decision.

Original EIP · ← EIPs