Skip to content
Smart contracts Audit finding Intermediate

User fund loss in supplyTokenTo() because of rounding

The supplyTokenTo() function calculates shares from the deposit amount using integer division, which rounds down. When the share price is high, a user can receive fewer shares than the value of their deposit, losing part of their funds.

unforgiven Follow

Originally disclosed on Code4rena

Submitted by unforgiven, also found by 0x1f8b

AaveV3YieldSource.sol#L231-L242 AaveV3YieldSource.sol#L357-L362

When user use supplyTokenTo() to deposit his tokens and get share in FeildSource because of rounding in division user gets lower amount of share. for example if token’s _decimal was 1 and totalSupply() was 1000 and aToken.balanceOf(FieldSource.address) was 2100 (becasue of profits in Aave Pool balance is higher than supply), then if user deposit 4 token to the contract with supplyTokenTo(), contract is going to mint only 1 share for that user and if user calls YeildToken.balanceOf(user) the return value is going to be 2 and user already lost half of his deposit. Of course if _precision was high this loss is going to be low enough to ignore but in case of low _precision and high price token and high balance / supply ratio this loss is going to be noticeable.

Proof of Concept

This is the code of supplyTokenTo():

  function supplyTokenTo(uint256 _depositAmount, address _to) external override nonReentrant {
    uint256 _shares = _tokenToShares(_depositAmount);
    require(_shares > 0, "AaveV3YS/shares-gt-zero");

    address _underlyingAssetAddress = _tokenAddress();
    IERC20(_underlyingAssetAddress).safeTransferFrom(msg.sender, address(this), _depositAmount);
    _pool().supply(_underlyingAssetAddress, _depositAmount, address(this), REFERRAL_CODE);

    _mint(_to, _shares);

    emit SuppliedTokenTo(msg.sender, _shares, _depositAmount, _to);
  }

which in line: _shares = _tokenToShares(_depositAmount) trying to calculated shares corresponding to the number of tokens supplied. and then transfer _depositAmount from user and mint shares amount for user. the problem is that if user convert _shares to token, he is going to receive lower amount because in most cases:

_depositAmount > _sharesToToken(_tokenToShares(_depositAmount))

and that’s because of rounding in division. Value of _shares is less than _depositAmount. so YeildSource should only take part of _depositAmount that equals to _sharesToToken(_tokenToShares(_depositAmount)) and mint _share for user.

Of course if _precision was high and aToken.balanceOf(FieldSource.address) / totalSupply() was low, then this amount will be insignificant, but for some cases it can be harmful for users. for example this conditions:

  • _perecision is low like 1 or 2.
  • token value is very high like BTC.
  • aToken.balanceOf(FieldSource.address) / totalSupply() is high due to manipulation or profit in Aave pool.

Tools Used

VIM

Recommended Mitigation Steps

To resolve this issue this can be done:

  function supplyTokenTo(uint256 _depositAmount, address _to) external override nonReentrant {
    uint256 _shares = _tokenToShares(_depositAmount);
    require(_shares > 0, "AaveV3YS/shares-gt-zero");

    _depositAmount = _sharesToToken(_shares); // added hero to only take correct amount of user tokens
    address _underlyingAssetAddress = _tokenAddress();
    IERC20(_underlyingAssetAddress).safeTransferFrom(msg.sender, address(this), _depositAmount);
    _pool().supply(_underlyingAssetAddress, _depositAmount, address(this), REFERRAL_CODE);

    _mint(_to, _shares);

    emit SuppliedTokenTo(msg.sender, _shares, _depositAmount, _to);
  }

PierrickGT (PoolTogether) confirmed and commented:

PR: https://github.com/pooltogether/aave-v3-yield-source/pull/16

Justin Goro (judge) decreased severity from High to Medium


The original report as published on Code4rena, by unforgiven. 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 contract mints shares based on a rounded-down division but still takes the full deposit amount, so the user pays more tokens than the minted shares are worth.

Impact

Users could lose a portion of their deposit when the token has low decimals and the share price is high; the loss is small when precision is high.

What the attacker needed

Low token decimals (e.g. 1 or 2); high share price or balance/supply ratio; user deposits via supplyTokenTo()

The fix

The contract should recalculate the deposit amount from the minted shares so it only takes the tokens that correspond to those shares.

How it was found

  1. Set the token decimals to a low value such as 1 and ensure the share price is high.
  2. Call supplyTokenTo() with a deposit amount that does not divide evenly into shares.
  3. Observe that the contract mints fewer shares than the deposit is worth, causing a loss.

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 deposit tokens into a savings contract, you get 'shares' that represent your portion of the pool. The contract calculates shares by dividing your deposit by the current share price, but whole-number division always rounds down. If the share price is high, that rounding can make you lose a noticeable part of your deposit.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by unforgiven

View all

50 more disclosed reports, best first.

More from PoolTogether

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.