Skip to content
Business logic Audit finding Intermediate

wrong bToken's exchangeRateStored used for calculate ColleteralValue

The collateral value calculation used the exchange rate of the debt token's bToken instead of the underlying token's bToken, producing an incorrect collateral value. This could lead to incorrect risk assessment and potentially allow positions to be liquidated when they should not be, or vice versa.

mert_eren Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-07-blueberry-judging/issues/117

Found by

mert_eren BlueBerryBank.sol calculate position's risk value with getPositionRisk() . This function used for is position can be liquadatable or not.

Solidity
    function getPositionRisk(uint256 positionId) public returns (uint256 risk) {
        uint256 pv = getPositionValue(positionId);
        uint256 ov = getDebtValue(positionId);
        uint256 cv = getIsolatedCollateralValue(positionId);


        if (
            (cv == 0 && pv == 0 && ov == 0) || pv >= ov /// Closed position or Overcollateralized position
        ) {
            risk = 0;
        } else if (cv == 0) {
            /// Sth bad happened to isolated underlying token
            risk = Constants.DENOMINATOR;
        } else {
            risk = ((ov - pv) * Constants.DENOMINATOR) / cv;
        }
    }

getIsolatedColleteralValue() calculate lended value in position.

Solidity
    function getIsolatedCollateralValue(
        uint256 positionId
    ) public override returns (uint256 icollValue) {
        Position memory pos = positions[positionId];
        /// NOTE: exchangeRateStored has 18 decimals.
        uint256 underlyingAmount;
        if (_isSoftVault(pos.underlyingToken)) {
            underlyingAmount =
                (ICErc20(banks[pos.debtToken].bToken).exchangeRateStored() *
                    pos.underlyingVaultShare) /
                Constants.PRICE_PRECISION;
        } else {
            underlyingAmount = pos.underlyingVaultShare;
        }
        icollValue = oracle.getTokenValue(
            pos.underlyingToken,
            underlyingAmount
        );
    }

For calculate total value it should calculate first underlyingAmount. Due to calculate this, it should first multiply pos.underlyingVaultShare with exchangeRateStored() of this token.Because when stored underlyingVaultShare in lend function, it not stored directly lended amount but the amount minted in softVault after this tokens lended to it.

Solidity
    function lend(
        address token,
        uint256 amount
    ) external override inExec poke(token) onlyWhitelistedToken(token) {
        ...


        if (_isSoftVault(token)) {
            _ensureApprove(token, bank.softVault, amount);
            pos.underlyingVaultShare += ISoftVault(bank.softVault).deposit(
                amount
            );
        } else {
            _ensureApprove(token, bank.hardVault, amount);
            pos.underlyingVaultShare += IHardVault(bank.hardVault).deposit(
                token,
                amount
            );
        }

SoftVault's deposit function call mint function of blueBerry bToken (which work same as compound cToken).And return minted amount of bToken. bToken mintAmount is amount/exchangeRateStored . So when calculate underlyingAmount , pos.UnderlyinVaultShare must be multiplied with btoken of underlyingToken's exchangeRateStored. However as seen above of the getIsolatedColleteralValue() code snippet it multiply with debtToken's bToken not underlyingToken's. Due to all bTokens' has different exchangeRate, underlyingAmount will be calculate wrong.

Vulnerability Detail

Impact

misCalculation of getIsolatedColleteralValue() so cv in getPositionRisk.This will lead some protocols can be

Code Snippet

https://github.com/sherlock-audit/2023-07-blueberry/blob/main/blueberry-core/contracts/BlueBerryBank.sol#L493

Tool used

Manual Review

Recommendation

use underlying token's bToken exchangeRate in getIsolatedColleteralValue() not debtToken's.

Discussion

merteren1234

Escalate I think this issue is not duplicate of #16. Because this issue indicate differeant root cause than #16. This issue's root cause is , wrong bToken's exchangeRate used in getIsolatedColleteral().Not about old exchangeRate used or not. Moreover without fixing this issue , #16 is not valid because pos.debtToken is updated with poke modifier in liquidate function and its exchangeRate used in getIsolatedColleteralValue() . This issue is about getIsolatedColleteralValue() use debtToken's exchangeRate which is wrong because this functions should use underlyingToken's exchangeRate. So even though #16 is fixed (there will be no impact without fix this issue.) this issue still remain and changing ICErc20(banks[pos.debtToken].bToken).exchangeRateStored() with ICErc20(banks[pos.underlyingToken].bToken).exchangeRateStored() is mandatory to take correct getIsolatedColleteralValue().

sherlock-admin2

Escalate I think this issue is not duplicate of #16. Because this issue indicate differeant root cause than #16. This issue's root cause is , wrong bToken's exchangeRate used in getIsolatedColleteral().Not about old exchangeRate used or not. Moreover without fixing this issue , #16 is not valid because pos.debtToken is updated with poke modifier in liquidate function and its exchangeRate used in getIsolatedColleteralValue() . This issue is about getIsolatedColleteralValue() use debtToken's exchangeRate which is wrong because this functions should use underlyingToken's exchangeRate. So even though #16 is fixed (there will be no impact without fix this issue.) this issue still remain and changing ICErc20(banks[pos.debtToken].bToken).exchangeRateStored() with ICErc20(banks[pos.underlyingToken].bToken).exchangeRateStored() is mandatory to take correct getIsolatedColleteralValue().

You've created a valid escalation!

To remove the escalation from consideration: Delete your comment.

You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.

Shogoki

Not a duplicate of #16

hrishibhat

@Shogoki is this a valid issue on its own?

nevillehuang

@Shogoki is this a valid issue on its own?

@Gornutz

hrishibhat

Result: High Unique After further review and discussions considering this a valid high issue as the exchange rate can be affected significantly by the tokens decimal and this is not a duplicate of #16

sherlock-admin2

Escalations have been resolved successfully!

Escalation status:

The original report as published on Sherlock, by mert_eren. 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

The function getIsolatedCollateralValue used the bToken of the debt token to fetch the exchange rate, rather than the bToken of the underlying token.

Impact

This miscalculation could lead to incorrect risk assessment, potentially causing wrongful liquidations or allowing undercollateralized positions to appear safe.

What the attacker needed

None

The fix

The fix is to use the underlying token's bToken exchange rate instead of the debt token's in getIsolatedCollateralValue.

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 protocol, your collateral is valued using an exchange rate. The code mistakenly used the exchange rate of a different token (the one you borrowed) instead of the one you deposited. This gives a wrong collateral value, which can affect whether your position is considered safe or liquidatable.

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 from Blueberry Update #3

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.