Skip to content
Business logic Write-up Advanced

[web3] Raydium Tick Manipulation Bugfix Review. This is a postmortem of a vulnerability… | by Immunefi | Jun, 2024 | Medium

The increase_liquidity function failed to verify that the provided TickArrayBitmapExtension account matched the pool's state, allowing an attacker to manipulate tick status and drain funds from liquidity pools.

medium.com

Published on medium.com

Immunefi

Immunefi, on medium.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 6 min read.

Context

Raydium is an automated market maker (AMM) built on the Solana blockchain, with an integrated central order book system, currently in its V3 iteration. Users can provide liquidity, perform swaps, and stake the RAY token for additional yield. A fundamental part of the protocol is the Concentrated Liquidity Market Maker (CLMM).

CLMM pools let liquidity providers concentrate their capital within a specific price range instead of spreading it along a curve from 0 to infinity. Liquidity is only active while the price sits inside the chosen range; outside it, the position stops earning fees and the provider is expected to adjust. This gives the same low slippage as a much larger traditional AMM pool with far less capital.

Ticks are the discrete price points of a CLMM. Each tick corresponds to a specific price level, and the space between ticks is the smallest possible price movement in the pool. A liquidity provider defines a range with tick_lower (the lowest price at which they will provide liquidity) and tick_upper (the highest). The tickarray_bitmap_extension manages the pool's pricing at extreme boundaries, very high or very low prices, and acts as an extended index that tracks which ticks have been initialised with non-zero liquidity beyond the default capacity of the system.

The vulnerable feature is the increase_liquidity function in increase_liquidity.rs, which adds liquidity to a position within a pool. It validates pool status, calculates token amounts from the user-provided maximums, updates fees, and increases liquidity in the position's state.

Setup

The report does not describe a test environment, tooling, or specific versions. The relevant preconditions come from the design of the function itself:

  • The instruction accepts a remaining_accounts vector, which holds every account passed into the instruction that is not declared in the Accounts struct. This is what allows a variable number of accounts to be supplied.
  • When the requested price range sits at an extreme boundary, the function expects an account from remaining_accounts to act as the tickarray_bitmap_extension.
  • Because remaining_accounts is flexible, the contract is responsible for validating that the accounts inside it are of the expected type and carry the correct permissions for the intended operation.

The whitehat is credited as @riproprip. The report was submitted on January 10, 2024.

Finding it

The flaw sits in the conditional handling of the tickarray_bitmap_extension. The function decides whether an account from remaining_accounts is needed for the operation, but it does not verify that remaining_accounts[0] is the accurate TickArrayBitmapExtension account linked to the current state of the pool.

That missing check is what makes the bug exploitable: the function does not confirm that the supplied account is the accurate TickArrayBitmapExtension for the pool's current state, which permits liquidity operations at arbitrary price boundaries.

Exploitation

The whitehat demonstrated the exploit by manipulating the tickarray_bitmap_extension so that the intended removal of liquidity at specific ticks was bypassed. The attack flips a tick's status in the bitmap, and transactions that should have adjusted liquidity at certain prices skip those checks, allowing unauthorised liquidity increases.

Step 1: Create a secondary pool

The attacker sets up a secondary pool.

Step 2: Target a tick

The attacker opens a position at a specific tick they want to exploit in the primary pool.

Step 3: Zero out and manipulate liquidity

They reduce the liquidity at this tick to zero and then increase it in a way that incorrectly uses the tickarray_bitmap of the primary pool.

The worked example

To make the process concrete, take price points A, B and C with A < B < C, where both A and B lie within the lower range of the bitmap_array:

  1. Identify a victim pool.
  2. Execute a swap to shift the price to just above point A (A+1).
  3. Create liquidity within the price range B to C.
  4. Perform a swap across B without crossing C, which adds liquidity to the pool state due to the manipulated tickarray_bitmap.
  5. Execute the described attack to switch the tickarray_bitmap status at B to DISABLED, allowing a swap at A+1 to skip over B without affecting liquidity.
  6. Repeat the attack to re-enable the tickarray_bitmap at B.
  7. Perform another swap over B without crossing C, effectively doubling the liquidity erroneously.
  8. This process can be repeated as many times as desired to amass an excessive amount of liquidity.
  9. Following this procedure, swaps directed towards C will yield disproportionately high amounts of Token A, indicating the vulnerability has been fully exploited.
  10. If the goal is to acquire Token B, the process can be replicated at the higher end of the price spectrum.

