Skip to content
Business logic Audit finding Intermediate

SiloConnector _getPositionTVL miscalculate the TVL position

The SiloConnector's _getPositionTVL function incorrectly calculates the total value locked (TVL) of a position by summing balances of different share tokens and multiplying by the price of the underlying asset without converting the share amounts to underlying tokens. This misvaluation affects the share price at which vault shares are minted or redeemed, potentially allowing value extraction or causing losses for users.

AlexCzm Follow with ArsenLupin, Sentryx and 1 other

Originally disclosed on Code4rena

Submitted by AlexCzm, also found by ArsenLupin, Sentryx, and 0xAlix2

Impact

_getPositionTVL from SiloConnector calculates wrongly the position’s TVL thus affecting the share price at which shares are minted / redeemed from the protocol.

Proof of Concept

Each connector implements a _getPositionTVL used to return the value of funds, in base token, its corresponding protocol holds.

This information is used by AccountingManager to calculate the total assets of the vault and used by 4626 standard to calculate the shares price.

The problem is that SiloConnector miscalculate the value sent to SiloConnector protocol.

    function _getPositionTVL(HoldingPI memory p, address base) public view override returns (uint256 tvl) {
        PositionBP memory bp = registry.getPositionBP(vaultId, p.positionId);
        (address siloToken) = abi.decode(bp.data, (address));
        ISilo silo = ISilo(siloRepository.getSilo(siloToken));
        (address[] memory assets, IBaseSilo.AssetStorage[] memory assetsS) = silo.getAssetsWithState();
        uint256 totalDepositAmount = 0;
        uint256 totalBAmount = 0;
        for (uint256 i = 0; i < assets.length; i++) {
            uint256 depositAmount = IERC20(assetsS[i].collateralToken).balanceOf(address(this));
            depositAmount += IERC20(assetsS[i].collateralOnlyToken).balanceOf(address(this));
            uint256 borrowAmount = IERC20(assetsS[i].debtToken).balanceOf(address(this));
            if (depositAmount == 0 && borrowAmount == 0) {
                continue;
            }
            uint256 price = _getValue(assets[i], base, 1e18);
            totalDepositAmount += depositAmount * price / 1e18;
            totalBAmount += borrowAmount * price / 1e18;
        }
        tvl = totalDepositAmount - totalBAmount;
    }

This function calls silo.getAssetsWithState() to get a list of assets and data associated with them. Then for each asset:

  1. calculates depositAmount as sum of 2 different token balances: collateralToken and collateralOnlyToken
  2. retrieve the borrowAmount as the balance of a 3rd token named debtToken.
  3. get the price of asset (eg. assets[i]) in ‘base’ token
  4. calculates and save the deposited and borrowed amounts

After all assetes have been looped over, the tvl is calculated as the difference between deposits and debt amounts: tvl - totalDepositAmount - totalBAmount

There are 2 problems here:

  1. collateralToken and collateralOnlyToken are 2 different tokens and protocol is summing them as if they represents amounts of the same token.
  2. protocol is multiplying the price of assets[i] by the amounts of shares and not the underlying token (which is assets[i]).

All 3 tokens ( collateralToken, collateralOnlyToken and debtToken) are share tokens with the same underlying asset but have different purposes and different exchange rate.

To help us creating a better picture of what each token represent we can look how AssetStorage struct looks like:

    /// @dev Storage struct that holds all required data for a single token market
    struct AssetStorage {
        /// @dev Token that represents a share in totalDeposits of Silo
        IShareToken collateralToken;
        /// @dev Token that represents a share in collateralOnlyDeposits of Silo
        IShareToken collateralOnlyToken;
        /// @dev Token that represents a share in totalBorrowAmount of Silo
        IShareToken debtToken;

        /// @dev COLLATERAL: Amount of asset token that has been deposited to Silo with interest earned by depositors.
        /// It also includes token amount that has been borrowed.
        uint256 totalDeposits;
        /// @dev COLLATERAL ONLY: Amount of asset token that has been deposited to Silo that can be ONLY used
        /// as collateral. These deposits do NOT earn interest and CANNOT be borrowed.
        uint256 collateralOnlyDeposits;
        /// @dev DEBT: Amount of asset token that has been borrowed with accrued interest.
        uint256 totalBorrowAmount;
    }

To make an analogy, it is like summing peanuts amount with fruits amount and then multiplying it by the peanut butter jelly price.

Recommended Mitigation Steps

In the for loop, after you got the share tokens balances with balanceOf:

  • convert all 3 share tokens balances to underlying token (eg. underlyingTotalDeposits, underlyingCollateralOnlyDeposits, underlyingBorrowAmount)
  • sum underlying deposits : underlyingTotalDeposits = underlyingTotalDeposits + underlyingCollateralOnlyDeposits
  • get the price of the underlying asset
  • calculate the totalDepositAmount and totalBAmount using the calculated underlyingTotalDeposits and underlyingBorrowAmount and price.

HadiEsna (NOYA) confirmed


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

The function sums balances of different share tokens (collateralToken and collateralOnlyToken) and multiplies by the price of the underlying asset without converting the share amounts to the underlying token amounts.

Impact

The miscalculation could affect the share price at which shares are minted or redeemed, potentially leading to incorrect valuations and financial losses for users or the protocol.

What the attacker needed

None

The fix

The function should convert all three share token balances to underlying token amounts, sum the underlying deposits, and then multiply by the price of the underlying asset.

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.

This report is about a smart contract that calculates how much money a user has in a savings protocol. The calculation adds up different types of tokens as if they were the same and uses the wrong price, so the total value is wrong. That wrong value is used to decide how many shares a user gets when they deposit or withdraw, which can be unfair.

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 AlexCzm

View all

3 more disclosed reports, best first.

More from NOYA

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.