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.
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.
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:
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);
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
- Call PoolTemplate.sol#resume() to resume the market after a pending period ends.
- IndexTemplate.sol#compensate() is called internally to make a payout.
- If the index pool cannot cover the compensation, the CDS pool is used to cover the shortage.
- CDSTemplate.sol#compensate() transfers shares instead of underlying tokens, causing precision loss.
- 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.