Skip to content
Business logic Audit finding Intermediate

Limit index isn't checked in repayDebt, so user control is void

The repayDebt function accepts a limit index meant to protect the borrower against an unfavourable change in the pool's price, but the check enforcing that limit only ran when collateral was also being pulled. A borrower whose strategy depended on the resulting price move could therefore have a repayment execute at a worse level than they intended.

hyh Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-04-ajna-judging/issues/85

Found by

hyh

Summary

repayDebt() resulting LUP _revertIfPriceDroppedBelowLimit() check is not performed in the case of pure debt repayment without collateral pulling.

Vulnerability Detail

LUP will move (up or no change) as a result of debt repayment and repayDebt() have limitIndex_ argument. As a part of multi-position strategy a user might not be satisfied with repay results if LUP has increased not substantially enough.

I.e. there is a user control argument, it is detrimental from UX perspective to request, but not use it, as for any reason a borrower might want to control for that move: they might expect the final level to be somewhere, as an example for the sake of other loans of that borrower.

Impact

Unfavorable repayDebt() operations will be executed and the borrowers, whose strategies were dependent on the realized LUP move, can suffer a loss.

Probability of execution is high (no prerequisites, current ordinary behavior), while the probability of the following loss is medium, so placing the severity to be medium.

Code Snippet

There is a limitIndex_ parameter in repayDebt():

https://github.com/sherlock-audit/2023-04-ajna/blob/main/ajna-core/src/ERC20Pool.sol#L208-L232

Solidity
    function repayDebt(
        address borrowerAddress_,
        uint256 maxQuoteTokenAmountToRepay_,
        uint256 collateralAmountToPull_,
        address collateralReceiver_,
>>      uint256 limitIndex_
    ) external nonReentrant {
        ...

        RepayDebtResult memory result = BorrowerActions.repayDebt(
            auctions,
            buckets,
            deposits,
            loans,
            poolState,
            borrowerAddress_,
            maxQuoteTokenAmountToRepay_,
            collateralAmountToPull_,
>>          limitIndex_
        );

Currently _revertIfPriceDroppedBelowLimit() is done on collateral pulling only:

https://github.com/sherlock-audit/2023-04-ajna/blob/main/ajna-core/src/libraries/external/BorrowerActions.sol#L365-L375

Solidity
        if (vars.pull) {
            // only intended recipient can pull collateral
            if (borrowerAddress_ != msg.sender) revert BorrowerNotSender();

            // an auctioned borrower in not allowed to pull collateral (even if collateralized at the new LUP) if auction is not settled
            if (result_.inAuction) revert AuctionActive();

            // calculate LUP only if it wasn't calculated in repay action
            if (!vars.repay) result_.newLup = Deposits.getLup(deposits_, result_.poolDebt);

>>          _revertIfPriceDroppedBelowLimit(result_.newLup, limitIndex_);

Tool used

Manual Review

Recommendation

Consider adding the same check in the repayment part:

https://github.com/sherlock-audit/2023-04-ajna/blob/main/ajna-core/src/libraries/external/BorrowerActions.sol#L328

Diff
            result_.newLup = Deposits.getLup(deposits_, result_.poolDebt);
+           _revertIfPriceDroppedBelowLimit(result_.newLup, limitIndex_);

If no repay or pull it looks ok to skip the check to save gas.

Discussion

grandizzy

Repayment can only make the position less risky, so don't see a need for frontrunning/stale TX protection. Documentation should clearly state this behavior.

0xffff11

I do see the issue being valid. As sponsor said, they don't see a need to implement a safeguard, but the watson demonstrated that users can suffer a loss if non-favorable repays are executed. Could keep the medium.

grandizzy

re discussed within team and we're going to provide a change for this behavior: https://github.com/ajna-finance/contracts/pull/914

0xffff11

Will keep the medium severity

dmitriia

re discussed within team and we're going to provide a change for this behavior: ajna-finance/contracts#914

Looks ok, limitIndex_ in repayDebt() is now effective in all the cases.

But it looks like when vars.repay == vars.pull == false the check will always revert the call as newPrice_ = result_.newLup == 0:

https://github.com/ajna-finance/contracts/blob/0332f341856e1efe4da8bb675886c8cfbee57b71/src/libraries/helpers/RevertsHelper.sol#L71-L76

Solidity
    function _revertIfPriceDroppedBelowLimit(
        uint256 newPrice_,
        uint256 limitIndex_
    ) pure {
        if (newPrice_ < _priceAt(limitIndex_)) revert LimitIndexExceeded();
    }

Consider:

https://github.com/ajna-finance/contracts/blob/0332f341856e1efe4da8bb675886c8cfbee57b71/src/libraries/external/BorrowerActions.sol#L393-L394

Diff
        // check limit price and revert if price dropped below
-       _revertIfPriceDroppedBelowLimit(result_.newLup, limitIndex_);
+       if (vars.pull || vars.repay) _revertIfPriceDroppedBelowLimit(result_.newLup, limitIndex_);

grandizzy

re discussed within team and we're going to provide a change for this behavior: ajna-finance/contracts#914

Looks ok, limitIndex_ in repayDebt() is now effective in all the cases.

But it looks like when vars.repay == vars.pull == false the check will always revert the call as newPrice_ = result_.newLup == 0:

https://github.com/ajna-finance/contracts/blob/0332f341856e1efe4da8bb675886c8cfbee57b71/src/libraries/helpers/RevertsHelper.sol#L71-L76

Solidity
    function _revertIfPriceDroppedBelowLimit(
        uint256 newPrice_,
        uint256 limitIndex_
    ) pure {
        if (newPrice_ < _priceAt(limitIndex_)) revert LimitIndexExceeded();
    }

Consider:

https://github.com/ajna-finance/contracts/blob/0332f341856e1efe4da8bb675886c8cfbee57b71/src/libraries/external/BorrowerActions.sol#L393-L394

Diff
        // check limit price and revert if price dropped below
-       _revertIfPriceDroppedBelowLimit(result_.newLup, limitIndex_);
+       if (vars.pull || vars.repay) _revertIfPriceDroppedBelowLimit(result_.newLup, limitIndex_);

in that case we early revert with InvalidAmount at L288 https://github.com/ajna-finance/contracts/blob/0332f341856e1efe4da8bb675886c8cfbee57b71/src/libraries/external/BorrowerActions.sol#L288

Solidity
        // revert if no amount to pull or repay
        if (!vars.repay && !vars.pull) revert InvalidAmount();

so later check not needed

dmitriia

Looks ok

The original report as published on Sherlock, by hyh. 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 limit check was placed inside the collateral-pull branch only, so the repayment-only path never compared the new price against the caller's limit index.

Impact

Repayments that the borrower would consider unfavourable could execute, and borrowers whose strategies relied on the realised price move could suffer a loss; the reporter rates this medium because it needs no special conditions but the resulting loss is only moderately likely.

What the attacker needed

None beyond normal pool use

The fix

The team agreed to change the behaviour and a pull request was opened so the limit index takes effect in all cases; a reviewer then noted the check should only run when a repay or pull actually happens, and the team replied that the no-op case already reverts earlier.

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.

In a lending pool, repaying debt changes an internal price that other positions depend on. The function lets the caller set a limit so the transaction fails if that price moves too far, but the code only enforced the limit when collateral was withdrawn at the same time. So a repayment on its own could go through even when the price ended up somewhere the borrower did not want.

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 hyh

View all

137 more disclosed reports, best first.

More from Ajna #2

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.