Skip to content
Business logic Audit finding Intermediate

Wrong formula of getSharesForAmount() can potentially cause fund loss when being used to calculate the shares to be used in withdraw()

The getter functions getAmountForShares() and getSharesForAmount() use totalAssets(), which includes the contract's base token balance, instead of the strategy controller's total value. An attacker using these functions to calculate shares for a withdrawal could receive less base token than expected, causing fund loss.

WatchPug Follow

Originally disclosed on Code4rena

Submitted by WatchPug

In Collateral, the getter functions getAmountForShares() and getSharesForAmount() is using totalAssets() instead of _strategyController.totalValue(), making the results can be different than the actual shares amount needed to withdraw() a certain amount of _baseToken and the amount of shares expected to get by deposit() a certain amount.

Specifically, totalAssets() includes the extra amount of _baseToken.balanceOf(Collateral).

Collateral.sol#L306-L329

function getAmountForShares(uint256 _shares)
    external
    view
    override
    returns (uint256)
{
    if (totalSupply() == 0) {
        return _shares;
    }
    return (_shares * totalAssets()) / totalSupply();
}

function getSharesForAmount(uint256 _amount)
    external
    view
    override
    returns (uint256)
{
    uint256 _totalAssets = totalAssets();
    return
        (_totalAssets > 0)
            ? ((_amount * totalSupply()) / _totalAssets)
            : 0;
}

Collateral.sol#L339-L343

function totalAssets() public view override returns (uint256) {
    return
        _baseToken.balanceOf(address(this)) +
        _strategyController.totalValue();
}

Collateral.sol#L137-L148

function withdraw(uint256 _amount)
    external
    override
    nonReentrant
    returns (uint256)
{
    require(_withdrawalsAllowed, "Withdrawals not allowed");
    if (_delayedWithdrawalExpiry != 0) {
        _processDelayedWithdrawal(msg.sender, _amount);
    }
    uint256 _owed = (_strategyController.totalValue() * _amount) /
        totalSupply();
    ...

Proof of Concept

Given:

  • _baseToken.balanceOf(Collateral) == 90
  • _strategyController.totalValue() == 110
  • totalSupply of shares = 100

totalAssets() returns: 200

getSharesForAmount(100) returns: 50, while withdraw(50) will actual only get: 55.

When Collateral is used by another contract that manages many users’ funds, and if it’s using getSharesForAmount() to calculate the amount of shares needed for a certain amount of underlying tokens to be withdrawn.

This issue can potentially cause fund loss to the user of that contract because it will actually send a lesser amount of _baseToken than expected.

Recommended Mitigation Steps

Consider changing Collateral.totalValue() to:

function totalAssets() public view override returns (uint256) {
    return
        _strategyController.totalValue();
}

ramenforbreakfast (prePO) confirmed and commented:

Well organized and thorough submission. This is a valid claim.

gzeon (judge) commented:

Agree with sponsor.


The original report as published on Code4rena, by WatchPug. 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 getter functions use totalAssets() (which adds the contract's base token balance) while withdraw() uses only _strategyController.totalValue(), creating a mismatch in share-to-asset conversion.

Impact

A contract using getSharesForAmount() to calculate shares for a withdrawal could cause users to receive less base token than expected, potentially leading to fund loss.

What the attacker needed

Another contract uses getSharesForAmount(); that contract manages many users' funds

The fix

The getter functions should use _strategyController.totalValue() instead of totalAssets(), or totalAssets() should be changed to return only the strategy controller's total value.

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 is a smart contract bug where two helper functions calculate how many shares correspond to a given amount of tokens. They use a total that includes tokens sitting idle in the contract, but the actual withdrawal function only counts tokens managed by the strategy. So if another contract relies on these helpers, it may ask for too few shares and the user gets less money than they should.

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 WatchPug

View all

197 more disclosed reports, best first.

More from prePO contest

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.