The core issue is the ability to flip a tick's status within the tickarray_bitmap, which lets liquidity be added without proper checks. That produces significant discrepancies in liquidity management and compromises the integrity of the pool.

Root cause

The increase_liquidity function fails to verify whether remaining_accounts[0] is the accurate TickArrayBitmapExtension account linked to the current state of the pool. The conditional logic only decides whether an account from remaining_accounts is needed; it does not confirm which account it is.

Because remaining_accounts is not declared in the Accounts struct, the contract must validate it itself. That validation was missing. The existing pool status validation and amount calculations in the function do not cover this, since they operate on the pool state and the user's inputs rather than on the identity of the supplied extension account.

Impact

A critical vulnerability in the Raydium protocol allowed an attacker to exploit the liquidity management functionality of the AMM to drain funds from liquidity pools. The manipulation lets an attacker amass an excessive amount of liquidity, after which swaps yield disproportionately high amounts of Token A, or Token B at the higher end of the price spectrum.

Raydium addressed and resolved the issue. A bounty of $505,000 in RAY tokens was awarded to the whitehat.

Fix

Raydium shipped a critical update, documented in the commit "Fix: fix for open position v2 with zero liquidity" in raydium-io/raydium-clmm (commit [redacted]). The correction adds a security check that confirms remaining_accounts[0] is the correct TickArrayBitmapExtension account associated with the pool's current state. This ensures proper handling of liquidity operations involving extreme price boundaries and prevents the exploit.

Variants to test

  • Other instructions that read remaining_accounts. Look for any handler that pulls an account out of remaining_accounts by index and uses it without checking its owner, discriminator, or that it belongs to the pool being operated on. A positive result is an instruction that accepts an account from an unrelated pool and completes without error.
  • Bitmap or index accounts passed by position. Wherever an extension, bitmap, oracle, or config account is supplied positionally, try substituting one from a second pool you control. A positive result is state changing on the victim pool while the supplied account belongs to yours.
  • Tick status flips. On a CLMM-style pool, try toggling a tick between enabled and disabled through operations that should not affect it, then swap across it. A positive result is liquidity being added or skipped without the corresponding price movement.
  • Extreme price boundaries. Exercise tick_lower and tick_upper values that force the extension path, at both the low and high ends. A positive result is the extension path being taken with an account that does not match the pool.
  • Repeated add and remove cycles. Repeat a zero-out and re-add sequence at the same tick and observe whether liquidity accumulates beyond what the deposited amounts justify. A positive result is a growing position balance that can be swapped out for the other token at a disproportionate rate.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 13 min read.

What happened, in plain words

Raydium is a place on the internet where people trade digital coins with each other without a middleman. It runs on a blockchain called Solana. A blockchain is a shared record book that many computers keep copies of, so nobody can quietly change the past. Raydium is what people call an automated market maker, or AMM: instead of buyers and sellers meeting, a pool of coins sits in a smart contract (a program that runs on the blockchain and moves coins according to fixed rules), and the program sets the price automatically.

On 10 January 2024, a security researcher who goes by the handle @riproprip found a serious flaw in one of Raydium's programs. The flaw was in a function called increase_liquidity, which is the piece of code that lets someone add coins to a pool. By abusing it, an attacker could amass an excessive amount of liquidity and then obtain disproportionately high amounts of a token through swaps. In plain terms, they could drain funds from the pools where ordinary people had deposited their coins.

Raydium fixed the problem quickly. The researcher was paid a bounty of $505,000 in RAY tokens, which is Raydium's own coin. A bounty is a reward a company offers to anyone who reports a security problem responsibly instead of exploiting it.

From the original on medium.com

This article walks through what Raydium is, what the flaw was, how it could have been used, and how it was fixed, explaining every term as it comes up.

What is Raydium?

Raydium is an AMM with an integrated central order book system, currently in its V3 iteration. An order book is the classic list of buy and sell orders you see on a stock exchange; Raydium combines that with the automatic pool model. "V3" just means the third major version of the software.

