Reentrancy on Flash Governance Proposal Withdrawal
The withdrawGovernanceAsset function sends the asset to the caller before deleting the pending decision record, so a token that hands control back during the transfer lets the attacker withdraw repeatedly. An attacker could drain the contract's balance of the configured asset.
Originally disclosed on Code4rena
Submitted by kirk-baird
The function withdrawGovernanceAsset() is vulnerable to reentrancy, which would allow the attacker to drain the balance of the flashGoverananceConfig.asset.
Note: this attack assumes the attacker may gain control of the execution flow in asset.tranfer() which is the case for many ERC20 tokens such as those that implement ERC777 but will depend on which asset is chosen in the configuration.
Proof of Concept
withdrawGovernanceAsset() does not follow the check-effects-interactions pattern as seen from the following code snippet, where an external call is made before state modifications.
function withdrawGovernanceAsset(address targetContract, address asset) public virtual {
require(
pendingFlashDecision[targetContract][msg.sender].asset == asset &&
pendingFlashDecision[targetContract][msg.sender].amount > 0 &&
pendingFlashDecision[targetContract][msg.sender].unlockTime < block.timestamp,
"Limbo: Flashgovernance decision pending."
);
IERC20(pendingFlashDecision[targetContract][msg.sender].asset).transfer(
msg.sender,
pendingFlashDecision[targetContract][msg.sender].amount
);
delete pendingFlashDecision[targetContract][msg.sender];
}
The attacker can exploit this vulnerability through the following steps:
assertGovernanceApproved(userA, target, false)- wait for
unlockTimeseconds to pass withdrawGovernanceAsset(target, asset)and gain control of the execution duringasset.transfer()- repeat step 3) until there balance of
FlashGovernanceArbiteris less thanpendingFlashDecision[target][msg.sender].amount
Recommended Mitigation Steps
There are two possible mitigations, the first is to implement the check-effects-interactions patter. This involves doing as checks and state changes before making external calls. To implement this in the current context delete the pendingFlashDecision before making the external call as follows.
function withdrawGovernanceAsset(address targetContract, address asset) public virtual {
require(
pendingFlashDecision[targetContract][msg.sender].asset == asset &&
pendingFlashDecision[targetContract][msg.sender].amount > 0 &&
pendingFlashDecision[targetContract][msg.sender].unlockTime < block.timestamp,
"Limbo: Flashgovernance decision pending."
);
uint256 amount = pendingFlashDecision[targetContract][msg.sender].amount;
IERC20 asset = IERC20(pendingFlashDecision[targetContract][msg.sender].asset);
delete pendingFlashDecision[targetContract][msg.sender];
asset.transfer(msg.sender, amount);
}
gititGoro (Behodler) acknowledged and commented:
I do mention in the documentation that the only eligible assets are EYE and EYE LPs but that rule isn’t enforced on a contract level which is why I’m acknowledging this rather than disputing it. Nonetheless it’s not relevant to the context of Limbo
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the function performs the external token transfer before deleting the pending decision entry, violating the check-effects-interactions order.
Impact
An attacker could drain the contract's balance of the configured asset, but only if that asset lets the recipient run code during the transfer, such as an ERC777 token.
What the attacker needed
Attacker can approve a governance decision; unlock time has passed; asset runs code on transfer (e.g. ERC777)
The fix
The pending decision should be deleted before the external transfer is made, following the check-effects-interactions pattern.
How it was found
- Approve a governance decision for the attacker's address on a target contract.
- Wait until the recorded unlock time has passed.
- Call withdrawGovernanceAsset and take control of execution during the token transfer.
- Repeat the withdrawal until the contract's balance is below the recorded amount.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A smart contract keeps a record of what each user is owed and pays it out. Here it pays first and only then clears the record, so if the token being paid can run code in the middle of the payment, the attacker can ask to be paid again before the record is cleared. Each repeat pays out the same amount until the contract runs out of that token.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.