Skip to content
DoS Audit finding Intermediate

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.

zraxx Follow

Originally disclosed on Sherlock

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.

Solidity
// 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.

Solidity
// 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

https://github.com/sherlock-audit/2024-03-wagmileverage-v2/blob/main/wagmi-leverage/contracts/FlashLoanAggregator.sol#L553-L560

https://github.com/sherlock-audit/2024-03-wagmileverage-v2/blob/main/wagmi-leverage/contracts/abstract/LiquidityManager.sol#L460-L478

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.

The original report as published on Sherlock, by zraxx. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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

  1. Attacker donates tokens to the FlashLoanAggregator contract before the victim's flash loan, using front-running.
  2. Victim performs a flash loan and receives a larger flashBalance than expected.
  3. In wagmiLeverageFlashCallback, holdTokenAmtIn becomes larger than expected and exceeds holdTokenDebt.
  4. 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.

More by zraxx

View all

1 more disclosed report, best first.

More from WagmiLeverage V2

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.