Users can do three main things on Raydium:

  • Provide liquidity: put coins into a pool so that other people can trade against them, and earn a share of the trading fees in return.
  • Swap: exchange one coin for another.
  • Stake the RAY token: lock up Raydium's own coin to earn extra yield, meaning extra coins over time.

A fundamental aspect of Raydium is the Concentrated Liquidity Market Maker, or CLMM. That is the feature the whole story revolves around, so it is worth understanding properly.

Concentrated Liquidity Market Maker (CLMM)

A liquidity pool is a pot of two coins, say coin X and coin Y, that anyone can trade against. The people who put coins in are called liquidity providers, or LPs. The pool needs enough coins on both sides to keep the price stable; if it is too thin, a single large trade moves the price a lot, which is called slippage.

Traditional AMM pools spread the LP's coins along a price curve from 0 to infinity. That means most of the money sits at prices that will never be reached, doing nothing.

CLMM pools let providers concentrate their liquidity within a specific price range. If you believe a coin will trade between two prices, you put your coins to work only in that band. While the price is inside the band, your money is active and earning fees. When the price leaves the band, your position stops earning and you are expected to adjust it.

The benefit is efficiency: a small CLMM pool can offer the same low slippage as a much larger traditional pool, because all of its capital is doing useful work at once.

How does CLMM work?

To see CLMM in action, the report uses Uniswap V3 as an example. Uniswap is another AMM, and its V3 release popularised this design. In that system, an LP can define a price range for their contribution. For instance, if an LP believes that the price of ETH will stay between $2,000 and $3,000, they can choose to concentrate their liquidity within this range.

  • When traders operate within this range, the LP's capital is used more intensively, earning them higher fees.
  • If the price moves outside of this range, their liquidity is no longer active, prompting them to adjust their position as needed.

This is both a more efficient use of capital for liquidity providers and leads to potentially better prices and deeper liquidity for the market to execute large trades with less impact on the price.

Vulnerability Analysis

A vulnerability is a weakness in a program that someone can use in a way the designers did not intend. The vulnerability here was located within the increase_liquidity function of the Raydium protocol. This function is essential for adding liquidity to a position within a liquidity pool.

It conducts several critical operations, including pool status validation (checking the pool is in a healthy state), token amount calculations based on user-provided maximums (working out how many coins the user must deposit), fee updates, and the actual increase of liquidity in the position's state (recording the new deposit).

What are ticks?

Ticks represent discrete price points in a CLMM. Each tick corresponds to a specific price level, and the space between ticks represents the smallest possible price movement in the pool. Think of a ruler: the ticks are the little marks along the edge, and the price can only sit on a mark, not between them.

By defining a range with tick_lower and tick_upper, an LP specifies the boundaries within which their liquidity will be active.

  • tick_lower: This is the lower boundary of the price range where the LP's liquidity will be active. It represents the lowest price point at which the LP is willing to provide liquidity.
  • tick_upper: This is the upper boundary of the price range where the LP's liquidity will be active. It represents the highest price point at which the LP is willing to provide liquidity.

The flaw was specifically in the conditional handling of the tickarray_bitmap_extension. A conditional is an if in code: do one thing when a condition is true, another when it is false.

The tickarray_bitmap_extension plays a crucial role in managing the pool's pricing at extreme boundaries (very high or low prices). It also acts as an extended index to manage a larger range of price ticks which helps track which ticks have been initialized (i.e. have non-zero liquidity) beyond the default capacity of the system. A bitmap is a compact way of storing many yes/no flags: each tick has a bit that says whether it is switched on or off. The extension is an extra page of those flags for prices far away from the current one.

The conditional logic decides whether an account from remaining_accounts is needed as part of the operation. This account is presumably related to handling the tick array bitmap extension. This function is designed to increase liquidity in a specific position within a liquidity pool using the remaining_accounts vector.

Here are a few details you should know about the remaining_accounts function:

  • remaining_accounts is a vector that contains all accounts that were passed into the instruction but are not declared in the Accounts struct. This is useful when you want your function to handle a variable amount of accounts, e.g. when initializing a game with a variable number of players.
  • While remaining_accounts provides significant flexibility, it also necessitates careful validation within the smart contract to ensure security. The contract must validate the accounts in remaining_accounts to confirm they are of the expected type and have the correct permissions for the intended operations.
  • Please refer to the following documentation for more information on remaining_accounts: https://www.anchor-lang.com/docs/the-program-module#context

