EIPs Explained

EIP-1706 · Withdrawn(已撤回) · Core(核心) · 2019-01-15

EIP-1706: Disable SSTORE with gasleft lower than call stipend

提议在剩余 gas 不超过 2300 时让 SSTORE 失败,使更便宜的存储写入不会破坏旧合约从 transfer/send 补贴中获得的重入保护;这一想法被并入 EIP-2200。

它解决什么问题?

当 Solidity 合约用 transfer 或 send 支付 ETH 时,接收方只会收到 2300 gas 的补贴(stipend)。许多合约隐式地依赖这一点:只有 2300 gas,恶意接收方无法写入存储,因为 SSTORE 的开销始终至少为 5000 gas。这使得通过 transfer 发起的重入实际上是无害的。

EIP-1283(净 gas 计量)让同一笔交易中对同一存储槽的重复写入变得便宜得多,低至几百 gas。在 Constantinople 升级前不久,安全研究人员证明,这会让接收方能在 2300 的补贴内修改状态,从而对现有合约开辟新的重入攻击。Constantinople 因此被推迟,EIP-1283 在 Petersburg 分叉中被移除(EIP-1716)。

EIP-1706 提出了一种既保留便宜的存储写入、又保留旧有安全假设的办法。

它如何运作

在 SSTORE 的 gas 计算中增加一条规则:

  • 如果执行 SSTORE 时 gasleft 小于或等于 2300,当前调用帧会以 out-of-gas 异常失败。

由于只靠补贴的调用恰好以 2300 gas 开始,它尝试的任何 SSTORE 都会失败,无论这次写入原本有多便宜。依赖补贴的合约保持安全,而带有更多 gas 的普通调用不受影响。

由于 SSTORE 此前从来不可能在少于 5000 gas 的情况下执行,作者认为主网上不会有东西被破坏。

示例代码

这条规则的伪代码,插入在任何其他 SSTORE gas 计费之前:

CALL_STIPEND = 2300

def sstore(evm, key, value):
    if evm.gas_left <= CALL_STIPEND:
        raise OutOfGas("SSTORE with gas left <= stipend")
    # ... normal net-metered SSTORE cost and refund logic follows

它对这种模式为何重要:

// Many contracts assumed `to` cannot change state during this call
payable(to).transfer(amount); // forwards only 2300 gas

需要注意

  • 状态是 Withdrawn。作者们更倾向于 EIP-2200,它重新引入了净 gas 计量,并包含同样的"gasleft ≤ 2300 则失败"哨兵。EIP-2200 在 Istanbul 中上线。
  • 这段插曲是 2300 补贴至今仍被视为 SSTORE 协议级不变量的主要原因。
  • 不要在新代码中把补贴当作安全机制。gas 成本可能变化,现代的指导意见是使用带有显式重入保护的 call,而不是 transfer/send。

#gas #sstore #security #reentrancy #core

详细信息

作者
Alex Forshtat <[email protected]>, Yoav Weiss <[email protected]>
状态
Withdrawn(已撤回)
类型
Standards Track / Core(核心)
创建日期
2019-01-15
依赖的提案

机器可读数据: Markdown · JSON