← COMP5565 全部讲次
Q09

期中模拟卷 B|重入推演 21

现有材料范围内的非官方复习,含英文术语、成立条件与自拟练习解析。

手机上宽表格可左右滑动;自测答案默认收起,点击题目下方按钮查看。

非官方自拟模拟卷 B,正式考试范围、题型、时长和开闭卷要求以讲师通知为准。 按现有期中前密码学基础、L01-L05 与 Lab 1 编写,借鉴所提供往年期末卷的可见设问结构;不加入期中后专题。本卷不是往年期中真题或押题。

全卷 100 分,建议练习 100 分钟。Part A 为自拟单选 15 题共 30 分;Part B 共 70 分。原参考文件没有选择题正文,不能据此预测实际选择规则。每题答案和给分点默认收起,自评分不等于教师评分。

统一前提:Solidity 0.8.30、默认 checked arithmetic;采用普通 EOA 交易模型,不讨论账户代码委托。代码仅用于教学;除题目说明外,gas 充足、无隐藏操作、无额外费用。计算按题面精确数值。

读题词:attacker = 攻击者;justify = 给出原因;at least = 至少;strictly = 严格;resulting state = 结果状态。问题中的“攻击者”不是哈希性质的名称。

来源代号:C01 是密码学基础;L01-L05 对应逐讲注解。答案回查页码均为原 PDF 页序。英文同义表达与机制正确的替代答案可以成立。

B 卷导航:单选题 1-8 · 单选题 9-15 · 机制与应用 16-20 · 重入推演 21 · 升级 状态与钱包 22-24

前一套复习题入口 · 复习资料

重入推演 21

B21 · Q21 Trace the code before judging the exploit [16 marks]

contract RefundPool {
    mapping(address => uint256) public credit;

    function deposit() external payable {
        credit[msg.sender] += msg.value;
    }

    function withdraw(uint256 amount) external {
        require(credit[msg.sender] >= amount, "credit");
        (bool ok, ) = msg.sender.call{value: amount}("");
        require(ok, "payment");
        credit[msg.sender] -= amount;
    }
}

All deposits happened in earlier, successful transactions. Immediately before the attack, the pool holds 10 ETH: honest users deposited 8 ETH and attacker contract A deposited 2 ETH. credit[A] is 2 ETH and A’s actual ETH balance is zero.

In a new transaction, A calls withdraw(2 ether). On its first receipt of ETH, A sets an internal flag and makes exactly one additional high-level call to withdraw(2 ether). On its second receipt it does nothing. A does not catch a failed reentrant call. There are no other calls. Ignore gas costs when reporting balances.

(a) [4 marks] Trace the two withdrawals and the two attempted credit deductions in execution order. State what the inner call sees and whether the outer deduction succeeds under Solidity 0.8.30.

(b) [4 marks] Give the final transaction status, pool ETH balance, A’s ETH balance, and credit[A]. Explain why deposits from earlier transactions are or are not affected.

(c) [4 marks] Start again from the stated initial balances. Change only the final statement to credit[msg.sender] = 0;. Give the resulting pool balance, A’s balance, A’s credit, and A’s profit relative to its earlier 2 ETH deposit. Explain the accounting problem.

(d) [4 marks] Return to the original checked-subtraction version. Show a Checks-Effects-Interactions repair. With the same callback that does not catch failure, does the attack transaction succeed after this repair? Briefly explain how the outcome could differ if A handles the rejected reentrant call and lets its callback return normally.

B21 · 查看答案与解析

(a) Call order [4 marks]

推理过程:credit 是账面可提款额,Pool ETH 与 A ETH 是实际钱,三列分开。外层付款后还没扣 credit,就进入 A 的回调;回调再提款一次,内层先结束,把 credit 从 2 扣到 0。随后外层恢复执行,再算 0 - 2,触发 underflow(下溢)。Solidity 0.8.30 默认检查无符号整数范围,因此不会留下负数余额。

