Calculation for directionMask is incorrect
The function that determines swap direction in a Uniswap V3 pool incorrectly assumes WETH is always token1, but Uniswap orders tokens by address. This can cause the direction mask to be wrong, leading to swaps in the opposite direction and potential financial loss.
0xleadwizard Follow with Agontuk, BenRai and 7 others
Originally disclosed on Code4rena
Submitted by 0xleadwizard, also found by Agontuk, BenRai, Infect3d, Jiri123, NexusAudits, Rhaydden, rouhsamad, SpicyMeatball, and ZhengZuo999
The _getQuoteAndDirection function’s flawed logic can cause incorrect direction determination in the UniswapV3 pool. The recommended mitigation ensures that the function dynamically identifies token0 and token1 and assigns the correct direction mask. This prevents potential financial losses and ensures accurate rebalancing.
Finding description and impact
The function previewRebalance is called off-chain, to calculate values that can be passed to the function rebalance when making a call for balancing the uniswapV3 vETH/WETH pool.
function previewRebalance()
public
view
returns (bool result, uint256 directionMask, uint256 amountIn, uint256 amountOut)
{
address tokenIn;
address tokenOut;
(tokenIn, tokenOut, amountIn) = _getTokenInOut();
(amountOut, directionMask) = _getQuoteAndDirection(tokenIn, tokenOut, amountIn);
result = amountOut > amountIn;
}
The function _getQuoteAndDirection takes tokenIn, tokenOut & amountIn as parameter to output amountOut & directionMask.
directionMask is used to decide if the swap is zero-for-one or one-for-zero in OKX.
The _getQuoteAndDirection function assumes that WETH is always token1, which can lead to incorrect direction determination in cases where WETH is actually token0. This is due to the fact that Uniswap sorts token0 and token1 lexicographically by their addresses, and not based on their logical roles.
function _getQuoteAndDirection(
address tokenIn,
address tokenOut,
uint256 amountIn
) internal view returns (uint256 amountOut, uint256 directionMask) {
(amountOut, , , ) = IQuoter(quoter).quoteExactInputSingleWithPool(
IQuoter.QuoteExactInputSingleWithPoolParams({
tokenIn: tokenIn,
tokenOut: tokenOut,
amountIn: amountIn,
fee: fee,
pool: uniswapPool,
sqrtPriceLimitX96: 0
})
);
>> directionMask = (tokenIn == weth) ? _BUY_MASK : _SELL_MASK;
}
Example: If the UniswapV3 pool has token0 as WETH (lower address value) and token1 as vETH (higher address value), and the pool has more vETH than WETH, the tokenIn will be WETH. However, because WETH is token0 in this case, the correct direction would be zero-for-one. The current logic mistakenly assumes WETH is token1, leading to an incorrect direction of one-for-zero.
For context, here is how the MASK is used in OKX:
uint256 private constant _ONE_FOR_ZERO_MASK = 1 << 255; // Mask for identifying if the swap is one-for-zero
let zeroForOne := eq(and(_pool, _ONE_FOR_ZERO_MASK), 0)
Recommended mitigation steps
Add the logic for considering if the tokenIn is token0 or token1.
function _getQuoteAndDirection(
address tokenIn,
address tokenOut,
uint256 amountIn
) internal view returns (uint256 amountOut, uint256 directionMask) {
// Retrieve token0 and token1 from the Uniswap pool
address token0 = IUniswapV3Pool(uniswapPool).token0();
address token1 = IUniswapV3Pool(uniswapPool).token1();
// Call the quoter to get the amountOut
(amountOut, , , ) = IQuoter(quoter).quoteExactInputSingleWithPool(
IQuoter.QuoteExactInputSingleWithPoolParams({
tokenIn: tokenIn,
tokenOut: tokenOut,
amountIn: amountIn,
fee: fee,
pool: uniswapPool,
sqrtPriceLimitX96: 0
})
);
// Determine directionMask based on tokenIn position (token0 or token1)
if (tokenIn == token0) {
directionMask = _SELL_MASK; // Zero-for-one direction
} else {
directionMask = _BUY_MASK; // One-for-zero direction
}
}
Shaneson (Lambo.win) acknowledged and commented:
When the VETH is deployed, the direction will be updated. But yes, this is still a good suggestion.
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 hardcodes the assumption that WETH is token1 instead of checking the pool's actual token0 and token1 ordering.
Impact
The incorrect direction mask could cause swaps to be executed in the wrong direction, leading to potential financial losses.
What the attacker needed
WETH is token0 in the pool; rebalance is called with the wrong mask
The fix
The function should be fixed by reading token0 and token1 from the Uniswap pool and setting the direction mask based on whether tokenIn equals token0.
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.
In a Uniswap pool, the two tokens are called token0 and token1, and their order is decided by sorting their addresses alphabetically, not by which token is more important. The code here always treats WETH as token1, so if WETH's address is actually lower, it becomes token0 and the swap direction is set backwards. That means the trade goes the wrong way, which could lose money.
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.