The ContinuousGDA implementation is incorrect leading to liquidation auctions running at the wrong price
The ContinuousGDA library used to price liquidation auctions implemented the pricing formula incorrectly, substituting the emission rate where the decay constant should have been used. This made the purchase price lower than intended, over-incentivising liquidations.
dirk_y Follow with Angry_Mustache_Man
Originally disclosed on Code4rena
Submitted by dirk_y, also found by Angry_Mustache_Man
https://github.com/GenerationSoftware/pt-v5-cgda-liquidator/blob/7f95bcacd4a566c2becb98d55c1886cadbaa8897/src/libraries/ContinuousGDA.sol#L39-L41 https://github.com/GenerationSoftware/pt-v5-cgda-liquidator/blob/7f95bcacd4a566c2becb98d55c1886cadbaa8897/src/libraries/ContinuousGDA.sol#L65 https://github.com/GenerationSoftware/pt-v5-cgda-liquidator/blob/7f95bcacd4a566c2becb98d55c1886cadbaa8897/src/libraries/ContinuousGDA.sol#L86
The LiquidationPair contract facilitates Periodic Continuous Gradual Dutch Auctions for yield. This uses the underlying ContinuousGDA.sol library in order to correctly price the auctions.
However this library incorrectly implements the formula, using the emission rate in a few places where it should use the decay constant. Since the decay constant is usually less than the emission rate (as can also be seen from the test suite), this means that the purchasePrice calculation is lower than it should be, meaning that liquidations are over-incentivised.
Proof of Concept
This is difficult to demonstrate given the issue is basically just that the formula in https://www.paradigm.xyz/2022/04/gda has been wrongly implemented. However I’ll point out a few issues in the code:
function purchasePrice(
SD59x18 _amount,
SD59x18 _emissionRate,
SD59x18 _k,
SD59x18 _decayConstant,
SD59x18 _timeSinceLastAuctionStart
) internal pure returns (SD59x18) {
if (_amount.unwrap() == 0) {
return SD59x18.wrap(0);
}
SD59x18 topE = _decayConstant.mul(_amount).div(_emissionRate);
topE = topE.exp().sub(ONE);
SD59x18 bottomE = _decayConstant.mul(_timeSinceLastAuctionStart);
bottomE = bottomE.exp();
SD59x18 result;
if (_emissionRate.unwrap() > 1e18) {
result = _k.div(_emissionRate).mul(topE).div(bottomE);
} else {
result = _k.mul(topE.div(_emissionRate.mul(bottomE)));
}
return result;
}
In the result calculation you can see that _k is divided by _emissionRate. However, according to the proper formula, _k should be divided by _decayConstant.
Another issue occurs in purchaseAmount where _k is added to lnParam instead of ONE and price is multiplied by _emissionRate instead of _decayConstant:
function purchaseAmount(
SD59x18 _price,
SD59x18 _emissionRate,
SD59x18 _k,
SD59x18 _decayConstant,
SD59x18 _timeSinceLastAuctionStart
) internal pure returns (SD59x18) {
if (_price.unwrap() == 0) {
return SD59x18.wrap(0);
}
SD59x18 exp = _decayConstant.mul(_timeSinceLastAuctionStart).exp();
SD59x18 lnParam = _k.add(_price.mul(_emissionRate).mul(exp)).div(_k);
SD59x18 numerator = _emissionRate.mul(lnParam.ln());
SD59x18 amount = numerator.div(_decayConstant);
return amount;
}
I would suggest double checking the formula and the derivation of the complementary formulas to calculate amount/k. The correct implementation is show in the diff below.
Recommended Mitigation Steps
The implementation should be updated to correctly calculate the price for a continuous GDA. I have made the required fixes in the diff below:
diff --git a/src/libraries/ContinuousGDA.sol b/src/libraries/ContinuousGDA.sol
index 721d626..7e2bb61 100644
--- a/src/libraries/ContinuousGDA.sol
+++ b/src/libraries/ContinuousGDA.sol
@@ -36,9 +36,9 @@ library ContinuousGDA {
bottomE = bottomE.exp();
SD59x18 result;
if (_emissionRate.unwrap() > 1e18) {
- result = _k.div(_emissionRate).mul(topE).div(bottomE);
+ result = _k.div(_decayConstant).mul(topE).div(bottomE);
} else {
- result = _k.mul(topE.div(_emissionRate.mul(bottomE)));
+ result = _k.mul(topE.div(_decayConstant.mul(bottomE)));
}
return result;
}
@@ -62,7 +62,7 @@ library ContinuousGDA {
return SD59x18.wrap(0);
}
SD59x18 exp = _decayConstant.mul(_timeSinceLastAuctionStart).exp();
- SD59x18 lnParam = _k.add(_price.mul(_emissionRate).mul(exp)).div(_k);
+ SD59x18 lnParam = ONE.add(_price.mul(_decayConstant).mul(exp)).div(_k);
SD59x18 numerator = _emissionRate.mul(lnParam.ln());
SD59x18 amount = numerator.div(_decayConstant);
return amount;
@@ -83,7 +83,7 @@ library ContinuousGDA {
) internal pure returns (SD59x18) {
SD59x18 exponent = _decayConstant.mul(_targetFirstSaleTime);
SD59x18 eValue = exponent.exp();
- SD59x18 multiplier = _emissionRate.mul(_price);
+ SD59x18 multiplier = _decayConstant.mul(_price);
SD59x18 denominator = (_decayConstant.mul(_purchaseAmount).div(_emissionRate)).exp().sub(ONE);
SD59x18 result = eValue.div(denominator);
return result.mul(multiplier);
Assessed type
Math
RaymondFam (Lookout) commented:
The formula in the doc is just one of many different price functions the protocol can work on.
asselstine (PoolTogether) confirmed
I agree that the pricing formula is up to the project to figure out and implement; it doesn’t necessarily have to be the one Paradigm laid out in their blog post.
Nevertheless, the sponsor confirmed the issue, so I will leave it as that.
IMO the issue’s severity can be raised to High, but I’d have liked to see further proof of price deviations to justify the higher severity. =)
I suggest explicitly stating out what the intended pricing formula is, so that wardens can verify the implementation’s correctness. Right now, it’s guesswork.
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 library used the emission rate in several places where the decay constant belonged, so the derived price and amount formulas did not match the intended continuous GDA maths.
Impact
Liquidation auctions ran at a lower price than intended, over-incentivising liquidators; the judge noted severity could be High but wanted more proof of the price deviation.
What the attacker needed
Liquidations active in the protocol
The fix
The implementation should be updated so the price and amount formulas use the decay constant where the emission rate was wrongly used.
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.
A Dutch auction starts at a high price and drops over time. This code calculates that falling price, but it used the wrong variable in the maths, so buyers were quoted a cheaper price than the design intended. That means people liquidating positions got a better deal than they should have.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.