Skip to content
Audit finding Intermediate

IndexTemplate.sol#compensate() will most certainly fail

The compensate function in IndexTemplate.sol suffers from precision loss when converting between shares and underlying tokens, causing the transferred amount to be less than the required shortage. This leads to a revert in offsetDebt, preventing the pool from resuming and freezing funds.

WatchPug Follow

Originally disclosed on Code4rena

Submitted by WatchPug

Precision loss while converting between the amount of shares and the amount of underlying tokens back and forth is not handled properly.


https://github.com/code-423n4/2022-01-insure/blob/19d1a7819fe7ce795e6d4814e7ddf8b8e1323df3/contracts/IndexTemplate.sol#L438-L447

uint256 _shortage;
if (totalLiquidity() < _amount) {
    //Insolvency case
    _shortage = _amount - _value;
    uint256 _cds = ICDSTemplate(registry.getCDS(address(this)))
        .compensate(_shortage);
    _compensated = _value + _cds;
}
vault.offsetDebt(_compensated, msg.sender);

In the current implementation, when someone tries to resume the market after a pending period ends by calling PoolTemplate.sol#resume(), IndexTemplate.sol#compensate() will be called internally to make a payout. If the index pool is unable to cover the compensation, the CDS pool will then be used to cover the shortage.

However, while CDSTemplate.sol#compensate() takes a parameter for the amount of underlying tokens, it uses vault.transferValue() to transfer corresponding _attributions (shares) instead of underlying tokens.

Due to precision loss, the _attributions transferred in the terms of underlying tokens will most certainly be less than the shortage.

At L444, the contract believes that it’s been compensated for _value + _cds, which is lower than the actual value, due to precision loss.

At L446, when it calls vault.offsetDebt(_compensated, msg.sender), the tx will revert at require(underlyingValue(msg.sender) >= _amount).

As a result, resume() can not be done, and the debt can’t be repaid.

Proof of Concept

Given:

  • vault.underlyingValue = 10,000
  • vault.valueAll = 30,000
  • totalAttributions = 2,000,000
  • _amount = 1,010,000
  • _shortage = _amount - vault.underlyingValue = 1,000,000
  • _attributions = (_amount * totalAttributions) / valueAll = 67,333,333
  • actualValueTransfered = (valueAll * _attributions) / totalAttributions = 1009999

Expected results: actualValueTransfered = _shortage;

Actual results: actualValueTransfered < _shortage.

Impact

The precision loss isn’t just happening on special numbers, but will most certainly always revert the txs.

This will malfunction the contract as the index pool can not compensate(), therefore the pool can not resume(). Causing the funds of the LPs of the pool and the index pool to be frozen, and other stakeholders of the same vault will suffer fund loss from an unfair share of the funds compensated before.

Recommendation

Change to:

https://github.com/code-423n4/2022-01-insure/blob/19d1a7819fe7ce795e6d4814e7ddf8b8e1323df3/contracts/IndexTemplate.sol#L439-L446

if (totalLiquidity() < _amount) {
    //Insolvency case
    _shortage = _amount - _value;
    uint256 _cds = ICDSTemplate(registry.getCDS(address(this)))
        .compensate(_shortage);
    _compensated = vault.underlyingValue(address(this));
}
vault.offsetDebt(_compensated, msg.sender);

oishun1112 (Insure) confirmed and disagreed with severity

oishun1112 (Insure) resolved

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 conversion between shares and underlying tokens uses integer division that truncates, causing a loss of precision.

Impact

The transaction will always revert, preventing the pool from resuming and causing funds to be frozen, with potential unfair loss to stakeholders.

What the attacker needed

Pending period ends; index pool insolvent

How it was found

  1. Call PoolTemplate.sol#resume() to resume the market after a pending period ends.
  2. IndexTemplate.sol#compensate() is called internally to make a payout.
  3. If the index pool cannot cover the compensation, the CDS pool is used to cover the shortage.
  4. CDSTemplate.sol#compensate() transfers shares instead of underlying tokens, causing precision loss.
  5. The transferred value is less than the shortage, so offsetDebt reverts.

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 a pool needs to pay out more than it has, it borrows from another pool. But due to rounding errors, the amount received is slightly less than needed, so the transaction fails. This means the pool can't restart and money gets stuck.

More by WatchPug

View all

197 more disclosed reports, best first.

More from InsureDAO contest

View all

The newest disclosed reports for this program.

Similar reports

Same bug class and techniques, clearest first.