Skip to content
Business logic Write-up Advanced

Synthetix

Synthetix used the wrong variable when calculating the amount received in an exchange, using the full source amount instead of the amount after settlement. An attacker could exploit this to receive more destination tokens than they should, potentially draining funds.

medium.com

Published on medium.com

Synthetix

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. 5 min read.

Context

Synthetix is a smart contract protocol, and the feature at the centre of this report is fee reclamation and rebate handling as described in Synthetix's SIP 236. When a trade is made, the destination amount is not finalised immediately: there is a cooldown period, after which any difference between the rate at the time of the trade and the rate at the time of settlement is either reclaimed from the user or rebated to them. The SIP defines the owing or owed calculation as follows:

Text
The calculation of owing or owed is as follows:Amount * (1 — feeRate) * (srcRate/destRate — newSrcRate/newDestRate)If the result is negative, the amount is owed as a rebate.

In other words, if the price of the destination token falls during the cooldown, the synth rebates the difference to the user in the destination token.

Setup

The report does not describe any accounts, roles, tooling or versions used by the researcher. The relevant code is the Synthetix Exchanger contract, and the report references specific line numbers within it (L442, L486) and links to the _convert function at lines 574 to 596 of contracts/Exchanger.sol in the Synthetix repository. The report's example scenario involves a pending rebate: a trade whose destination token price has dropped during the cooldown period, so that the user is owed a rebate in the destination token.

Finding it

The researcher identified that Synthetix used the wrong variable when computing the amount to be received on a subsequent exchange. The rebate logic itself is intended to compensate the user when the destination price decreases, but the exchange path failed to account for the pending rebate when sizing the new trade.

Specifically, if a user calls exchange before reclaiming their rebate, the exchange sourceAmount is inflated. When an exchange occurs, the amount the user will receive, amountReceived, is calculated inside the internal call to _exchange. Within _exchange, amountReceived is assigned on L486:

Text
amountReceived = _deductFeesFromAmount(entry.destinationAmount, entry.exchangeFeeRate);

Looking at how entry.destinationAmount is calculated on L442, effectiveValueAndRatesAtRound is called with the parameters sourceCurrencyKey, sourceAmount, destinationCurrencyKey, entry.roundIdForSrc and entry.roundIdForDest.

The problem is that sourceAmount is passed here instead of sourceAmountAfterSettlement. The full source amount, before the outstanding debts are applied, is therefore used to calculate the destination amount. When _convert runs, only the sourceAmountAfterSettlement is burned, yet the amountReceived computed from the full sourceAmount is issued.

Exploitation

Step 1: Create a pending rebate

A user exchanges one synth for another, for example sUSD to sBTC, and the price of the destination token (sBTC) drops during the cooldown period. Under the SIP 236 rules, the synth owes the user a rebate for the difference.

Step 2: Exchange again before reclaiming

Instead of reclaiming the rebate first, the user calls exchange again. Because the pending debt has not been settled, the sourceAmount used in the exchange is inflated relative to what will actually be burned.

Step 3: Receive an inflated destination amount

Inside _exchange, entry.destinationAmount is derived from the full sourceAmount via effectiveValueAndRatesAtRound, and amountReceived is then computed from it on L486. The subsequent _convert call burns only sourceAmountAfterSettlement, but the larger amountReceived is issued to the user. The user therefore receives more of the destination token than the settled source amount entitles them to.

To summarise the report's own description: _exchange used the sourceAmount variable to calculate the amount that has to be rebated to the destination, but did not perform a post-reclaim check on it to verify whether the amount is accurate relative to the destination token value.

The report's worked example is an exchange from sUSD to sBTC where the price of sBTC dropped during the cooldown period. The synth rebates the difference in sBTC, but the difference is rebated in the destination token and the synth failed to calculate the rebate value of sBTC in sUSD.

Root cause

The mistake is a variable selection error in the exchange path. The destination amount is computed from sourceAmount, the pre-settlement figure, rather than from sourceAmountAfterSettlement. The burn side of the operation correctly uses the settled amount, so the two sides of the trade are sized from different quantities. The report says _exchange did not do a post-reclaim check on the amount to see whether it was accurate relative to the destination token value.

Impact

