Skip to content
Business logic Audit finding Intermediate

executeBuyCreditMarket returns the wrong amount of cash and overestimates the amount that needs to be checked in the variable pool

The executeBuyCreditMarket function returns the gross cash amount rather than the net amount after fees, so the liquidity check overestimates the required variable pool balance. This could cause a buy order to revert unnecessarily when the pool has enough liquidity for the actual net transfer.

said Follow with ether_sky, zhaojohnson and 7 others

Originally disclosed on Code4rena

Submitted by said, also found by ether_sky, zhaojohnson, Honour, samuraii77, inzinko, trachev, alix40, hyh, and KupiaSec

When executeBuyCreditMarket is called and returns the cash amount, it will return the cash amount without deducting the fee. This results in overestimating the value that needs to be validated by validateVariablePoolHasEnoughLiquidity.

Proof of Concept

When executeBuyCreditMarket is called, it will transfer cashAmountIn - fees to the borrower. This means the amount that needs to be available for withdrawal in the variable pool is cashAmountIn - fees.

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

    function executeBuyCreditMarket(State storage state, BuyCreditMarketParams memory params)
        external
>>>     returns (uint256 cashAmountIn)
    {
        // ...

        uint256 creditAmountOut;
        uint256 fees;

        if (params.exactAmountIn) {
            cashAmountIn = params.amount;
            (creditAmountOut, fees) = state.getCreditAmountOut({
                cashAmountIn: cashAmountIn,
                maxCashAmountIn: params.creditPositionId == RESERVED_ID
                    ? cashAmountIn
                    : Math.mulDivUp(creditPosition.credit, PERCENT, PERCENT + ratePerTenor),
                maxCredit: params.creditPositionId == RESERVED_ID
                    ? Math.mulDivDown(cashAmountIn, PERCENT + ratePerTenor, PERCENT)
                    : creditPosition.credit,
                ratePerTenor: ratePerTenor,
                tenor: tenor
            });
        } else {
            creditAmountOut = params.amount;
            (cashAmountIn, fees) = state.getCashAmountIn({
                creditAmountOut: creditAmountOut,
                maxCredit: params.creditPositionId == RESERVED_ID ? creditAmountOut : creditPosition.credit,
                ratePerTenor: ratePerTenor,
                tenor: tenor
            });
        }

       // ...

>>>     state.data.borrowAToken.transferFrom(msg.sender, borrower, cashAmountIn - fees);
        state.data.borrowAToken.transferFrom(msg.sender, state.feeConfig.feeRecipient, fees);
    }

However, executeBuyCreditMarket will return cashAmountIn, and provide it to validateVariablePoolHasEnoughLiquidity and check if the variable pool have cashAmountIn amount of token.

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

    function buyCreditMarket(BuyCreditMarketParams calldata params) external payable override(ISize) whenNotPaused {
        state.validateBuyCreditMarket(params);
>>>     uint256 amount = state.executeBuyCreditMarket(params);
        if (params.creditPositionId == RESERVED_ID) {
            state.validateUserIsNotBelowOpeningLimitBorrowCR(params.borrower);
        }
>>>     state.validateVariablePoolHasEnoughLiquidity(amount);
    }

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

    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);
        }
    }

This will overestimate the amount of liquidity that needs to be available in the variable pool and could cause the call to unnecessarily revert.

Recommended Mitigation Steps

Update the returned cashAmountIn to cashAmountIn - fees at the end of executeBuyCreditMarket.

    function executeBuyCreditMarket(State storage state, BuyCreditMarketParams memory params)
        external
>>>     returns (uint256 cashAmountIn)
    {
        // ...

        uint256 creditAmountOut;
        uint256 fees;

        if (params.exactAmountIn) {
            cashAmountIn = params.amount;
            (creditAmountOut, fees) = state.getCreditAmountOut({
                cashAmountIn: cashAmountIn,
                maxCashAmountIn: params.creditPositionId == RESERVED_ID
                    ? cashAmountIn
                    : Math.mulDivUp(creditPosition.credit, PERCENT, PERCENT + ratePerTenor),
                maxCredit: params.creditPositionId == RESERVED_ID
                    ? Math.mulDivDown(cashAmountIn, PERCENT + ratePerTenor, PERCENT)
                    : creditPosition.credit,
                ratePerTenor: ratePerTenor,
                tenor: tenor
            });
        } else {
            creditAmountOut = params.amount;
            (cashAmountIn, fees) = state.getCashAmountIn({
                creditAmountOut: creditAmountOut,
                maxCredit: params.creditPositionId == RESERVED_ID ? creditAmountOut : creditPosition.credit,
                ratePerTenor: ratePerTenor,
                tenor: tenor
            });
        }

       // ...

        state.data.borrowAToken.transferFrom(msg.sender, borrower, cashAmountIn - fees);
        state.data.borrowAToken.transferFrom(msg.sender, state.feeConfig.feeRecipient, fees);
+       cashAmountIn = cashAmountIn - fees;
    }

Assessed type

Invalid Validation

aviggiano (Size) confirmed and commented:

Fixed in https://github.com/SizeCredit/size-solidity/pull/133.

This is arguably a Low-severity issue since the likelihood is Low

  • Aave being illiquid.
  • Order reverting by a fee amount, which is of the order of a percentage point on the dollar.

Impact is Low (order reverting), but maybe a smaller order could succeed.

hansfriese (judge) commented:

After consideration, keeping it as a valid Medium to remain consistent with #152.

Note: For full discussion, see here.


The original report as published on Code4rena, by said. 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 function returns the gross cash amount instead of the net amount after fees, so the subsequent liquidity check uses an inflated value.

Impact

The overestimated liquidity requirement could cause the buyCreditMarket call to revert unnecessarily, although the likelihood is low because it requires the variable pool to be nearly illiquid.

What the attacker needed

Variable pool nearly illiquid; buy order with fee amount

The fix

The function should return cashAmountIn minus fees so that the liquidity check uses the correct net 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.

When you buy credit, you pay a cash amount and a fee is taken out before the rest goes to the borrower. The contract checks that the pool has enough liquidity using the full cash amount instead of the smaller amount after the fee. This means the check can fail even when there is actually enough money for the real transfer.

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 said

View all

21 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.