Step Pool ETH A ETH credit[A] What happens
Initial 10 0 2 Earlier deposits already committed
Outer transfer 8 2 2 Credit has not yet been deducted
Inner transfer 6 4 2 Reentrant check still sees 2
Inner deduction 6 4 0 2 - 2 succeeds
Outer deduction - - - Attempts 0 - 2; checked underflow

表中金额以 ETH 表示;代码的整数以 wei 表示。最后一行是异常点,不是允许出现负余额。

English: Both checks initially see the old credit. The inner withdrawal deducts the credit first. When the outer frame resumes, its subtraction underflows under Solidity 0.8.30, reverting the top-level attack transaction.

给分:旧 credit 可重入 1;两次转账顺序 1;内层先扣为零 1;外层 underflow 导致回滚 1。

(b) Final state [4 marks]

推理过程:从失败位置向外追:外层扣减报错 → 整笔攻击交易失败 → 这笔交易里两次 ETH 转账、内层已完成的 credit 扣减一并撤销。恢复点是“本次攻击开始前”,不是合约刚部署时。之前成功交易中的存款不回滚,因此恢复 Pool 10、A 0、credit 2。

English: The transaction reverts. The pool ends with 10 ETH, A with 0 ETH, and credit[A] with 2 ETH. The successful inner call’s effects are also undone when its enclosing transaction reverts. Earlier deposits are not undone; the state returns to the state immediately before this attack transaction.

给分:status 失败 1;pool = 10 且旧存款保留 1;A = 0 1;credit[A] = 2 1。题设忽略 gas,因此不要把发起交易 EOA 的 gas 支出混进 A 合约余额。

(c) Replacing subtraction with assignment [4 marks]

推理过程:先重新从题设初始状态开始,不沿用上一小问的中间值。两次付款仍先发生,Pool 10 → 8 → 6,A 0 → 2 → 4。返回时内层写 0,外层再写 0,赋值没有 0 - 2 的下溢,所以能够成功。A 收到 4,但其中 2 是原来自己的本金,净利润才是 2。

English: The two transfers now succeed. Each returning frame assigns zero, so there is no subtraction underflow. The pool ends with 6 ETH, A with 4 ETH, and A’s credit with 0. Relative to A’s earlier 2 ETH deposit, its net profit is 2 ETH, ignoring gas. The same credit was used twice before being cleared.

给分:pool = 6 1;A = 4 1;credit = 0 1;profit = 2 并解释重复使用旧 credit 1。收到 4 不是净赚 4。本题不能凭看到 call 就机械判“必能获利”:必须先判断版本、记账方式与回滚传播。

(d) CEI and failure propagation [4 marks]

推理过程:CEI 统一读作“检查 → 更新状态 → 外部交互”。先扣 credit 后再付款,回调再次进入时已看到 0,检查失败。题设不捕获这个失败,它使收款回调失败,再使付款 call 返回 false,最后 require(ok) 把外层也回滚。若 A 捕获内层失败并正常返回,外层付款成功,便只完成一次合法提款。

function withdraw(uint256 amount) external {
    require(credit[msg.sender] >= amount, "credit");
    credit[msg.sender] -= amount;
    (bool ok, ) = msg.sender.call{value: amount}("");
    require(ok, "payment");
}

English: Credit is deducted before control leaves the pool. Reentry now fails its credit check. With the stated uncaught high-level callback, the payment callback fails, ok is false, and the outer withdrawal reverts: pool 10, A 0, credit 2. If A instead catches the failed reentrant call and returns normally, the single legitimate withdrawal can succeed: pool 8, A 2, credit 0.

给分:正确 CEI 顺序 2;原回调导致全笔回滚 1;捕获失败后合法提款可成功 1。CEI 修复本题的记账顺序,不代表对整份业务合约的安全证明。回查:L04b p.55-59、p.66-74 与重入补充。

下一页 →
Q10 期中模拟卷 B|升级 状态与钱包 22-24

个人整理的学习笔记,不是官方材料;数字与结论以课件和讲师为准。