Incorrect boundaries check in GeVault’s getActiveTickIndex can temporarily freeze assets due to Index out of bounds error
The getActiveTickIndex function can return an index that is too high, causing an out-of-bounds error when the contract tries to deploy assets to four ticks. This can make rebalance, deposit, and withdraw operations revert, temporarily freezing assets.
GeVault stores the TokenisableRange instances it operates on in an ordered array:
TokenisableRange[] public ticks;
Whenever a rebalancing happens, the GeVault contract withdraws all its liquidity and redeploys it on at most four ticks, starting from (and including) the one identified by the getActiveTickIndex function:
However, the getActiveTickIndex(), given that the termination condition of its for loop, can return indices up to ticks.length - 3, included (because the increment of activeTickIndex that made the boundary check fail is kept):
/// @notice Return first valid tick
function getActiveTickIndex() public view returns (uint activeTickIndex) {
if (ticks.length >= 5){
// looking for index at which the underlying asset differs from the next tick
for (activeTickIndex = 0; activeTickIndex < ticks.length - 3; activeTickIndex++){
// [...]
if ( /* ... */ )
break;
}
}
}
So the highest value it can possibly return is ticks.length - 3. If we take this value and project where rebalance() will deploy assets, we’ll have the ticks at the four indices: [ticks.length - 3, ticks.length - 2, ticks.length - 1, ticks.length], and the last value will overflow, causing the rebalancing, and the liquidity operation that triggered it (if any) to fail.
Impact
Whenever the market is such that the getActiveTickIndex returns the last possible index, the contract will revert on any rebalance, deposit, and more importantly withdraw operations. Despite the impact including locking assets, this finding is reported as medium severity because the protocol governance could resolve the situation by adding extra ticks & rescue the assets without requiring a contract code upgrade.
Proof of Concept
I have a running PoC in my environment, which I will keep aside and will be happy to provide if requested, but I would rather not share it because it’s a monstrous setup with a couple of workarounds to not make it even worse.
High level my setup is:
set up a GeVault with 6 ranges, most of which are mostly at higher prices than the market:
range at index 0 at 3001-3250 (implicit, e10)
1 at 2751-3000
2 at 2501-2750
3 at 2251-2500
4 at 2001-2250
5 at 1751-2000
with USDC/WETH at 1870 (I forked mainnet at block 17811921)
call getActiveTickIndex() which will return 3 (higher than 2, which is the max value that would not make the fund deployment go out of range)
deposit some WETH - this will revert out of bounds
Tools Used
IDE, foundry
Recommended Mitigation Steps
Decrease by 1 the loop boundaries:
/// @notice Return first valid tick
function getActiveTickIndex() public view returns (uint activeTickIndex) {
if (ticks.length >= 5){
// looking for index at which the underlying asset differs from the next tick
- for (activeTickIndex = 0; activeTickIndex < ticks.length - 3; activeTickIndex++){
+ for (activeTickIndex = 0; activeTickIndex < ticks.length - 4; activeTickIndex++){
(uint amt0, uint amt1) = ticks[activeTickIndex+1].getTokenAmountsExcludingFees(1e18);
(uint amt0n, uint amt1n) = ticks[activeTickIndex+2].getTokenAmountsExcludingFees(1e18);
if ( (amt0 == 0 && amt0n > 0) || (amt1 == 0 && amt1n > 0) )
break;
}
}
}
Or use a dedicated loop variable, and explicitly return the correct value.
getActiveTickIndex goal is to return a valid index if it exists, independently of whether the rest of the tx will succeed or fail bc there arent enough ticks.
The sponsor’s comment confirms that getActiveTickIndex returns an index that may not have enough ticks after it, which is the root cause of this finding.
Therefore, I believe both the finding’s description and availability impact on GeVault’s rebalance, deposit, and withdraw remain valid, so I would ask to kindly reconsider the invalidation of this finding.
For the sake of accurate reporting and in accordance with sponsor’s intention that was now made clear, I would recommend a different mitigation than what was initially reported - protecting the GeVault.deployAssets() code that uses the active tick index from the out-of-bounds error reported in this finding:
The original report as published on Code4rena, by 3docSec. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Root cause
The loop condition in getActiveTickIndex allows the index to reach ticks.length - 3, but deployAssets then accesses ticks at index up to ticks.length, causing an out-of-bounds error.
What the attacker needed
GeVault with at least 5 ticks; Market price near the last ticks
The fix
The code was changed to avoid the out-of-bounds error, as stated in the mitigation.
How it was found
Set up a GeVault with six ticks, most at higher prices than the market.
Call getActiveTickIndex(), which returns an index higher than the maximum safe value.
Deposit some WETH, which reverts due to out-of-bounds access.
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.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
A smart contract keeps a list of price ranges (ticks) and uses a function to pick a starting point for investing. That function can pick a starting point too close to the end of the list, so when the contract tries to use four ranges in a row, it goes past the end and the transaction fails. This means users cannot withdraw their funds until the list is extended.
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.