P can be updated to zero which can cause a DOS when liquidating troves
The product factor P in the stability pool can become zero after a few liquidations with small product factors, despite an assertion that it should never be zero. This causes a denial of service for certain liquidations, preventing the protocol from clearing bad debt.
Submitted by koxuan, also found by fs0c
P is asserted to never be zero in _updateRewardSumAndProduct which is called for every liquidation. However, there is an edge case that can cause P to be zero, causing DOS to certain liquidations.
Proof of Concept
In _updateRewardSumAndProduct notice assert(newP > 0);. However, the SCALE_FACTOR check is insufficient in ensuring P is always more than zero. Three cycles of very small P can cause P to drop to zero and hence causing revert for that particular liquidation of troves. POC steps are as follows,
- First liquidation newProductFactor is 1e2,
1e18*1e2/1e18 = 1e2, since its less than scale factor. 1e9 is multiplied before dividing with 1e18. P = 1e11 after 1st liquidation cycle. - Second liquidation newProductFactor is 1e3,
1e11*1e3/1e18 = 0, since its less than scale factor. 1e9 is multipled. P = 1e5 - Third liquidation newProductFactor is 1e3,
1e5*1e3/1e8 = 0, since its less than scale factor, 1e9 is multipled. However, even after 1e9 is multiplied it will result in 1e17, which is less than DECIMAL_PRECISION of 1e18. P = 0. - Revert due to
assert(newP > 0);.
// If the Stability Pool was emptied, increment the epoch, and reset the scale and product P
if (newProductFactor == 0) {
currentEpoch = currentEpochCached.add(1);
emit EpochUpdated(currentEpoch);
currentScale = 0;
emit ScaleUpdated(currentScale);
newP = DECIMAL_PRECISION;
// If multiplying P by a non-zero product factor would reduce P below the scale boundary, increment the scale
} else if (currentP.mul(newProductFactor).div(DECIMAL_PRECISION) < SCALE_FACTOR) {
newP = currentP.mul(newProductFactor).mul(SCALE_FACTOR).div(DECIMAL_PRECISION);
currentScale = currentScaleCached.add(1);
emit ScaleUpdated(currentScale);
} else {
newP = currentP.mul(newProductFactor).div(DECIMAL_PRECISION);
}
assert(newP > 0);
P = newP;
emit P_Updated(newP);
}
Reasons for putting high is because
- It breaks the invariant of P being > 0 which according to the docs would break deposit tracking when Pool is not empty.
- Even though in some lucky cases, smaller liquidation can be made if batch liquidation is done, in the event that P is at a precarious place whereby all permutations of liquidations done will result in P being 0, liquidation will be DOSed which can be detrimental to protocol as they cannot liquidate bad debt and hence might lead it to insolvency.
Recommended Mitigation Steps
Recommend setting SCALE_FACTOR to 1e18, even though the docs did explain that 1e9 is used instead of 1e18 to ensure negligible precision loss, the alternative option is redesigning the mitigation mechanism of rounding error for P.
tess3rac7 (Ethos Reserve) commented via duplicate issue #444:
Since this is a disclosed issue in the Liquity repo as of 2 weeks before the time of this comment, and we have made no changes to this piece of code that makes it different from the liquity liquidation logic, I’m not sure if this is a valid bug report made as part of the C4A content, or if it’s just relaying of information that has been public knowledge since before the contest began. Requesting judge to weigh in.
Trust (judge) commented via duplicate issue #444:
The affected code is in scope, therefore imo the issue is valid unless communicated otherwise.
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 scale factor used to prevent P from becoming too small is insufficient, allowing P to round down to zero after repeated small multiplications.
Impact
Certain liquidations could be blocked, preventing the protocol from clearing bad debt and potentially leading to insolvency.
What the attacker needed
Multiple liquidations with small product factors; P already reduced by prior liquidations
The fix
The scale factor should be increased to 1e18, or the rounding-error mitigation for P should be redesigned.
How it was found
- First liquidation with newProductFactor 1e2 reduces P to 1e11 after scaling.
- Second liquidation with newProductFactor 1e3 reduces P to 1e5 after scaling.
- Third liquidation with newProductFactor 1e3 reduces P to 0 because the scaled result is below 1e18.
- The assertion `assert(newP > 0)` fails and the liquidation 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.
In this lending protocol, a number called P tracks how much of the stability pool remains after liquidations. The code assumes P will never reach zero, but with several small liquidations it can become zero, and then the code crashes (reverts) when trying to update it. That means some liquidations cannot happen, which could leave bad debt in the system.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.