The report describes the bug as critical. A user with a pending rebate could call exchange before reclaiming it and receive an amount of the destination token calculated from the full, unsettled source amount, while only the settled amount is burned. The user receives more of the destination token than the settled source amount entitles them to. The condition required is a pending rebate, which arises when the destination token price falls during the cooldown period. The report does not quantify the maximum loss or state how many times the pattern could be repeated.

Fix

Synthetix patched the vulnerability at commit [redacted] in the Synthetix repository. The report does not describe the contents of the patch beyond linking to that commit.

Variants to test

  1. Look for other paths where a pre-settlement amount is used to size an output while a post-settlement amount is used to debit the user. A positive result is any function where the input to a rate or value calculation differs from the quantity actually burned or transferred.
  2. Check rebate and fee-reclamation flows in other protocols for missing post-settlement validation. A positive result is a rebate or credit that is denominated in one asset but never converted back to the accounting asset before being reused.
  3. Test sequences where a user acts again before a pending settlement completes. A positive result is a second operation whose size depends on unsettled balances, producing an output larger than the settled input supports.
  4. Review internal conversion helpers for parameter mismatches, such as passing a gross amount where a net amount is expected. A positive result is a call site where the variable name and the surrounding settlement logic disagree.

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. 11 min read.

What happened, in plain words

Synthetix is a set of programs that run on a blockchain (a shared, public record of transactions that many computers keep copies of). These programs are called smart contracts: code that lives on the blockchain and moves digital tokens around according to fixed rules, with no human in the middle. Synthetix lets people swap one kind of digital token for another, and it has a special rule for handling price changes that happen just after a swap.

On 2 May 2022, a security researcher who goes by the name thunderdeep14 found a serious mistake in that rule. He reported it through Immunefi, a platform where people who find bugs in blockchain projects can report them safely and get paid. The Synthetix team fixed the bug shortly afterwards. He was paid $100,000, the maximum reward for a critical bug in Synthetix's programme, plus another $50,000 because Synthetix takes part in Nexus Mutual's matching programme, which tops up payouts. That made $150,000 in total.

From the original on medium.com

The rest of this article explains, from scratch, what the bug was, how someone could have taken advantage of it, and how it was fixed. You do not need to know how to program to follow it; every term is explained the first time it appears, and there is a glossary at the end.

Background: what Synthetix does and what a "synth" is

A token is a digital unit of value recorded on a blockchain. You can think of it like a token in an arcade: it is not cash, but it stands for value and can be exchanged for things.

Synthetix issues tokens called synths (short for synthetic assets). A synth is designed to track the price of something else, such as a currency or a commodity. In this report the two synths that matter are sUSD (a synth that tracks the US dollar) and sBTC (a synth that tracks Bitcoin).

A smart contract is a program stored on the blockchain. Once deployed, anyone can call its functions, and it runs exactly as written. The particular contract involved here is called Exchanger; its job is to handle swaps between synths.

The feature: fee reclamation and rebates

When you swap one synth for another on Synthetix, the amount you receive is not final straight away. There is a short waiting period called a cooldown. The reason is that prices on a blockchain are not instant: the price the contract uses is fed in from outside, and it can move between the moment you make the trade and the moment the trade is finalised.

After the cooldown, Synthetix compares the price at the time of the trade with the price at settlement. If the price moved in a way that means you got slightly too much, the protocol reclaims the difference from you. If it moved so that you got slightly too little, the protocol pays you back the difference, which is called a rebate.

The rules for this are written down in a document called SIP 236 (SIP stands for Synthetix Improvement Proposal: a public design document that describes how a feature should work). The report quotes the key section:

Text
The calculation of owing or owed is as follows:Amount * (1 — feeRate) * (srcRate/destRate — newSrcRate/newDestRate)If the result is negative, the amount is owed as a rebate.

Line by line, in plain words:

  • Amount * (1 — feeRate) is the size of your trade after the protocol's fee is taken off. feeRate is the percentage fee, so 1 — feeRate leaves you with the rest.
  • srcRate/destRate is the exchange rate between the two synths at the time you made the trade. srcRate is the price of the synth you are giving up, destRate is the price of the synth you are getting.
  • newSrcRate/newDestRate is the same ratio at the time of settlement, using the updated prices.
  • Subtracting the second ratio from the first tells you how much the rate moved against you or in your favour.
  • If the result is negative, you were short-changed, so the amount is owed back to you as a rebate.

