Wrong slippage check
The addLiquidity function calculates the expected LP token amount using Curve's calc_token_amount and then applies a slippage tolerance to that already-exact value. An attacker could exploit this by manipulating the pool to be imbalanced, causing the function to accept a much lower amount of LP tokens than intended, leading to value loss.
Submitted by cmichel, also found by danb, Alex the Entreprenerd, hickuphh3, hyh, and WatchPug
The ThecosomataETH.addLiquidity function computes the expectedAmount and then subtracts a slippage percentage from it.
function addLiquidity(uint256 ethAmount, uint256 btrflyAmount) internal {
uint256[2] memory amounts = [ethAmount, btrflyAmount];
uint256 expectedAmount = ICurveCryptoPool(CURVEPOOL).calc_token_amount(
amounts
);
uint256 minAmount = expectedAmount - ((expectedAmount * slippage) / 1000);
ICurveCryptoPool(CURVEPOOL).add_liquidity(amounts, minAmount);
}
According to the Curve docs 21.4, this amount is already exact and takes the slippage into account (but not fees).
If the pool is imbalanced, the calc_token_amount will already return a wrong amount and the additional slippage check on the wrong amount is unnecessary (except for the fees).
Recommended Mitigation Steps
Consider computing the minimum expected LP tokens off-chain and pass them to the performUpkeep function as a parameter to prevent sandwich attacks.
drahrealm (Redacted Cartel) confirmed and commented:
Thanks for the finding. Confirmed that this is not the right way for handling slippage. Will be updating the flow a little bit to allow externally sourced data for the expected amount.
kphed (Redacted Cartel) disputed and commented:
Changing to “sponsor disputed” since using values derived off-chain doesn’t prevent sandwich attacks and could make it easier to get sandwiched: using the off-chain calculation method, a MEV operator would only need to parse the tx input when deciding to front run us (vs. needing to simulate the tx if we were to do our calculations on-chain).
Additionally, we’re not using the
StableSwapcontract referenced in the warden’s comment.
Alex the Entreprenerd (judge) commented:
I think the sponsor’s perspective is interesting in that I believe any MEV researcher could write a simple algorithm to check for the tx inputs to detect a slippage check.
However in practice they’d still have to run a simulation as you could input the off-chain price with any variation (different decimals, as ETH, as USD, as BTC, multiply by 2 or 5 or w/e)
Additionally while there can be arguments made as to how to mitigate, the finding is still valid.
Asking Curve for the
calc_token_amountwill return whatever price the pool can offer at that time, because tx are atomic that means that any front-running or price manipulation would have already happened in a tx before the request.This means that at worst you could directly use the output from
calc_token_amount(multiplication has no impact).What the finding also implies, is that if the pool were to be completely imbalances (99% of in asset, 1% of out asset) the price you’d get would be very low, and the code wouldn’t be able to detect it (the code is effectively same as having 0 * .95)
Because the finding has to do with potential value extraction, I believe the finding to be valid and of medium severity.
As for mitigation, there are 2 viable options:
- Use Chainlink Price Feed to get an accurate price
- Provide the price as a parameter
For option 2, I don’t believe that argument to be valid for the examples above (just shift, multiply or obfuscate the param) Additionally, while you may never get a guarantee of perfect pricing, providing a price will give you a guarantee of a minimum price, this ensures you can opt-in into the slippage you’d be willing to tolerate.
To give further details, let’s look at using Flashbots (Flashbots RPC or a private mempool, either is a great idea).
By using a private tx with the code provided for this contest, in the case of low liquidity, you’d still loose a considerable amount of value. No front-run needs to happen as in asking the price to the pool, you’ll always get a valid response.
This has happened to Yield Farming Aggregators (last I remember was yearn with StakeDAO token or similar). To summarize: Asking the price to the pool in the same tx is the same as having a 0 slippage check, which means you can loose value even without being front-run.
Now let’s add the idea of being front-run while using Flashbots RPC: -> You have calculate the off-chain Price, which means there’ a require that will revert if the tx will fail, which means (because Flashbots is awesome) the tx won’t be mined unless the tx goes through (miner get’s a tip). This means you can be extremely strict with your slippage check, providing you with as much MEV protection as possible.
For these reasons I believe the finding to be valid and I recommend you do explore:
- Flashbots (private TXs)
- Price as parameter
- Chainlink Price Feeds
kphed (Redacted Cartel) commented:
I think the sponsor’s perspective is interesting in that I believe any MEV researcher could write a simple algorithm to check for the tx inputs to detect a slippage check.
That was my point, that it lowers the difficulty threshold.
Thanks, the warden’s recommended solution makes more sense now after your elaboration. We were planning on using Flashbots Protect and will look into your other suggestions as well.
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 code applies a slippage tolerance to an amount that already reflects the pool's current state, effectively making the slippage check useless when the pool is imbalanced.
Impact
An attacker could cause the contract to accept a significantly lower amount of LP tokens than expected, leading to value loss for the protocol, especially if the pool is imbalanced.
What the attacker needed
Pool must be imbalanced; no front-run needed
The fix
The minimum expected LP tokens should be computed off-chain and passed as a parameter to the performUpkeep function, or a price feed should be 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.
When you add money to a liquidity pool, you get pool tokens in return. The code asks the pool how many tokens you should get, then subtracts a small safety margin. But the pool's answer already includes the effect of any imbalance, so subtracting more means you could get far fewer tokens than you deserve if someone manipulates the pool first.
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.