feat(checks): ERC-777 reentrancy via tokensReceived hook (closes #19)#26
Merged
Conversation
Adds a check + vulnerable fixture for the ERC-777 tokensReceived reentrancy class — the same vulnerability that drained imBTC and Uniswap V1 in April 2020 ($340K) and several smaller venues since. The bug class is identical to classic ETH reentrancy, but routes through the ERC-777 recipient hook instead of receive(). Tools that look only for low-level receive() callbacks miss it. New files: - src/checks/ERC777ReentrancyCheck.sol — abstract check + attacker - src/examples/VulnerableERC777Vault.sol — fixture: mock ERC-777 token (hook-on-transfer) + vault that pays out before settling - test/ERC777Reentrancy.t.sol — example wiring (excluded from CI forge test per ci.yml --no-match-contract '^(Example|...)'; this is a demo, not an invariant — drained-balance > deposit triggers fail() so the run surfaces the bug) Override hooks parallel ReentrancyCheck.sol: getWithdrawCalldata() / getERC777Token() / getDepositValue() / performDeposit(address, uint256)
Comment on lines
+77
to
+85
| function withdraw() external { | ||
| uint256 owed = deposits[msg.sender]; | ||
| require(owed > 0, "no deposit"); | ||
|
|
||
| // External call (with hook) BEFORE state update — vulnerable. | ||
| token.transfer(msg.sender, owed); | ||
|
|
||
| deposits[msg.sender] = 0; // Too late — attacker already re-entered. | ||
| } |
|
|
||
| /// @dev Attacker contract. Re-enters `withdraw` via the tokensReceived hook. | ||
| contract ERC777ReentrantAttacker is IERC777Recipient { | ||
| address public target; |
| address public target; | ||
| bytes public payload; | ||
| uint256 public reentries; | ||
| uint256 public maxReentries = 2; |
| /// (recipient hook before settlement) are identical. | ||
| contract MockERC777Token { | ||
| mapping(address => uint256) public balanceOf; | ||
| string public name = "Mock 777"; |
| contract MockERC777Token { | ||
| mapping(address => uint256) public balanceOf; | ||
| string public name = "Mock 777"; | ||
| string public symbol = "M777"; |
Comment on lines
+77
to
+85
| function withdraw() external { | ||
| uint256 owed = deposits[msg.sender]; | ||
| require(owed > 0, "no deposit"); | ||
|
|
||
| // External call (with hook) BEFORE state update — vulnerable. | ||
| token.transfer(msg.sender, owed); | ||
|
|
||
| deposits[msg.sender] = 0; // Too late — attacker already re-entered. | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a check + vulnerable fixture for the ERC-777
tokensReceivedreentrancy class — the same bug that drained imBTC and Uniswap V1 in April 2020 (~$340K). It is structurally identical to classic ETH reentrancy but routes through the ERC-777 recipient hook, so detectors that only watch for low-levelreceive()callbacks miss it.Closes #19.
Files
src/checks/ERC777ReentrancyCheck.sol— abstract check, mirrorsReentrancyCheck.sol's shape; attacker reimplementsIERC777Recipient.tokensReceivedsrc/examples/VulnerableERC777Vault.sol— minimalMockERC777Token(callstokensReceivedon contract recipients) +VulnerableERC777Vault(pays before settling balance)test/ERC777Reentrancy.t.sol— example audit harness wiring check ↔ vaultOverride hooks
getWithdrawCalldata()getERC777Token()tokensReceivedis hookedgetDepositValue()performDeposit(address, uint256)CI behavior
ExampleERC777ReentrancyAuditmatches the existing^(Example|TestERC4626)exclusion in.github/workflows/audit.yml. The example is a demo-of-detection (it callsfail()when the vulnerability is detected — that is the check working), not a runnable invariant.forge buildstill validates compilation.Test plan
forge buildsucceedsforge test --no-match-contract '^(Example|TestERC4626)'stays greenforge test --match-contract ExampleERC777ReentrancyAudit -vvvshows the vulnerability detection lognonReentrant) → check passes