Skip to content
Audit finding Intermediate

Loss of precision leads to under-collateralized

A rounding-down error in the yield vault's share calculation caused the vault to appear under-collateralized by one wei after a normal deposit. This could wrongly put the vault into a withdraw-only mode, blocking further deposits.

bin2chen Follow

Originally disclosed on Code4rena

Submitted by bin2chen

Since _yieldVault mostly calculates shares and uses round down when depositing, there is often a 1 wei loss of precision, which can cause the vault to go into under-collateralized mode by mistake.

Proof of Concept

When a user deposits an asset, we update _lastRecordedExchangeRate; the calculation is done by method _currentExchangeRate().

The code is as follows:

  function _currentExchangeRate() internal view returns (uint256) {
    uint256 _totalSupplyAmount = _totalSupply();
    uint256 _totalSupplyToAssets = _convertToAssets(
      _totalSupplyAmount,
      _lastRecordedExchangeRate,
      Math.Rounding.Down
    );

@>  uint256 _withdrawableAssets = _yieldVault.maxWithdraw(address(this));

    if (_withdrawableAssets > _totalSupplyToAssets) {
      _withdrawableAssets = _withdrawableAssets - (_withdrawableAssets - _totalSupplyToAssets);
    }

    if (_totalSupplyAmount != 0 && _withdrawableAssets != 0) {
      return _withdrawableAssets.mulDiv(_assetUnit, _totalSupplyAmount, Math.Rounding.Down);
    }

    return _assetUnit;
  }

This method takes _yieldVault.maxWithdraw(address(this)) as the maximum value to calculate the current exchange rate. If the exchange rate is lower than _assetUnit, then it goes into an under-collateralized mode, where it can only be withdrawn, not deposited. So if _yieldVault is losing money, it goes into under-collateralized.

But there is one missing consideration here:

As long as _yieldVault is not exclusive, there will be precision loss issues. After _yieldVault.deposit(), maxWithdraw() will lose precision because most vaults will “round down” the shares calculations. For example, depositing 1000000000; however, it can only withdraw 999999999.

This leads to the problem that when the first deposit is made, it is likely to go into an under-collateralized mode immediately due to the 1 wei loss.

The following code demonstrates that when a non-exclusive _yieldVault, once Alice first deposits normally, it immediately enters an under-collateralized mode.

Add to Deposit.t.sol:

  function testLossPrecision() external {
    vm.startPrank(alice);

    //0.Constructing a yieldVault that is already profitable
    uint256 _amount = 333e18;
    underlyingAsset.mint(alice, _amount);
    underlyingAsset.approve(address(yieldVault), type(uint256).max);
    yieldVault.deposit(_amount,alice);
    //profitable 0.1e18
    underlyingAsset.mint(address(yieldVault), 0.1e18);

    //1.alice deposit
    _amount = 100e18;
    underlyingAsset.mint(alice, _amount);
    underlyingAsset.approve(address(vault), type(uint256).max);
    console2.log("deposit:",_amount);
    vault.deposit(_amount, alice);
    console2.log("maxWithdraw:",yieldVault.maxWithdraw(address(vault)));
    console2.log("loss :",_amount - yieldVault.maxWithdraw(address(vault)));
    console2.log("isVaultCollateralized:",vault.isVaultCollateralized());
    return;
   }
$ forge test --match-test testLossPrecision -vvv

[PASS] testLossPrecision() (gas: 402881)
Logs:
  deposit: 100000000000000000000
  maxWithdraw: 99999999999999999999
  loss : 1
  isVaultCollateralized: false

This small loss of precision should not be treated as a loss. We can avoid it by adding 1wei when calculating the exchange rate.

Recommended Mitigation Steps

Have _yieldVault.maxWithdraw() + 1 to avoid loss of precision into an under-collateralized mode.

  function _currentExchangeRate() internal view returns (uint256) {
    uint256 _totalSupplyAmount = _totalSupply();
    uint256 _totalSupplyToAssets = _convertToAssets(
      _totalSupplyAmount,
      _lastRecordedExchangeRate,
      Math.Rounding.Down
    );

-   uint256 _withdrawableAssets = _yieldVault.maxWithdraw(address(this));
+   uint256 _withdrawableAssets = _yieldVault.maxWithdraw(address(this)) + 1;

    if (_withdrawableAssets > _totalSupplyToAssets) {
      _withdrawableAssets = _withdrawableAssets - (_withdrawableAssets - _totalSupplyToAssets);
    }

    if (_totalSupplyAmount != 0 && _withdrawableAssets != 0) {
      return _withdrawableAssets.mulDiv(_assetUnit, _totalSupplyAmount, Math.Rounding.Down);
    }

    return _assetUnit;
  }

Assessed type

Decimal

asselstine (PoolTogether) confirmed

PierrickGT (PoolTogether) commented:

Nice catch. Since we don’t have any control over the YieldVault exchange rate, if it gets manipulated at any time, we should revert any deposits where the amount of withdrawable assets from the YieldVault is lower than the expected amount.

PoolTogether mitigated:

Improved vault share / asset calculation. PR: https://github.com/GenerationSoftware/pt-v5-vault/pull/18

Status: Not fully mitigated. Full details in report from dirk_y and 0xStalin, and in the Mitigation Review section below.


The original report as published on Code4rena, by bin2chen. 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 exchange rate calculation used the yield vault's maxWithdraw value without accounting for the one-wei rounding loss from share conversions.

Impact

The vault could enter an under-collateralized mode where deposits are blocked, even though the loss is only one wei.

What the attacker needed

A non-exclusive yield vault; a normal deposit

The fix

The exchange rate calculation should add one wei to the maxWithdraw value to avoid the rounding loss; the project later improved the share/asset calculation but the report notes it was not fully mitigated.

How it was found

  1. Set up a yield vault that already holds some assets and has a small profit.
  2. Alice deposits a normal amount into the main vault.
  3. Check the yield vault's maxWithdraw for the main vault and compare it to the deposit amount.
  4. Observe that maxWithdraw is one wei less than the deposit, and the vault reports itself as not collateralized.

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 money into a savings vault, the vault gives you shares representing your portion. Because of how computers round numbers down, the vault may think it has one tiny unit (a wei, the smallest unit of cryptocurrency) less than it really does. That tiny difference made the system think it was short of funds and switch to a mode where no one could deposit more.

More by bin2chen

View all

139 more disclosed reports, best first.

More from PoolTogether

View all

The newest disclosed reports for this program.

Similar reports

Same bug class and techniques, clearest first.