Array Length of tickTracking_ can be purposely increased to Brick Minting and Burning of most users’ liquidity positions
The tickTracking_ array grows by one entry every time a trade crosses a tick, and the reward-accrual loop iterates over every entry. An attacker can make many tiny swaps back and forth across a tick boundary to inflate the array, causing minting, burning and harvesting of liquidity to run out of gas and revert for most users.
Submitted by Banditx0x, also found by Banditx0x, maanas, emerald7017, adriro, twicek, 0xDING99YA, 0xpiken, 3docSec, and 0xWaitress
Lines of code
https://github.com/code-423n4/2023-10-canto/blob/40edbe0c9558b478c84336aaad9b9626e5d99f34/canto_ambient/contracts/mixins/LiquidityMining.sol#L24-L35 https://github.com/code-423n4/2023-10-canto/blob/40edbe0c9558b478c84336aaad9b9626e5d99f34/canto_ambient/contracts/mixins/LiquidityMining.sol#L122
Impact
A malicious user can brick minting, burning and harvesting of liquidity for almost all liquidity providers.
Important NOTE: This is a different vector from another gas issue, which is iterating over too many ticks in (int24 i = lowerTick + 10; i <= upperTick - 10; ++i). That issue affects wide liquidity positions, while this attack vector affects even liquidity positions with a relatively small number of ticks.
Proof of Concept
When accrueConcentratedPositionTimeWeightedLiquidity is called, under most conditions for every potentially eligible tick, it will iterate over every tickTrackingData in tickTracking:
while (time < block.timestamp && tickTrackingIndex < numTickTracking)
tickTracking is iterated by tickTrackingIndex++;
The array mapped by tickTracking_ is increased by 1 for a tick every time a trade through the liquidity pool changes the price from a different tick to this tick. This is implemented in the crossTicks function:
function crossTicks(
bytes32 poolIdx,
int24 exitTick,
int24 entryTick
) internal {
uint256 numElementsExit = tickTracking_[poolIdx][exitTick].length;
tickTracking_[poolIdx][exitTick][numElementsExit - 1]
.exitTimestamp = uint32(block.timestamp);
StorageLayout.TickTracking memory tickTrackingData = StorageLayout
.TickTracking(uint32(block.timestamp), 0);
tickTracking_[poolIdx][entryTick].push(tickTrackingData);
}
A user could purposely increase the length of the tickTracking_ array and cause the gas limit to be reached whenever the array is looped over.
The price impact required to cross a tick is from 0 to 1 bps, with 1 bps as the tick width. This is already extremely small, but the attacker could have the swap amount be a very small fraction of a bps if they first swap to make the price end very close to a tick boundary, then execute multiple extremely small swaps which bounce the price back and forth over the tick boundary.
Note that the CANTO liquidity rewards are targeted to stable pools. An attacker can be quite confident, for example, that a USDC/USDT pool will trade at around $1, and the ticks closest to $1 will always be eligible for rewards and therefore be looped over by all rewardable positions when accrueConcentratedPositionTimeWeightedLiquidity is called. Therefore, the attack can be targeted to just one or two ticks to affect almost every user.
accrueConcentratedPositionTimeWeightedLiquidity is called during minting, burning and harvesting liquidity positions. Therefore, this gas griefing attack will make all these functions revert for almost every user. This would basically break the functionality of concentrated liquidity pools on Ambient.
Contrast the effect to the cost to the attacker: using the aforementioned attack vector the main cost to the attacker will be the gas costs of performing the swaps. This is far lower than the damage that is done to the protocol/users.
One additional factor which makes this attack easy to execute, is that crossing ticks, even if the entry and exit is within the same block.timestamp, adds to the array length. Tracking this is unnecessary, because the tick was active for 0 blocks, and therefore, the time delta and allocated rewards is zero.
Recommended Mitigation Steps
One immediate step would to pop() tickTrackingData as soon as the exitTimestamp == entryTimestamp. This happens to the last element of the array when crossTicks is called. Tracking this is unnecessary, because the tick was active for 0 blocks, and therefore, the time delta and allocated rewards is zero.
The documentation stated that CANTO rewards are meant to be distributed for stable pools for this codebase. The term “stable” could have different interpretations, but this recommendation assumes that this refers to stablecoin-like or pegged asset pairs such as stETH/WETH, USDT/USDC etc.
Instead of iterating through every tick, one could assume a range where the stable assets could lie and then reward all positions that lie within the specified range; in this case, +/- 10 ticks of the price tick.
This makes an assumption that these “stable assets” will actually stay pegged to each other. However, the current accounting architecture has multiple problems:
- Given the high number of loops required by the current accounting mechanism, there are multiple reasons that gas could run out. This includes iterating through too many ticks or having too many tick entries/exits.
- The current mechanism increases the gas costs of all minting, burning and harvesting.
- DOS attacks like the one described in this issue are possible.
Assuming a stable price has the downside of misallocating rewards if the stable assets depeg from each other. However, this may be a reasonable tradeoff to prevent this DOS attack.
Assessed type
DoS
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 design appends a new tick-tracking entry on every tick crossing, even when entry and exit happen in the same block, and the reward loop iterates over the whole array without a bound.
Impact
Minting, burning and harvesting of liquidity positions could revert for almost all users, effectively breaking concentrated liquidity pools on Ambient; the attacker's main cost is swap gas.
What the attacker needed
Ability to trade in the target pool; target tick is near a stable price; gas funds for many small swaps
The fix
The report recommends popping the tick-tracking entry when exit and entry timestamps are equal, and/or rewarding positions within a fixed range around the stable price instead of iterating every tick.
How it was found
- Swap so the price ends very close to a tick boundary.
- Execute many extremely small swaps that bounce the price back and forth over that boundary.
- Each crossing appends another entry to the tickTracking_ array for that tick.
- When accrueConcentratedPositionTimeWeightedLiquidity runs, it loops over all entries and runs out of gas, so minting, burning and harvesting revert.
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 liquidity pool tracks price movements by recording each time the price crosses a small boundary called a tick. The code that pays out rewards later reads every one of those records in a loop, and each read costs gas. By making lots of tiny trades that cross the same boundary repeatedly, an attacker fills the list with junk records so the loop becomes too expensive to finish, and normal users' actions fail.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.