Exploitable vulnerability in SDEX
The SDEX order matching engine executes trades at a 1:1 ratio when the traded amount is extremely small (0.0000001), ignoring the price set in the order. An attacker can repeatedly buy valuable assets like BTC for almost nothing and sell them at market price for profit.
- Triaged in 9 hours
- Fixed in 6 months
- Disclosed after 7 months
Hi,
Last Thursday I discovered the exploitable vulnerability in SDEX. I immediately reported the bug directly to Jed by email and he confirmed it.
It's all about rounding during trades. You see, I found that orders are always executed if the price matches market, even if the amount is as small as 0.0000001. A minimum traded amount is also 0.0000001, so if we are buying the more valued asset (say, we are buying BTC for XLM), an asset is traded 1:1 regardless of the price set in MANAGE_ORDER operation.
It's not a problem if traded assets have roughly equal value (for example, MOBY/XLM rate is 0.34, CNY/XLM - 0.66). Nobody sanely will spend 100 stroops for fees just to trade 0.0000001 CNY with a slightly better rate.
Now let's consider trading on XLM/BTC pair. Market exchange price is ~ 37,000 XLM/BTC.
I create an order selling 0.0000001 XLM with a price, say, 50,000 XLM/BTC and submit it to the network. The price is higher than avg market, so the order is executed immediately. That's it, I bought 0.0000001 BTC for 0.0000001 XLM. If I trade back BTC back to lumens at the market price I'll get 0.0037000 XLM, which is much greater than originally spent 0.0000101 XLM.
Proof
(examples on Mainnet)
- ManageOffer: https://horizon.stellar.org/operations/71512944940158977
- Effects: https://horizon.stellar.org/operations/71512944940158977/effects
(reproduced on Testnet)
- ManageOffer: https://horizon-testnet.stellar.org/operations/34556568129245185
- Account effects: https://horizon-testnet.stellar.org/accounts/GDD7OTUPGR7FMJZLSLYLXTUCPG5IA5UTSPXXZWYRX3HJMGMWWSKCCH65/effects
- Account balance: https://horizon-testnet.stellar.org/accounts/GDD7OTUPGR7FMJZLSLYLXTUCPG5IA5UTSPXXZWYRX3HJMGMWWSKCCH65
Attack vector
- Create 20 accounts and fund with, say, 120 XLM each.
- Code a primitive bot that submits a transaction with 100 ManageOffer operations for each account every 4 seconds (roughly 900*100 =90,000 operations per hour for each account).
- Rent a cheap cloud server and launch the bot.
- Once in 10 minutes or so bot should sell all traded BTC at market price and send all profits to a master-account.
- Repeat until there is at least one open offer. Then switch to another asset (BTC issued by another anchor, or, for example, ETH tokens).
Attacker hourly profit:
(0.0037-0.0000101) * 20 * 90000=6641.82 XLM/hour.
If the attacker is greedy, he may initially create up to 50 accounts instead of 20. However, it will immediately affect the overall network performance and everybody will be alarmed. A witty attacker may rather run a bot with 20-30 accounts on a Sunday night to gain a maximum profit.
Traders that are selling BTC (or any other pricey asset) are effectively loosing all money, because offers are traded at a fraction (1/37,000 in case of BTC) of the market value despite the fact that the initial price was set correctly.
Not sure if this issue is eligible for reward, as Jed mentioned that you are working on something like this. If yes, here is my account address:
GB3VFWJW7ZSY2VX666SMVQNHAOMTQ6Y2723CU4XL26F455574JNLC54Z
Impact
An attack may lead to a loss of a substantial amount of users' funds. It can't be stopped or prevented without the entire Stellar Network upgrade due to the nature of the distributed ledger.
Once an attacker is blocked, he may change an IP or the target Stellar Core validator node and continue an attack. In such case he can still more than 100,000 XLM (~20,000 USD) during the first day of attack.
Context
Stellar is a distributed ledger, and SDEX is its built-in decentralised exchange. Users place orders with the MANAGE_ORDER operation, and the network matches them against the order book. The researcher, orbitlens, found a rounding flaw in how trades are executed: an order is always executed if the price matches the market, even when the traded amount is as small as 0.0000001. Because the minimum traded amount is also 0.0000001, buying the more valuable asset in a pair results in the two assets being traded 1:1, regardless of the price set in the MANAGE_ORDER operation.
The researcher reported the bug directly to Jed by email and Jed confirmed it.
Setup
The proof was produced on two networks:
- Mainnet, using Horizon at
horizon.stellar.org. - Testnet, using Horizon at
horizon-testnet.stellar.org.
The testnet account used for the reproduction is [redacted]. The attack vector uses 20 funded accounts (up to 50 if the attacker is greedy), a bot that submits transactions, and a cheap cloud server to run it from. The researcher also gives an account address for a reward:
[redacted]
Finding it
The researcher found that orders are always executed if the price matches the market, even if the amount is as small as 0.0000001. Since the minimum traded amount is also 0.0000001, buying the more valuable asset in a pair (for example, buying BTC for XLM) means the assets are traded 1:1 regardless of the price set in the MANAGE_ORDER operation.
On pairs where the two assets have roughly equal value this is harmless. The report gives MOBY/XLM at a rate of 0.34 and CNY/XLM at 0.66, and notes that nobody will sanely spend 100 stroops in fees just to trade 0.0000001 CNY at a slightly better rate.
The problem appears on a pair with a large price difference. On XLM/BTC the market exchange price is about 37,000 XLM/BTC. An order selling 0.0000001 XLM at a price of, say, 50,000 XLM/BTC is above the average market price, so it is executed immediately, and the seller ends up buying 0.0000001 BTC for 0.0000001 XLM. Trading that BTC back to lumens at the market price yields 0.0037000 XLM, far more than the 0.0000101 XLM originally spent.
Exploitation
Step 1: Place a tiny sell order above market price
Create an order selling 0.0000001 XLM at a price above the market, for example 50,000 XLM/BTC, and submit it to the network. Because the price is higher than the average market price, the order is executed immediately. The result is that 0.0000001 BTC is bought for 0.0000001 XLM.
Step 2: Trade the acquired asset back at market price
Sell the acquired BTC back to lumens at the market price. At roughly 37,000 XLM/BTC, 0.0000001 BTC returns 0.0037000 XLM, against the 0.0000101 XLM originally spent.
Step 3: Confirm on Mainnet and Testnet
The report provides Horizon links as proof. On Mainnet:
- ManageOffer: https://horizon.stellar.org/operations/71512944940158977
- Effects: https://horizon.stellar.org/operations/71512944940158977/effects
On Testnet, where the issue was reproduced:
- ManageOffer: https://horizon-testnet.stellar.org/operations/34556568129245185
- Account effects: https://horizon-testnet.stellar.org/accounts/[redacted]/effects
- Account balance: https://horizon-testnet.stellar.org/accounts/[redacted]
Step 4: Scale with a bot
The attack vector is described as follows:
- Create 20 accounts and fund with, say, 120 XLM each.
- Code a primitive bot that submits a transaction with 100 ManageOffer operations for each account every 4 seconds (roughly 900*100 = 90,000 operations per hour for each account).
- Rent a cheap cloud server and launch the bot.
- Once in 10 minutes or so the bot should sell all traded BTC at market price and send all profits to a master account.
- Repeat until there is at least one open offer. Then switch to another asset (BTC issued by another anchor, or, for example, ETH tokens).
The attacker's hourly profit is given as:
(0.0037-0.0000101) * 20 * 90000=6641.82 XLM/hour.
If the attacker is greedy, he may initially create up to 50 accounts instead of 20. That would immediately affect overall network performance and everybody would be alarmed. A witty attacker may instead run a bot with 20 to 30 accounts on a Sunday night to gain maximum profit.
Root cause
The flaw is in the rounding during trades. The minimum traded amount of 0.0000001 combined with immediate execution at any matching price means the two assets move 1:1 at the smallest unit. The two assets move 1:1 at the smallest unit. The report does not describe the code path that performs the rounding; it only states the behaviour.
Impact
Traders selling BTC, or any other pricey asset, effectively lose all their money, because offers are traded at a fraction of the market value (1/37,000 in the case of BTC) despite the initial price being set correctly. An attack may lead to a loss of a substantial amount of users' funds. It cannot be stopped or prevented without an entire Stellar Network upgrade, due to the nature of the distributed ledger. Once an attacker is blocked, he may change an IP or the target Stellar Core validator node and continue the attack. In such a case he can still make more than 100,000 XLM (about 20,000 USD) during the first day of the attack.
Fix
The report does not describe a fix. It notes that Jed mentioned they were working on something like this, and the researcher was unsure whether the issue was eligible for a reward.
Variants to test
- Pairs with a large price gap. Look for any order book where one asset is worth orders of magnitude more than the other, and try the smallest allowed amount at a price above market. A positive result is an immediate fill that credits the valuable asset at a 1:1 ratio rather than at the stated price.
- Minimum amount boundaries. Test the smallest tradable unit on other exchanges and matching engines. A positive result is a fill that ignores the price because the amount rounds to the minimum unit.
- Other issuers of the same asset. The report suggests switching to BTC issued by another anchor, or ETH tokens, once the first asset's open offers are exhausted. A positive result is the same 1:1 behaviour on the new asset.
What happened, in plain words
Stellar is a public payment network and its built-in exchange is called SDEX. People use it to swap one digital asset for another, a bit like a currency exchange desk at an airport. You say what you want to sell, what you want to buy, and at what price, and the network tries to match you with someone on the other side.
A security researcher called orbitlens found a flaw in how the exchange handles very small trades. When the price of an order matched the market, the exchange would execute it even if the amount was tiny, as small as 0.0000001 of an asset. Because that same tiny number is also the smallest amount the system allows, buying the more valuable asset in a pair ended up happening at a one-to-one rate, no matter what price the person had set. In plain terms: you could pay a microscopic amount of a cheap asset and receive the same microscopic amount of an expensive asset, then sell the expensive asset at the real market price and make a profit.
The researcher reported the bug directly to Jed by email and Jed confirmed it.
The product and the feature
Stellar is a distributed ledger: a shared record of who owns what, kept by many computers instead of one company. SDEX is its decentralised exchange, meaning the buying and selling happens on that shared record rather than on a private website.
Users place orders with an operation called MANAGE_ORDER. An operation is a single instruction sent to the network, like a line on a form. The network matches orders against an order book, which is simply the list of all open buy and sell orders waiting to be filled.
The researcher found a rounding flaw in how trades are executed. Rounding is what happens when a number cannot be stored exactly and gets cut to a fixed number of decimal places, the way a shop might round 1.999 pounds to 2.00. Here the smallest unit the system handles is 0.0000001. An order is always executed if the price matches the market, even when the traded amount is as small as that minimum. Because the minimum traded amount is also 0.0000001, buying the more valuable asset in a pair results in the two assets being traded 1:1, regardless of the price set in the MANAGE_ORDER operation.
Setting up the proof
The proof was produced on two networks:
- Mainnet, the real Stellar network, using Horizon at
horizon.stellar.org. - Testnet, a practice network for testing, using Horizon at
horizon-testnet.stellar.org.
Horizon is the public interface, or API, that lets programs talk to the Stellar network. An API is a way for one piece of software to ask another for information or to perform an action, like a waiter taking your order to the kitchen. Horizon lets you look up operations, effects and account balances.
The testnet account used for the reproduction is [redacted]. An account address is like a bank account number: it identifies where funds are held, but it does not by itself let anyone spend them.
The attack vector uses 20 funded accounts (up to 50 if the attacker is greedy), a bot that submits transactions, and a cheap cloud server to run it from. A bot is a program that performs repetitive actions automatically. A cloud server is a rented computer in a data centre that runs around the clock.
The researcher also gives an account address for a reward:
[redacted]
The researcher gives this address in case the issue is eligible for a reward.
Finding it
The researcher found that orders are always executed if the price matches the market, even if the amount is as small as 0.0000001. Since the minimum traded amount is also 0.0000001, buying the more valuable asset in a pair (for example, buying BTC for XLM) means the assets are traded 1:1 regardless of the price set in the MANAGE_ORDER operation.
On pairs where the two assets have roughly equal value this is harmless. The report gives MOBY/XLM at a rate of 0.34 and CNY/XLM at 0.66, and notes that nobody will sanely spend 100 stroops in fees just to trade 0.0000001 CNY at a slightly better rate. A stroop is the smallest unit of XLM, the way a penny is the smallest unit of a pound.
The problem appears on a pair with a large price difference. On XLM/BTC the market exchange price is about 37,000 XLM/BTC. An order selling 0.0000001 XLM at a price of, say, 50,000 XLM/BTC is above the average market price, so it is executed immediately, and the seller ends up buying 0.0000001 BTC for 0.0000001 XLM. Trading that BTC back to lumens at the market price yields 0.0037000 XLM, far more than the 0.0000101 XLM originally spent. Lumens is another name for XLM.
Exploitation
Step 1: Place a tiny sell order above market price
Create an order selling 0.0000001 XLM at a price above the market, for example 50,000 XLM/BTC, and submit it to the network. Because the price is higher than the average market price, the order is executed immediately. The result is that 0.0000001 BTC is bought for 0.0000001 XLM.
Why this step: the order is above the average market price, so it is executed immediately, and 0.0000001 BTC is bought for 0.0000001 XLM.
Step 2: Trade the acquired asset back at market price
Sell the acquired BTC back to lumens at the market price. At roughly 37,000 XLM/BTC, 0.0000001 BTC returns 0.0037000 XLM, against the 0.0000101 XLM originally spent.
Why this step: the profit only becomes real money once the valuable asset is sold at the normal market rate. The attacker is converting the cheaply obtained BTC back into XLM at the price everyone else uses.
Step 3: Confirm on Mainnet and Testnet
The report provides Horizon links as proof. On Mainnet:
- ManageOffer: https://horizon.stellar.org/operations/71512944940158977
- Effects: https://horizon.stellar.org/operations/71512944940158977/effects
On Testnet, where the issue was reproduced:
- ManageOffer: https://horizon-testnet.stellar.org/operations/34556568129245185
- Account effects: https://horizon-testnet.stellar.org/accounts/[redacted]/effects
- Account balance: https://horizon-testnet.stellar.org/accounts/[redacted]
Why this step: these links let anyone check the transactions on the public record. An operation link shows a single instruction, an effects link shows what changed as a result (for example, which balances moved), and an account balance link shows the current holdings of an account. Seeing the same behaviour on both the real network and the practice network makes the finding stronger.
Step 4: Scale with a bot
The attack vector is described as follows:
- Create 20 accounts and fund with, say, 120 XLM each.
- Code a primitive bot that submits a transaction with 100 ManageOffer operations for each account every 4 seconds (roughly 900*100 = 90,000 operations per hour for each account).
- Rent a cheap cloud server and launch the bot.
- Once in 10 minutes or so the bot should sell all traded BTC at market price and send all profits to a master account.
- Repeat until there is at least one open offer. Then switch to another asset (BTC issued by another anchor, or, for example, ETH tokens).
Why these steps: one tiny trade earns a tiny profit, so the attacker repeats it many times across many accounts. A master account is simply a central account where the profits are collected. An anchor is an organisation that issues a token on Stellar and promises to hold the equivalent real asset. When the first asset's open offers run out, the attacker moves to another asset with the same trick.
The attacker's hourly profit is given as:
(0.0037-0.0000101) * 20 * 90000=6641.82 XLM/hour.
This line multiplies the profit from one tiny trade by the number of accounts and the number of operations per hour. The result is the estimated hourly gain in XLM.
If the attacker is greedy, he may initially create up to 50 accounts instead of 20. That would immediately affect overall network performance and everybody would be alarmed. A witty attacker may instead run a bot with 20 to 30 accounts on a Sunday night to gain maximum profit.
Root cause
The flaw is in the rounding during trades. The minimum traded amount of 0.0000001 combined with immediate execution at any matching price means the two assets move 1:1 at the smallest unit. The two assets move 1:1 at the smallest unit. The report does not describe the code path that performs the rounding; it only states the behaviour.
Impact
Traders selling BTC, or any other pricey asset, effectively lose all their money, because offers are traded at a fraction of the market value (1/37,000 in the case of BTC) despite the initial price being set correctly. An attack may lead to a loss of a substantial amount of users' funds. It cannot be stopped or prevented without an entire Stellar Network upgrade, due to the nature of the distributed ledger. Once an attacker is blocked, he may change an IP or the target Stellar Core validator node and continue the attack. In such a case he can still make more than 100,000 XLM (about 20,000 USD) during the first day of the attack. An IP is the numeric address of a computer on the internet, and a Stellar Core validator node is a computer that helps maintain the shared ledger.
Fix
The report does not describe a fix. It notes that Jed mentioned they were working on something like this, and the researcher was unsure whether the issue was eligible for a reward.
Variants to test
- Pairs with a large price gap. Look for any order book where one asset is worth orders of magnitude more than the other, and try the smallest allowed amount at a price above market. A positive result is an immediate fill that credits the valuable asset at a 1:1 ratio rather than at the stated price.
- Minimum amount boundaries. Test the smallest tradable unit on other exchanges and matching engines. A positive result is a fill that ignores the price because the amount rounds to the minimum unit.
- Other issuers of the same asset. The report suggests switching to BTC issued by another anchor, or ETH tokens, once the first asset's open offers are exhausted. A positive result is the same 1:1 behaviour on the new asset.
- Rate limiting is when a service deliberately slows down or blocks repeated requests from the same source.
Glossary
- Stellar: a distributed ledger, a shared record of ownership kept by many computers.
- SDEX: Stellar's built-in decentralised exchange, where users trade digital assets.
- Distributed ledger: a record of transactions maintained by many computers rather than one central authority.
- Operation: a single instruction sent to the Stellar network, such as placing an order.
MANAGE_ORDER: the Stellar operation used to place, change or cancel an order on the exchange.- Order book: the list of all open buy and sell orders waiting to be matched.
- Rounding: cutting a number to a fixed number of decimal places, which can change its value slightly.
- Stroop: the smallest unit of XLM, the way a penny is the smallest unit of a pound.
- Lumen: another name for XLM, the native asset of Stellar.
- Mainnet: the real, live Stellar network where actual funds are used.
- Testnet: a practice Stellar network for testing, with no real value.
- Horizon: the public API that lets programs read from and send to the Stellar network.
- API: a way for one piece of software to ask another for information or to perform an action.
- Account address: a public identifier for an account, like a bank account number.
- Bot: a program that performs repetitive actions automatically.
- Cloud server: a rented computer in a data centre that runs continuously.
- Master account: a central account where an attacker collects profits.
- Anchor: an organisation that issues a token on Stellar and holds the equivalent real asset.
- IP: the numeric address of a computer on the internet.
- Stellar Core validator node: a computer that helps maintain the Stellar ledger.
- CVSS: a standard score for the severity of a security vulnerability, from 0 to 10.
- Business logic error: a flaw in the rules of how a system is supposed to work, rather than in its code syntax.
- Rate limiting: deliberately slowing down or blocking repeated requests from the same source.
- Effects: the changes that result from an operation, such as balances moving.