Router.getOrCreatePoolAndAddLiquidity can be frontrunned which leads to price manipulation
The Router.getOrCreatePoolAndAddLiquidity function allows a user to create a pool and add liquidity in one transaction. An attacker can front-run this transaction to create the pool with a different initial price, causing the victim's liquidity to be added at the attacker's price and allowing the attacker to profit.
Originally disclosed on Code4rena
Submitted by rvierdiiev
Router.getOrCreatePoolAndAddLiquidity can be frontrunned which allows the setting of a different initial price.
Proof of Concept
When new Pool is created, it is already initialized with active tick (Pool.sol#L76). This allows the pool creator to provide price for assets. So when first LP calls Pool.addLiquidity, this active tick is used to determine how many assets LP should deposit.
Router.getOrCreatePoolAndAddLiquidity function allows caller to add liquidity to the pool that can be not created yet. In such case it will create it with initial active tick provided by sender and then it will provide user’s liquidity to the pool. In case the pool already exists, function will just add liquidity to it.
Router.sol#L277-L287
function getOrCreatePoolAndAddLiquidity(
PoolParams calldata poolParams,
uint256 tokenId,
IPool.AddLiquidityParams[] calldata addParams,
uint256 minTokenAAmount,
uint256 minTokenBAmount,
uint256 deadline
) external payable checkDeadline(deadline) returns (uint256 receivingTokenId, uint256 tokenAAmount, uint256 tokenBAmount, IPool.BinDelta[] memory binDeltas) {
IPool pool = getOrCreatePool(poolParams);
return addLiquidity(pool, tokenId, addParams, minTokenAAmount, minTokenBAmount);
}
- Pool of tokenA:tokenB doens’t exist.
- User calls
Router.getOrCreatePoolAndAddLiquidityfunction to create pool with initial active tick and provide liquidity in same tx. - Attacker frontruns tx and creates such pool with different active tick.
Router.getOrCreatePoolAndAddLiquidityis executed and new Pool was not created, and liquidity was added to the Pool created by attacker.- Attacker swapped provided by first depositor tokens for a good price.
- First depositor lost part of funds.
Tools Used
VS Code
Recommended Mitigation Steps
Do not allow actions together in one tx. Do it in 2 tx. First, create Pool. And in second tx, add liquidity.
gte620v (Maverick) disputed and commented:
A user can protect against this by setting
isDelta=falseinAddLiquidityParams.This comment is also relevant: https://github.com/code-423n4/2022-12-Stealth-Project-findings/issues/83#issuecomment-1353917297
kirk-baird (judge) decreased severity to Medium and commented:
This is an interesting one. I’m inclined to side with the warden on this. Although it is possible to protect against this attack, it’s not immediately clear this will always happen.
My thoughts are that if you are calling
getOrCreatePoolAndAddLiquidity()for a pool you don’t believe exists you may setpoolParams.activeTickand thenisDelta = truewith the expectation you’ll be setting a delta frompoolParams.activeTick.If the front-runner creates a new pool with a different active tick then the user will be vulnerable since they have
isDelta = true.However, since protection does exist although optional, I think the severity should be downgraded to Medium.
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 function combines pool creation and liquidity addition in one transaction, so an attacker can front-run to set the initial price.
Impact
The first depositor could lose part of their funds, though the attack can be prevented by setting isDelta=false.
What the attacker needed
Victim calls getOrCreatePoolAndAddLiquidity; Attacker can front-run the transaction
The fix
The function should not allow pool creation and liquidity addition in one transaction; instead, create the pool first and add liquidity in a separate transaction.
How it was found
- Pool of tokenA:tokenB doesn't exist.
- User calls Router.getOrCreatePoolAndAddLiquidity to create pool with initial active tick and provide liquidity in same tx.
- Attacker frontruns tx and creates such pool with different active tick.
- Router.getOrCreatePoolAndAddLiquidity is executed and new Pool was not created, and liquidity was added to the Pool created by attacker.
- Attacker swapped provided by first depositor tokens for a good price.
- First depositor lost part of funds.
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 trading pool, the price is set by the first person who creates it. If you try to create and add money in one step, someone else can create the pool first with a bad price. Then your money gets added at that bad price, and the attacker can take some of it.
What is Race condition?
Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.