EIPs Explained

EIP-5283 · Stagnant · Core · 2022-07-17

EIP-5283: Semaphore for Reentrancy Protection

Proposed a precompile that reverts if the caller already appears in the call stack, giving contracts a reentrancy guard that never writes storage and so stays compatible with parallel EVM execution.

What problem does it solve?

The standard reentrancy guard, OpenZeppelin's ReentrancyGuard, stores a flag. On entry it checks the slot and sets it, and on exit it resets it. That read-then-write on one storage slot conflicts with parallel transaction execution. The most effective parallel EVM designs detect conflicts per storage slot, and a guard slot touched by every call to a popular token or protocol serializes everything that uses it.

EIP-5283 proposes a reentrancy guard with no storage access at all, so contracts written today would stay parallelizable later.

How it works

The proposal adds a precompile, Semaphore, at address 0x0A. It was the next free address in the range reserved by EIP-1352 when the EIP was written.

  • When called, it checks whether the caller's address appears more than once in the current call stack.
  • If it does, which means the contract has been re-entered, the precompile behaves like an immediate REVERT, so the CALL returns 0.
  • Otherwise it returns 1.
  • It costs a flat 100 gas, sized for scanning a nearly full call stack of about 400 addresses.

A contract calls it at the start of a protected function and reverts if the call fails. No cleanup is needed on exit, because the call stack unwinds by itself.

The rationale also compares other options. Transient storage (TLOAD/TSTORE), a SSTORE_COUNT opcode and a LOCKCALL opcode would all need new opcodes. A precompile avoids changes to compilers and tools.

Sample code

The modifier from the EIP, cleaned up:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

abstract contract SemaphoreGuard {
    address constant SEMAPHORE = address(0x0A);   // as proposed; never deployed

    modifier nonReentrant() {
        assembly {
            if iszero(staticcall(1000, SEMAPHORE, 0, 0, 0, 0)) { revert(0, 0) }
        }
        _;
    }
}

contract Test is SemaphoreGuard {
    function second() external nonReentrant {}
    function first() external nonReentrant { this.second(); } // reverts: Test is on the stack twice
}

With the EIP active, calling second() directly succeeds and calling first() reverts.

Things to know

  • Status: Stagnant. It was never scheduled for a hard fork, and the activation block was never set. Security Considerations were still marked "needs discussion".
  • Address 0x0A was later taken by the KZG point evaluation precompile from EIP-4844. Do not use the sample above on any real chain.
  • The problem was largely solved another way. Transient storage (EIP-1153), live on mainnet since the Cancun upgrade, lets a reentrancy flag live in per-transaction memory that is never written to state. OpenZeppelin provides ReentrancyGuardTransient.
  • The author pointed out that a precompile call costs about 800 gas in total. That is more than the roughly 200 gas a storage guard costs thanks to EIP-1283-style net metering, unless precompile call costs are reduced.
  • A guard only helps new contracts. Contracts already deployed with storage-based guards would not become parallelizable.

#reentrancy #precompile #parallel-execution #core #security

Details

Authors
Sergio D. Lerner (@SergioDemianLerner)
Status
Stagnant
Type
Standards Track / Core
Created
2022-07-17
Requires

Machine-readable: Markdown · JSON