When the amout of token acquired by a flash loan exceeds the expected value, the callback function will fail
The flash loan callback calculates the borrowed amount from the contract's total balance rather than the balance difference, so a donation to the aggregator before a victim's flash loan inflates the reported amount. This causes the subsequent swap check to fail, denying the victim's flash loan.
Source: https://github.com/sherlock-audit/2024-03-wagmileverage-v2-judging/issues/9
Found by
zraxx
Summary
When the amout of token acquired by a flash loan exceeds the expected value, the callback function will fail.
Vulnerability Detail
The function wagmiLeverageFlashCallback is used to handle the repayment operation after flash loan. After obtaining enough saleToken, it uses _v3SwapExact to convert the saleToken into holdToken. We know that the amount of holdTokens (holdTokenAmtIn) is proportional to the amount of saleTokens (amountToPay) obtained from flash loans. Later, the function will check the holdTokenAmtIn is no large than decodedData.holdTokenDebt.
// Swap tokens to repay the flash loan
uint256 holdTokenAmtIn = _v3SwapExact(
v3SwapExactParams({
isExactInput: false,
fee: decodedData.fee,
tokenIn: decodedData.holdToken,
tokenOut: decodedData.saleToken,
amount: amountToPay
})
);
decodedData.holdTokenDebt -= decodedData.zeroForSaleToken
? decodedData.amounts.amount1
: decodedData.amounts.amount0;
// Check for strict route adherence, revert the transaction if conditions are not met
(decodedData.routes.strict && holdTokenAmtIn > decodedData.holdTokenDebt).revertError(
ErrLib.ErrorCode.SWAP_AFTER_FLASH_LOAN_FAILED
);
In the function _excuteCallback, the amount of token finally obtained by the user through flash loan is flashBalance, which is the balance of the contract.
// Transfer the flashBalance to the recipient
decodedData.saleToken.safeTransfer(decodedDataExt.recipient, flashBalance);
// Invoke the WagmiLeverage callback function with updated parameters
IWagmiLeverageFlashCallback(decodedDataExt.recipient).wagmiLeverageFlashCallback(
flashBalance,
interest,
decodedDataExt.originData
);
Now let me describe how the attacker compromises the flash loans.
First, the attacker makes a donation to the FlashLoanAggregator contract before the victim performs a flash loan (using front-run). Then victim performs a flash loan, and he/she will get much more flashBalance than expected. Finally, in the function wagmiLeverageFlashCallback, the holdTokenAmtIn is larger than experted, which leads to fail.
Impact
DOS
Code Snippet
Tool used
Manual Review
Recommendation
In the function _excuteCallback, the amount of token finally obtained by the user through flash loan should be the the balance difference during the flash loan period.
Discussion
WangSecurity
seems to be working as expected since all these parameters are user supplied (I mean decodedData.routes.strict)
sherlock-admin4
The protocol team fixed this issue in PR/commit https://github.com/RealWagmi/wagmi-leverage/commit/37b0268ec936d63beee873dd61b096b47cd673c1.
WangSecurity
After additional discussions, we decided to make this a valid medium, cause it's a valid attack vector, but takes a lot of funding and additional external factors
spacegliderrrr
Fix looks good, in case aggregator has extra funds, callback will not have to repay for them.
sherlock-admin4
The Lead Senior Watson signed off on the fix.
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 callback uses the contract's absolute token balance instead of the balance difference measured across the flash loan.
Impact
A victim's flash loan could be made to fail, causing a denial of service, though the attack requires significant funding and additional external factors.
What the attacker needed
Attacker can front-run the victim's transaction; attacker has significant token funding to donate; victim performs a flash loan
The fix
The issue was fixed by calculating the amount obtained as the balance difference during the flash loan period.
How it was found
- Attacker donates tokens to the FlashLoanAggregator contract before the victim's flash loan, using front-running.
- Victim performs a flash loan and receives a larger flashBalance than expected.
- In wagmiLeverageFlashCallback, holdTokenAmtIn becomes larger than expected and exceeds holdTokenDebt.
- The strict route check reverts, causing the callback to fail.
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 flash loan is a loan that must be borrowed and repaid within one transaction. The contract here tells the borrower how much it received by looking at its whole balance, not just the change from the loan. If someone sends extra tokens to the contract first, the borrower is told they received more than they did, and a later safety check fails, blocking the loan.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.