Reallocation depends on the slot0 price, which can be manipulated
The reallocate function reads the Uniswap V3 slot0 spot price and tick to decide whether to rebalance the LP position. An attacker can manipulate the spot price momentarily to force a reallocation to an extreme tick range, causing a loss of yield for the protocol.
Submitted by ayden, also found by ZanyBonzy, emmac002, Tigerfrake, BlockSafe, 0xhere2learn, Yunaci, air_0x, and Takarez
Anyone can invoke the reallocate function to reallocate the LP position to be within the desired range. Whether reallocation is necessary depends on the slot0 price. However, the slot0 price can be manipulated, potentially leading to the LP position being out of the current range and resulting in a loss of yield for the protocol.
Proof of Concept
Reallocate first read slot0 Perp.sol::reallocate to get current price:
function reallocate(
DataType.PairStatus storage _assetStatusUnderlying,
SqrtPerpAssetStatus storage _sqrtAssetStatus
) internal returns (bool, bool, int256 deltaPositionBase, int256 deltaPositionQuote) {
> (uint160 currentSqrtPrice, int24 currentTick,,,,,) = IUniswapV3Pool(_sqrtAssetStatus.uniswapPool).slot0();
Then, compare current price with the threshold Perp.sol::reallocate:
// if the current tick does reach the threshold, then rebalance
int24 tick;
bool isOutOfRange;
if (currentTick < _sqrtAssetStatus.tickLower) {
// lower out
isOutOfRange = true;
tick = _sqrtAssetStatus.tickLower;
} else if (currentTick < _sqrtAssetStatus.tickUpper) {
// in range
isOutOfRange = false;
} else {
// upper out
isOutOfRange = true;
tick = _sqrtAssetStatus.tickUpper;
}
Once out of range protocol invoke Perp.sol::swapForOutOfRange to update rebalance position:
function swapForOutOfRange(
DataType.PairStatus storage pairStatus,
uint160 _currentSqrtPrice,
int24 _tick,
uint128 _totalLiquidityAmount
) internal returns (int256 deltaPositionBase, int256 deltaPositionQuote) {
uint160 tickSqrtPrice = TickMath.getSqrtRatioAtTick(_tick);
// 1/_currentSqrtPrice - 1/tickSqrtPrice
int256 deltaPosition0 =
LPMath.calculateAmount0ForLiquidity(_currentSqrtPrice, tickSqrtPrice, _totalLiquidityAmount, true);
// _currentSqrtPrice - tickSqrtPrice
int256 deltaPosition1 =
LPMath.calculateAmount1ForLiquidity(_currentSqrtPrice, tickSqrtPrice, _totalLiquidityAmount, true);
if (pairStatus.isQuoteZero) {
deltaPositionQuote = -deltaPosition0;
deltaPositionBase = -deltaPosition1;
} else {
deltaPositionBase = -deltaPosition0;
deltaPositionQuote = -deltaPosition1;
}
updateRebalancePosition(pairStatus, deltaPosition0, deltaPosition1);
}
Tools Used
Foundry
Recommended Mitigation Steps
It’s recommend to use TWAP price instead of slot0 price to get the current price.
Assessed type
Invalid Validation
0xsomeone (judge) increased severity to High and commented:
The submission and its duplicates detail how the re-allocation of the pool’s positions will occur via a permissionless mechanism that will rely on the spot square root price evaluation of the Uniswap V3 pair.
This interaction is insecure, as a user can manipulate the square root price of a pair momentarily to an unfavorable price point, force a re-allocation to occur, and then capitalize on the re-allocated liquidity in an extreme tick range that would effectively result in a loss for the pool.
I believe this vulnerability merits a high-risk rating as it can be trivially exploited and would result in financial loss for a pair’s liquidity providers. To note, a re-allocation will also incur fees associated with the swaps performed by the
Perpmechanism thereby permitting multiple re-allocations to continuously drain the funds involved in a particularpairIdposition exacerbating the effects of the issue described.
Root cause of the above and issue #157 is the same, which is manipulation of the
slot0price. Even though both issues explain different impacts, they originate from the same root cause, so they should be grouped together.For example, if an oracle gives an incorrect price, there could be multiple impacts like liquidation failure, withdrawal pause, or deposit pause; but I don’t think we can create different sets for each impact. Here, the root cause is the same, and both issue can be mitigated by preventing manipulation of the
slot0price
@Tripathi - The root cause is not manipulation of the
slot0data point, but rather how theslot0data point is consumed in a different way across two distinct contracts. Given that different code segments give birth to the vulnerability, different impact is observed on each one, and that mitigation of one would not resolve the other (as we cannot change the Uniswap code), the submissions will remain distinct.
We agree that the impact is different, but the affected code is the same and is implemented multiple times.
It is similar to using transfer instead of
safetransfer. In some places, it will lead to high impact and in others, it will lead to less impact. Again, we will have to change every transfer function tosafetransfer(As mitigation of one would not resolve the other), but it used to be considered the same group of issues.The root cause is not manipulation of the
slot0data point, but rather how theslot0data point is consumed in a different way across two distinct contractsThe root cause is indeed using the spot price from the DEX, which can be manipulated by external factors.
It is important to understand that not all vulnerabilities are equal and just because some code is the same across the codebase does not necessarily mean it is a problem. As an example, a re-entrancy vulnerability stemming from the same external call at different instances of the codebase would be treated distinctly as the re-entrancy itself is not the problem; how the data points in the contract are processed is.
In this instance, the
slot0has multiple data points and the vulnerabilities require a different approach to being resolved. Simply implementing a TWAP for the re-allocation mechanism is incorrect as that would make the re-allocation system permanently inefficient. Additionally, the re-allocationslot0vulnerability also utilizes the current tick which is incorrect as well.Fixing the
currentSqrtPriceto use a TWAP would be insufficient as the current tick would be used as well. To properly address this vulnerability, a potential re-design might be warranted which would require a privileged role to perform re-allocations, re-allocations to be authorized via a signature, and other such secondary security measures that would prevent both the malleable tick and square root price from being used.The less severe issue #157 will solely utilize the square root price data point, and using a TWAP in that case is safe and a sound approach.
While I appreciate your diligent review of this submission and its sibling #157, I will maintain them as separate due to a reasonable alleviation for one exhibit being inapplicable for the other. To sum up:
- Different code segments and contracts are involved
- One exhibit uses 1 data point while the other uses 2 data points
- A TWAP solution would be applicable to one and a different solution would need to be applied to the other
Note: For full discussion, see here.
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 reallocation logic relies on the manipulable spot price and current tick from slot0 instead of a manipulation-resistant price source.
Impact
The pool's liquidity providers could suffer financial loss, and repeated reallocations could continuously drain funds through swap fees.
What the attacker needed
Ability to manipulate the Uniswap V3 spot price; reallocate function is permissionless
The fix
The text suggests using a TWAP price instead of slot0, but the judge notes a redesign may be needed, such as a privileged role or signature authorisation for reallocations.
How it was found
- Manipulate the Uniswap V3 pool's spot price to an unfavourable point.
- Call the permissionless reallocate function.
- The function reads the manipulated slot0 price and tick.
- The function detects the position as out of range and swaps to rebalance at an extreme tick.
- The attacker capitalises on the reallocated liquidity, causing a loss for the pool.
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 uses the current price from a trading pool to decide when to move its funds. An attacker can briefly push that price to a wrong value, tricking the contract into moving funds to a bad position. This can make the pool lose money.