Liquidity manipulation is possible when trading
The Trade contract uses the Uniswap spot price (slot0) without any manipulation checks or TWAP, so an attacker can flash-loan to move the pool price and cause a victim's trade to execute at an inaccurate rate, potentially losing funds.
Yunaci Follow with Rhaydden, crypticdefense and 11 others
Originally disclosed on Code4rena
Submitted by Yunaci, also found by Rhaydden, crypticdefense, Naresh, kodyvim, emmac002, erictee, WinSec, ZanyBonzy, Sparrow, julian_avantgarde, lightoasis, Bauchibred, and Sathish9098
The Trade contract lacks any checks against the retrieved price. It doesn’t utilize TWAP. Consequently, an attacker can manipulate the spot price (slot0) through a flash loan attack. This manipulation can lead to highly inaccurate trade results and potential loss of funds for the users.
Proof of Concept
The Trade contract initiates the trading process by calling Trade::trade(). This function sets the tradeResult.sqrtPrice by calling Trade::getSqrtPrice().
The Trade::getSqrtPrice() retrieves the sqrtPrice using the uniswapPool and the boolean isQuoteZero as arguments.
Internally calls the UniHelper::getSqrtPrice() function with the relevant parameters and returns the sqrtPrice calculated directly using the spot price(slot0), which an attacker can manipulate, which can lead to highly inaccurate trade results and potential loss of funds for the users.
https://github.com/code-423n4/2024-05-predy/blob/main/src/libraries/Trade.sol#L112C5-L114C6
/src/libraries/Trade.sol
112: function getSqrtPrice(address uniswapPoolAddress, bool isQuoteZero) internal view returns (uint256 sqrtPriceX96) {
113: return UniHelper.convertSqrtPrice(UniHelper.getSqrtPrice(uniswapPoolAddress), isQuoteZero);
114: }
https://github.com/code-423n4/2024-05-predy/blob/main/src/libraries/UniHelper.sol#L13C4-L15C6
/src/libraries/UniHelper.sol
13: function getSqrtPrice(address uniswapPoolAddress) internal view returns (uint160 sqrtPrice) {
14: (sqrtPrice,,,,,,) = IUniswapV3Pool(uniswapPoolAddress).slot0();
15: }
Coded PoC
In order to run the test, go to test/pool and replace the content of Trade.t.sol with the one from the gist below. You can also delete the file and create a new one with its code.
https://gist.github.com/Vancelott/198c4e9133a7ee5d4d6900471a9717f7
Basic flow of the test:
- Alice wants to initiate a trade in a specific pair.
- After that, she simply calls trade.
- A malicious user (Bob) tracks the protocol’s mempool and notices the call.
- Bob takes out a flash loan and adds liquidity to the Uniswap pool associated with the pair in Predy.
- As the spot price is used within the calculations of the trade, Alice has traded for less tokens due to Bob’s added liquidity.
- Bob then withdraws his liquidity and pays back the flash loan.
Recommended Mitigation Steps
Consider using the TWAP price instead of the spot price in order to prevent price manipulation.
syuhei176 (Predy) acknowledged and commented via duplicate Issue #74:
To ensure that the price of the target Uniswap pool has not been manipulated during Squart transaction, we need to check slot0. Therefore, a
getSqrtPricefunction is necessary.
0xsomeone (judge) decreased severity to Medium
0xsomeone (judge) commented via duplicate Issue #74:
The submission and its duplicates detail how it is dangerous to rely on the
slot0to provide proper data for a particular trade.There is a particular code segment of the system that will utilize the
slot0square root price at face value, and that is the execution of aTrade::swapwith a net-zerototalBaseAmount.Liquidations and Gamma Market trade executions will properly apply slippage checks, but direct interaction with the
PredyPoolwill not. Given that the allowlist can be disabled for a particularpairId, it infers that users would eventually be able to directly interact with thePredyPooland this would be unsafe to do so in case of a net-neutral trade interaction.I believe a severity of medium is appropriate as trades will be affected, and at the very least the interaction by the user will utilize improper asset ratios when executed. The interactions within the
Perpcontract involved are complex, and I welcome the Sponsor to refute my understanding of the codebase at this point.
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 Trade contract reads the Uniswap pool's spot price (slot0) directly without using a time-weighted average price (TWAP) or any other manipulation check.
Impact
A victim's trade could execute at an inaccurate price, causing them to receive fewer tokens and potentially lose funds; the judge noted that liquidations and Gamma Market trades apply slippage checks, but direct PredyPool interactions with a net-zero totalBaseAmount do not.
What the attacker needed
Attacker can flash-loan funds; attacker can add liquidity to the Uniswap pool; victim trades directly with PredyPool; trade has net-zero totalBaseAmount
The fix
The Trade contract should use the TWAP price instead of the spot price to prevent manipulation.
How it was found
- Alice initiates a trade in a specific pair by calling trade.
- Bob monitors the mempool and notices Alice's call.
- Bob takes out a flash loan and adds liquidity to the Uniswap pool associated with the pair.
- Because the spot price is used in the trade calculation, Alice receives fewer tokens due to Bob's added liquidity.
- Bob withdraws his liquidity and repays the flash loan.
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 smart contract trade uses the current price from a Uniswap pool to decide how many tokens to give. An attacker can temporarily change that price by borrowing a large amount of money (a flash loan) and trading in the pool. The victim's trade then happens at the wrong price, so they get fewer tokens than they should.