Skip to content
Business logic Audit finding Intermediate

Sandwich attack on loan fulfillment will temporarily prevent users from accessing their borrowed funds

The protocol checks that the Aave pool has enough liquidity when a loan is created but does not reserve or remove that liquidity. An attacker can front-run a loan by depositing liquidity and back-run by removing it, leaving the borrower unable to withdraw their borrowed funds until liquidity returns.

gesha17 Follow with grearlake, samuraii77 and 3 others

Originally disclosed on Code4rena

Submitted by gesha17, also found by grearlake, samuraii77, mt030d, KupiaSec, and VAD37

The protocol does not remove borrowed token liquidity from the aave pool once a loan is initiated. It only checks if there is enough available liquidity via the validateVariablePoolHasEnoughLiquidity() function. A user is expected to withdraw their funds after the loan is created. This means that a malicious user can sandwich loans that exceed the available liquidity in aave by depositing liquidity in a frontrun transaction and removing it in a backrun transaction. This way a user that takes out a loan will not be able to remove their funds until there is enough available liquidity in the pool.

Also note that liquidity may become unavailable via other factors, like external users removing liquidity from aave.

Proof of Concept

The sellCreditMarket() function does not remove liquidity from aave, but only checks if there is enough liquidity available via a call to validateVariablePoolHasEnoughLiquidity().

https://github.com/code-423n4/2024-06-size/blob/main/src/Size.sol#L194

    function sellCreditMarket(SellCreditMarketParams memory params) external payable override(ISize) whenNotPaused {
        state.validateSellCreditMarket(params);
        uint256 amount = state.executeSellCreditMarket(params);
        if (params.creditPositionId == RESERVED_ID) {
            state.validateUserIsNotBelowOpeningLimitBorrowCR(msg.sender);
        }
        // @audit validate liquidity after execution??
        state.validateVariablePoolHasEnoughLiquidity(amount);
    }

Also, the executeSellCreditMarket() function transfers the size protocol accounting borrowAToken to the borrower, instead of the underlying token itself, leaving the user to withdraw their borrowed funds in a separate transaction.

https://github.com/code-423n4/2024-06-size/blob/main/src/libraries/actions/SellCreditMarket.sol#L202

    function validateSellCreditMarket(State storage state, SellCreditMarketParams calldata params) external view {
        ...
        state.data.borrowAToken.transferFrom(params.lender, msg.sender, cashAmountOut);
        state.data.borrowAToken.transferFrom(params.lender, state.feeConfig.feeRecipient, fees);
    }

The validateVariablePoolHasEnoughLiquidity() function only checks the balance of the variable pool, and does not remove liquidity:

https://github.com/code-423n4/2024-06-size/blob/main/src/libraries/CapsLibrary.sol#L67

    function validateVariablePoolHasEnoughLiquidity(State storage state, uint256 amount) public view {
        uint256 liquidity = state.data.underlyingBorrowToken.balanceOf(address(state.data.variablePool));
        if (liquidity < amount) {
            revert Errors.NOT_ENOUGH_BORROW_ATOKEN_LIQUIDITY(liquidity, amount);
        }
    }

Recommended Mitigation Steps

Remove the liquidity from the pool when the loan is created and still let the user pull their funds in a separate transaction to remain compliant with pull over push pattern.

aviggiano (Size) acknowledged and commented:

We acknowledge this issue, but we understand that MEV attacks like frontrunning, sandwich attacks, etc., do not belong to the protocol layer. For example, the on-chain Uniswap logic can’t do anything against sandwich attacks.

Also, this can be mitigated with a multicall.

I am not sure how Code4rena judges will weigh in on this issue, but since it was not explicitly stated on the list of Known Limitations, we have decided to make it more explicit in our documentation here.

hansfriese (judge) commented:

Will keep as Medium since it accurately highlights the irrationality of the current validation process.


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

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 protocol validates available liquidity without reserving or transferring the underlying tokens at loan creation, leaving the borrower to withdraw later from a pool whose liquidity can change.

Impact

A borrower may be temporarily unable to access their borrowed funds; the text notes this can also happen through normal liquidity changes, and the protocol team argues it is an MEV issue that can be mitigated with a multicall.

What the attacker needed

Attacker can front-run and back-run transactions; loan amount exceeds available Aave liquidity

The fix

The recommended mitigation is to remove the liquidity from the pool when the loan is created while still letting the user pull their funds in a separate transaction.

How it was found

  1. Attacker deposits liquidity into the Aave pool before the victim's loan transaction.
  2. Victim creates a loan that passes the liquidity check because of the attacker's deposit.
  3. Attacker removes their liquidity in a backrun transaction.
  4. Victim cannot withdraw their borrowed funds because the pool lacks sufficient liquidity.

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.

When you take out a loan, the system only checks that the lending pool has enough money available, but it does not set that money aside for you. An attacker can temporarily add money to the pool before your loan, then take it back right after, so when you try to withdraw your loan there is not enough left. This is like a shop checking it has an item in stock but not reserving it for you, so someone else can buy it first.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by gesha17

View all

4 more disclosed reports, best first.

More from Size

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.