Two terms in there need unpacking. An account on Solana is a storage slot on the blockchain that holds data, such as a pool's state or a user's balance. An instruction is a single command sent to a program, like "add liquidity". The Accounts struct is the fixed, declared list of accounts an instruction expects; the framework checks those automatically. Anything extra the caller tacks on ends up in remaining_accounts, and because the framework does not know what those are, the program itself has to check them.

The vulnerability arises because the function fails to verify whether remaining_accounts[0] is the accurate TickArrayBitmapExtension account linked to the current state of the pool. remaining_accounts[0] means the first extra account in that list. This oversight permits an attacker to execute liquidity operations at arbitrary price boundaries as described in the vulnerability demonstration section below.

Normal behaviour

In normal operation, when a user adds liquidity:

  1. The increase_liquidity function validates the current state of the pool and the price range specified by tick_lower and tick_upper.
  2. It calculates the token amounts needed based on the user's input and current pool state.
  3. It updates the fee structure and increases the liquidity in the specified position.
  4. If the price range specified requires the tickarray_bitmap_extension (because it is at an extreme boundary), the function uses an account from remaining_accounts to manage this extension properly.

Step 4 is where the trust problem lives. The function takes the caller's word for which extension account to use, instead of checking that it belongs to this pool.

Attack behaviour

Due to the bug, the function fails to verify whether remaining_accounts[0] is the accurate TickArrayBitmapExtension account linked to the current state of the pool.

This oversight permits an attacker to execute liquidity operations at arbitrary price boundaries. In everyday terms: imagine a bank teller who accepts whatever ledger book you hand over, without checking it is the branch's own ledger. You could bring a ledger from a branch you control, with whatever numbers you like written in it, and the teller would act on those numbers.

Vulnerability Demonstration

The whitehat showed how to exploit the system by manipulating the tickarray_bitmap_extension. This allowed them to bypass the intended removal of liquidity at specific price points, known as ticks. "Whitehat" means a researcher who finds flaws and reports them to be fixed, rather than using them for personal gain.

Steps of the attack

  1. Create a secondary pool: The attacker sets up a secondary pool.
  2. Target a tick: They open a position at a specific tick they want to exploit in the primary pool.
  3. Zero out and manipulate liquidity: They reduce the liquidity at this tick to zero and then increase it in a way that incorrectly uses the tickarray_bitmap of the primary pool.

They then need a position in the victim pool at the tick they intend to tamper with, and they need to drive that tick's recorded liquidity to zero so that later operations treat it as empty and skip the usual checks.

This manipulation lets the attacker "flip" a tick's status in the bitmap.Transactions that should have adjusted liquidity at certain prices bypass these checks, allowing unauthorised liquidity increases.

To clarify the exploit process using a practical example, consider price points A, B, and C, with A < B < C and both A and B within the lower range of the bitmap_array.

  1. Identify a victim pool.
  2. Execute a swap to shift the price to just above point A (A+1).
  3. Create liquidity within the price range B to C.
  4. Perform a swap across B without crossing C, which adds liquidity to the pool state due to the manipulated tickarray_bitmap.
  5. Execute the described attack to switch the tickarray_bitmap status at B to DISABLED, allowing a swap at A+1 to skip over B without affecting liquidity.
  6. Repeat the attack to re-enable the tickarray_bitmap at B.
  7. Perform another swap over B without crossing C, effectively doubling the liquidity erroneously.
  8. This process can be repeated as many times as desired to amass an excessive amount of liquidity.
  9. Following this procedure, swaps directed towards C will yield disproportionately high amounts of Token A, indicating the vulnerability has been fully exploited.
  10. If the goal is to acquire Token B, the process can be replicated at the higher end of the price spectrum.

Walking through it: step 2 moves the market price to a spot just above A, so the next trades will pass through B. Step 3 places real liquidity between B and C. Step 4 trades across B; because the bitmap has been manipulated, the swap adds liquidity to the pool state. Steps 5 and 6 toggle B off and on again, which is the flip at the heart of the bug. Step 7 repeats the trade and effectively doubles the liquidity erroneously. Step 8 says you can loop this as long as you like. Step 9 is the payoff: swaps directed towards C yield disproportionately high amounts of Token A. Step 10 notes the same trick works at the top end of the price range if the attacker wants Token B instead.