In everyday terms: imagine you order a foreign currency at the bank, and by the time you collect it the exchange rate has shifted. The bank either asks you for a little more or hands you a little back. Synthetix does the same thing automatically, in synths.

The report's summary of the rule is that if the price of the destination token (the one you are buying) falls during the cooldown, the synth rebates the difference to you in that destination token.

The setup for the bug

The report does not describe any particular accounts, tools or software versions used by the researcher. What matters is the code of the Exchanger contract, and the report points to specific line numbers in it: L442 and L486. It also links to the _convert function at lines 574 to 596 of contracts/Exchanger.sol in the Synthetix code repository.

The report's example scenario involves a pending rebate: a trade you have made whose destination token price dropped during the cooldown, so that the protocol owes you a rebate in the destination token, but you have not yet collected it.

Finding the bug

The researcher noticed that Synthetix used the wrong variable when working out how much you should receive on a later exchange. A variable is simply a named box in a program that holds a value; using the wrong one is like picking up the wrong envelope because two envelopes look similar.

The rebate logic is meant to compensate you when the destination price falls. But the exchange code did not take the pending rebate into account when sizing the new trade.

Specifically, if you call the exchange function (the function that performs a swap) before collecting your rebate, the sourceAmount used for the exchange is inflated. sourceAmount is the amount of the synth you are giving up. "Inflated" means it is larger than it should be, because it still includes value that is effectively owed back to the protocol.

When an exchange happens, the amount you will receive, called amountReceived, is calculated inside an internal function called _exchange. Inside _exchange, amountReceived is set on line 486:

Text
amountReceived = _deductFeesFromAmount(entry.destinationAmount, entry.exchangeFeeRate);

Line by line:

  • amountReceived is the variable that will hold how much of the destination synth you get.
  • _deductFeesFromAmount(...) is a helper function that takes an amount and subtracts the fee from it.
  • entry.destinationAmount is the gross amount of the destination synth, before the fee.
  • entry.exchangeFeeRate is the fee rate for this exchange.
  • So the line says: take the gross destination amount, subtract the fee, and store the result as what you receive.

Now, how is entry.destinationAmount itself worked out? On line 442, a function called effectiveValueAndRatesAtRound is called with these parameters (inputs): sourceCurrencyKey, sourceAmount, destinationCurrencyKey, entry.roundIdForSrc and entry.roundIdForDest.

  • sourceCurrencyKey identifies which synth you are giving up.
  • sourceAmount is how much of it you are giving up.
  • destinationCurrencyKey identifies which synth you are getting.
  • entry.roundIdForSrc and entry.roundIdForDest are identifiers for the price round (a numbered batch of price updates) used for each side.

The problem is that sourceAmount is passed here instead of a different variable called sourceAmountAfterSettlement. sourceAmountAfterSettlement is the amount that remains after the pending debts (such as a rebate you owe back) have been applied. So the full source amount, before those debts are applied, is used to calculate the destination amount.

Then, when the _convert function runs (the part that actually moves the tokens), only sourceAmountAfterSettlement is burned. "Burning" means destroying tokens so they can no longer be used. But the amountReceived, which was calculated from the full sourceAmount, is issued to you. In other words, the two sides of the trade are measured with different rulers: you are charged the smaller, settled amount, but paid out as if you had given the larger, unsettled amount.

To summarise the report's own description: the _exchange function used the sourceAmount variable to calculate the amount that has to be rebated to the destination, but did not do a post-reclaim check on it to see whether the amount is accurate relative to the destination token's value. A post-reclaim check would be a validation step after the reclaim is applied, confirming the numbers still make sense.

An example of the bug in action

The report gives a worked example. Suppose you are exchanging sUSD for sBTC, and the price of sBTC drops during the cooldown period. The synth would rebate you the difference in sBTC. The issue is that the difference is rebated in the destination token, and the synth failed to calculate the rebate's value of sBTC in sUSD. In everyday terms: you are handed back change in a foreign currency, but nobody checks what that change is worth in your own currency before using it in the next calculation.

How someone could exploit it

The report's example scenario can be described in three steps.

Step 1: Create a pending rebate

You exchange one synth for another, for example sUSD to sBTC, and the price of the destination token (sBTC) drops during the cooldown period. Under the SIP 236 rules, the synth now owes you a rebate for the difference. At this point you have a pending rebate: money the protocol owes you, not yet collected.

Step 2: Exchange again before reclaiming

