Classic Reentrancy Attack (The DAO Pattern)
In a reentrancy exploit, a malicious recipient contract intercepts an external ETH or token call via fallback() or receive() and recurses back into the vulnerable withdraw function before the user balance state is decremented.
Vulnerable ImplementationExternal call before state update function withdraw(uint256 amount) external {
require(balances[msg.sender] >= amount);
// ❌ VULNERABLE: External call BEFORE state update!
(bool ok, ) = msg.sender.call{value: amount}("");
require(ok, "Transfer failed");
// State updated too late:
balances[msg.sender] -= amount;
} Nexus Defense: CEI + NonReentrantOpenZeppelin Standard function withdraw(uint256 amount) external nonReentrant {
require(balances[msg.sender] >= amount);
// ✅ CHECKS-EFFECTS-INTERACTIONS:
// 1. Effects: State modified BEFORE external call
balances[msg.sender] -= amount;
// 2. Interactions: Safe transfer with mutex lock
(bool ok, ) = msg.sender.call{value: amount}("");
require(ok, "Transfer failed");
}Protocol Verification Proof:Foundry invariant fuzz tests in test/NexusLending.t.sol execute 10,000 randomized recursive reentrant callbacks. 100% of reentrancy attempts revert with ReentrancyGuardReentrantCall().