The core issue is the ability to "flip" a tick's status within the tickarray_bitmap, allowing liquidity to be added without proper checks. This leads to significant discrepancies in liquidity management and compromises the integrity of the liquidity pool.

Vulnerability Fix

The vulnerability was addressed by Raydium through a critical update, as documented in their GitHub commit:

[## Fix: fix for open position v2 with zero liquidity · raydium-io/raydium-clmm@83b5a47

Open-Source Concentrated Liquidity Market Maker. Contribute to raydium-io/raydium-clmm development by creating an…

github.com](https://github.com/raydium-io/raydium-clmm/commit/[redacted]?source=post_page-----c6aae4527ed6------------------------------------------)

A commit is a saved change in a code repository, with a message describing it. The correction involved adding a security check to ensure the correct application of the tickarray_bitmap_extension:

This fix introduces a validation step to confirm that the remaining_accounts[0] is the correct TickArrayBitmapExtension account associated with the pool's current state.

In plain words, the teller now checks that the ledger book you hand over is the branch's own before writing anything in it. This ensures the proper handling of liquidity operations involving extreme price boundaries, thereby preventing the exploitation of this vulnerability in the future.

Acknowledgements

We would like to thank the whitehat @riproprip for doing an amazing job in responsibly disclosing such an important bug. Big props also to the Raydium team who quickly responded to the report and patched the issue.

This bugfix review was written by Immunefi triager, Arbaz Hussain. A triager is the person at a bug bounty platform who reads incoming reports, checks them, and routes them to the right team.

If you're a developer or a whitehat considering a lucrative bug-hunting career in web3, this message is for you. Web3 is the umbrella term for blockchain-based applications. With 10 to 100 times the rewards commonly found in web2 (the traditional web), your efforts will pay off exponentially by switching to web3.

Check out the Web3 Security Library, and start earning rewards on Immunefi, the leading bug bounty platform for web3 with the world's biggest payouts.

Glossary

  • Account: a storage slot on a blockchain that holds data, such as a pool's state or a user's balance.
  • AMM (Automated Market Maker): a program that holds pools of coins and sets prices automatically, so people can trade without a human counterparty.
  • Anchor: a framework for writing Solana programs that checks declared accounts for you.
  • Bitmap: a compact way of storing many yes/no flags, one bit per item.
  • Blockchain: a shared record book kept by many computers, which makes past entries hard to change.
  • Bounty: a reward paid for responsibly reporting a security flaw.
  • CLMM (Concentrated Liquidity Market Maker): an AMM design where liquidity providers choose a price range in which their coins are active.
  • Commit: a saved change in a code repository, with a message describing it.
  • Conditional: an if in code that chooses between paths based on a test.
  • Instruction: a single command sent to a blockchain program, such as "add liquidity".
  • Liquidity: coins sitting in a pool, available for others to trade against.
  • Liquidity provider (LP): someone who deposits coins into a pool to earn fees.
  • Pool: a pot of two coins that people trade against.
  • Position: an LP's individual deposit, defined by a price range.
  • remaining_accounts: the list of extra accounts passed to an instruction that are not declared in the Accounts struct, so the program must validate them itself.
  • Slippage: how much the price moves because of a single trade; thin pools suffer more.
  • Smart contract: a program on a blockchain that moves coins according to fixed rules.
  • Solana: the blockchain Raydium runs on.
  • Staking: locking up a coin to earn extra coins over time.
  • Swap: exchanging one coin for another.
  • Tick: a discrete price point in a CLMM; the smallest price step in the pool.
  • tick_lower / tick_upper: the lower and upper price boundaries of an LP's active range.
  • tickarray_bitmap_extension: an extra bitmap that tracks which ticks are initialised at extreme prices, beyond the default capacity.
  • Triager: the person at a bug bounty platform who reads and routes incoming reports.
  • Vector: an ordered list of items in code.
  • Vulnerability: a weakness in a program that can be used in a way the designers did not intend.
  • Web2 / Web3: the traditional web, and the blockchain-based web respectively.
  • Whitehat: a researcher who finds flaws and reports them to be fixed rather than exploiting them.
  • Yield: extra coins earned over time from an asset.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.