Instead of collecting the rebate first, you call exchange again. Because the pending debt has not been settled, the sourceAmount used in this second exchange is inflated relative to what will actually be burned. Think of it as a shop that lets you spend a credit note twice: once as credit, and once as if it were cash in your hand.

Step 3: Receive an inflated destination amount

Inside _exchange, entry.destinationAmount is derived from the full sourceAmount via effectiveValueAndRatesAtRound, and amountReceived is then computed from it on line 486. The later _convert call burns only sourceAmountAfterSettlement, but the larger amountReceived is issued to you. You therefore receive more of the destination token than the settled source amount entitles you to.

Root cause

The mistake is a variable selection error in the exchange path. The destination amount is computed from sourceAmount, the pre-settlement figure, rather than from sourceAmountAfterSettlement. The burn side of the operation correctly uses the settled amount, so the two sides of the trade are sized from different quantities. The report says _exchange did not do a post-reclaim check on the amount to see whether it was accurate relative to the destination token's value.

Impact

The report describes the bug as critical. A user with a pending rebate could call exchange before reclaiming it and receive an amount of the destination token calculated from the full, unsettled source amount, while only the settled amount is burned. The user receives more of the destination token than the settled source amount entitles them to. The condition required is a pending rebate, which arises when the destination token price falls during the cooldown period. The report does not quantify the maximum loss or state how many times the pattern could be repeated.

The fix

Synthetix patched the vulnerability at commit [redacted] in the Synthetix repository. A commit is a saved snapshot of a code change, with a unique identifier, so anyone can look up exactly what was altered. The report does not describe the contents of the patch beyond linking to that commit.

Variants to test

Each is written as what to look for and what counts as a positive result.

  1. Look for other paths where a pre-settlement amount is used to size an output while a post-settlement amount is used to debit the user. A positive result is any function where the input to a rate or value calculation differs from the quantity actually burned or transferred.
  2. Check rebate and fee-reclamation flows in other protocols for missing post-settlement validation. A positive result is a rebate or credit that is denominated in one asset but never converted back to the accounting asset before being reused.
  3. Test sequences where a user acts again before a pending settlement completes. A positive result is a second operation whose size depends on unsettled balances, producing an output larger than the settled input supports.
  4. Review internal conversion helpers for parameter mismatches, such as passing a gross amount where a net amount is expected. A positive result is a call site where the variable name and the surrounding settlement logic disagree.

Glossary

  • Blockchain: a shared, public record of transactions kept by many computers, which smart contracts run on.
  • Bug bounty: a programme that pays people for finding and reporting security bugs responsibly.
  • Burn: to destroy tokens so they can no longer be used.
  • Cooldown: a short waiting period after a trade before the final amount is settled.
  • Commit: a saved snapshot of a code change, identified by a unique string.
  • Critical: the most severe bug rating, meaning the bug could cause major loss or damage.
  • Destination token: the token you are buying in a swap.
  • Exchanger: the Synthetix smart contract that handles swaps between synths.
  • Fee rate: the percentage fee charged on a trade.
  • Function: a named block of code in a program that performs a task and can be called by other code.
  • Immunefi: a platform for reporting and rewarding blockchain security bugs.
  • Nexus Mutual matching programme: a scheme that tops up bug bounty payouts for eligible projects.
  • Parameter: an input value passed to a function.
  • Pending rebate: a rebate the protocol owes you but which you have not yet collected.
  • Post-reclaim check: a validation step after a reclaim is applied, confirming the numbers are still correct.
  • Rebate: money paid back to you because a price moved in your favour after a trade.
  • Reclaim: money taken back from you because a price moved against you after a trade.
  • SIP: Synthetix Improvement Proposal, a public design document describing how a feature should work.
  • Smart contract: a program stored on a blockchain that runs exactly as written and can be called by anyone.
  • Source amount: the amount of the token you are giving up in a swap.
  • Source amount after settlement: the amount left after pending debts are applied.
  • sBTC: a synth designed to track the price of Bitcoin.
  • sUSD: a synth designed to track the price of the US dollar.
  • Synth: a synthetic asset token issued by Synthetix that tracks the price of something else.
  • Token: a digital unit of value recorded on a blockchain.
  • Variable: a named box in a program that holds a value.
  • Whitehat: a security researcher who finds bugs and reports them responsibly rather than abusing them.

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.