ThecosomataETH: Oracle price can be better secured (freshness + tamper-resistance)
The contract relies on a Curve pool's price oracle to calculate liquidity amounts, which could be manipulated by an attacker who skews the price a few blocks before the upkeep transaction, causing the treasury to deposit more ETH or burn more BTRFLY than necessary. The report recommends adding freshness and deviation checks to the oracle usage.
Submitted by hickuphh3
The ThecosomataETH contract adds ETH and BTRFLY tokens as liquidity into the ETH-BTRFLY curve crypto pool. The calculateAmountRequiredForLP() function relies on the price_oracle value returned by the pool to calculate the ETH and BTRFLY amounts to be added as liquidity. It is therefore important to ensure that price_oracle is accurate.
At the time of writing, the pool has about $5M in liquidity, which is comparable to that of the liquidity provided on UniswapV3. Flash loan attacks are therefore possible, but ineffective (explained further later).
In the curve v2 whitepaper, the price oracle mechanism is explained briefly in the “Algorithm for repegging” section. It is reproduced below for convenience.
Internally, we have a price oracle given by an exponential moving average (EMA) applied in N-dimensional price space. Suppose that the last reported price is
pLast, and the update happenedtseconds ago while the half-time of the EMA isT1/2. Then the oracle pricep_newis given as:
α = 2^(− t / T1/2);
p_new = pLast * (1 - α) + α * p_old // p_old = current price_oracle
Impact
With oracles (curve pool now, to be switched to chainlink based oracle as per comment in L27), there is an inverse correlation between freshness and tamper-resistance.
We can expect price_oracle to be relatively fresh as trades will occur whenever arbitrage opportunities arise against the UniV3 pool which has comparable liquidity. Note that the ETH-BTRFLY pool has a half-time of 10 minutes (T1/2 = 600). This means that after exactly 10 mins, both pLast and p_old have equal weightage.
It is unclear how resistant the EMA oracle is against manipulation. Flash loan attacks, while possible, will be ineffective because t will be zero (pLast will be ignored in the update). However, a sophisticated attacker could possibly skew the price oracle by inflating the price of BTRFLY a couple of blocks before the performUpkeep() transaction to get the treasury to deposit more ETH / burn more BTRFLY than necessary.
Recommended Mitigation Steps
In my opinion, both freshness and tamper-resistance can be better secured.
This can be done by:
- Ensuring that the price was updated within a certain limit.
// eg. last price update / trade must have been executed within the past hour
uint256 lastPricesTimestamp = ICurveCryptoPool(CURVEPOOL).last_prices_timestamp();
require(block.timestamp - lastPricesTimestamp <= 1 hours, 'stale price');
- Checking that the last reported price
pLasthas not deviated too far from the current oracle pricep_old. One can argue that it would be safer to add liquidity when the market isn’t volatile.
uint256 lastPrice = ICurveCryptoPool(CURVEPOOL).last_prices();
uint256 oraclePrice = ICurveCryptoPool(CURVEPOOL).price_oracle();
uint256 percentDiff;
// eg. require difference in prices to be within 5%
if (lastPrice > oraclePrice) {
percentDiff = (lastPrice - oraclePrice) * 1e18 / oraclePrice;
} else {
percentDiff = (oraclePrice - lastPrice) * 1e18 / oraclePrice;
}
require(percentDiff <= 5e16, 'volatile market');
drahrealm (Redacted Cartel) commented:
Idem with M-05, we will proceed with doing calculating the min token amount off-chain, then specify it when calling
performUpKeep.Thanks for the finding.
Alex the Entreprenerd (judge) commented:
Agree that solution is based off of M-05. While I believe simpler solutions where highlighted, I feel the warden put in the extra effort to make a valuable submission.
As such, I’ll mark the finding as unique.
Personally I would not trust Curve Pricing model over a Price Feed at this time, however am happy to be proven wrong.
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 contract uses the pool's oracle price without checking its freshness or how much it deviates from the last reported price, making it susceptible to manipulation over a short period.
Impact
A sophisticated attacker could skew the oracle price to make the treasury deposit more ETH or burn more BTRFLY than necessary, though flash loan attacks are ineffective because the time weight would be zero.
What the attacker needed
Attacker can trade on the Curve pool; attacker can influence price before upkeep
The fix
The contract should be fixed by requiring that the price was updated within a certain time limit and that the last reported price has not deviated too far from the current oracle price.
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.
This contract uses a price from a trading pool to decide how much of two tokens to add as liquidity. If an attacker can temporarily push that price up or down, the contract might make a bad trade that loses money for the treasury. The report suggests checking that the price is recent and not too different from the last price.