EIP-7705 Reentrancy guard opcode addition (NONREENTRANT and REENTRANT opcodes)

No call decision · EVM & execution · EIP status Stagnant · created 2024-05-09

A proposal to add two new EVM opcodes, NONREENTRANT and REENTRANT, that let a contract set and clear its own reentrancy-lock state, making defense against reentrancy attacks cheaper and easier.

Why it is needed

A significant portion of funds stolen from Ethereum, including the DAO hack, is due to reentrancy attacks. A reentrancy attack occurs when a contract calls an external contract and, before updating its own state, the called contract calls back into the original contract to drain funds. Preventing this requires implementing a lock directly in contract code, which costs gas and developers often omit. The introduction of transient storage (EIP-1153) lowered the cost, but it is still not cheap enough to apply as a default. Because this EIP provides reentrancy protection cheaply at the opcode level, developers can more easily build safe contracts.

What changes

Today

After EIP-7705

Who is affected

In depth

Opcode definitions

Two new opcodes are added, NONREENTRANT (0xF6) and REENTRANT (0xF7). They respectively set and clear the contract's reentrancy-lock flag.

How it works

A contract that has executed NONREENTRANT can no longer have execution context handed to it via any *CALL opcode. Calling a contract whose reentrancy-lock flag is set is treated identically to executing a single REVERT opcode.

Idempotence

Both NONREENTRANT and REENTRANT are idempotent. Executing NONREENTRANT again on a contract that is already locked has no effect, and the same applies to REENTRANT.

Scope and restoration

The reentrancy-lock flag's scope is limited to the current transaction, and the flag is reset once every transaction ends. If a REVERT or an exceptional halt occurs, the contract's reentrancy-lock flag reverts to its value before the call.

Gas cost

Both NONREENTRANT and REENTRANT have a fixed gas cost of 5 (G_mid). The cost of saving the current value on the call stack for revert handling is explained as already included in the overhead cost of the *CALL opcodes.

Alternative design

An alternative design with a single opcode (NONREENTRANT) that pops a value from the stack and sets the reentrancy-lock flag based on that value is also mentioned as under consideration. This alternative may be adopted depending on feedback.

Current status and limitations

This EIP is in Stagnant status; test cases and a reference implementation have not yet been written, and the Security Considerations section is also empty, marked TBD. There is no call decision on fork inclusion. The concrete specification may therefore change going forward.

Original EIP · ← EIPs