Skip to content
Business logic Write-up Advanced

[web3] Aurora Infinite Spend Bugfix Review ($6m Payout). This is a postmortem of a vulnerability… | by Immunefi | Apr, 2022 | Medium

A flaw in Aurora's Rainbow Bridge allowed an attacker to use delegatecall to trigger the ExitToNear contract without actually sending ETH, causing the bridge to release nETH on NEAR. This could have led to a direct loss of 70k ETH and $200m in other assets.

medium.com

Published on medium.com

Aurora

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

Aurora is an implementation of the EVM built on the NEAR network that supports the tools available in the Ethereum ecosystem. Besides the EVM, Aurora developed the Rainbow Bridge, which lets users transfer assets between Ethereum, NEAR and Aurora. In practice it allows users to deposit ETH and ERC20 tokens from Ethereum mainnet into the nested layer of NEAR that is Aurora.

Bridges generally follow an IOU model: funds are locked by the bridge contract on the source chain and an equivalent wrapped asset is issued on the destination chain. For every dollar moved across, the bridge has to hold that dollar on its native chain in case the user wants to move it back, so bridges accumulate a lot of funds. They also carry more attack surface than a typical DeFi project: on top of the contracts and the dapp, a bridge usually needs monitoring nodes, validator keys and secure communication channels.

The feature at the centre of this bug is withdrawal from Aurora. Two contracts in the Aurora Engine handle it: ExitToNear and ExitToEthereum. They are special, built-in (precompiled) contracts that handle withdraw requests from the Aurora EVM.

For mapped ERC20 tokens, the template contract on Aurora triggers these two contracts. In both cases the logic looks fine for ERC20 tokens, because calling these functions first burns the ERC20 tokens before making a call to the special built-in contracts. That is how ERC20 tokens are withdrawn from Aurora to NEAR or to Ethereum.

ETH is handled slightly differently. The special contract is still called, but the user simply sends ETH to it, and the contract generates an ExitToNear event that records the sender, destination and amount of the exit. At the end of the ExitToNear contract execution, exit_event_log containing the event info is returned. When the main execution is done, these logs, along with all the other logs produced during execution, are checked by filter_promises_from_logs in aurora-engine/engine/src/engine.rs. As long as the log is generated with the hardcoded address ExitTo(Near|Ethereum)::ADDRESS, the log.data is processed as new promises to be scheduled. Those transfer promises can therefore be generated as long as the code of the native contracts is invoked.

Setup

The relevant preconditions are structural:

The native contract used in the exploit is ExitToNear at [redacted].

Finding it

The starting point is the difference between the three major types of contract call in Ethereum: regular CALL, STATICCALL and DELEGATECALL. STATICCALL is explicitly prohibited in the Aurora Engine, so the interesting pair is CALL and DELEGATECALL.

When contract A makes a CALL to contract B by calling foo(), the function execution relies on contract B's storage, and msg.sender is set to contract A. Contract A called foo(), so msg.sender is contract A's address and msg.value is the ETH sent along with that function call. Changes made to state during that call can only affect contract B.

From the original on medium.com

When the same call is made using DELEGATECALL, foo() is called on contract B but in the context of contract A. The logic of contract B is used, but any state changes made by foo() affect the storage of contract A, and msg.sender points to the EOA who made the call in the first place. Importantly for this bug, msg.value points to the first call context, not the second: Ether is not sent along with delegatecall.

From the original on medium.com

Since transfer promises from Aurora to NEAR can be generated by calling the ExitToNear built-in contract directly, the question becomes whether DELEGATECALL can be used to trick the Aurora Engine into thinking the Ether was actually sent. It can, because the code only checks that the LOG was generated by the built-in address, and it assumes that msg.value > 0 actually means ETH was sent to that address. With delegatecall() the msg.value is inherited from the original calling context, while the ETH is no longer passed to the native contract.

Exploitation

The malicious contract takes the ETH value from the caller, triggers the exit event in the ExitToNear contract, then sends the nETH back to the caller. The nETH can be deposited into the Aurora EVM again through the Aurora Bridge, effectively doubling the attacker's original balance.

From the original on medium.com

Step 1: Bridge Ether from Ethereum to Aurora

The attacker bridges Ether from Ethereum to Aurora using the Rainbow Bridge (Aurora Bridge).

Step 2: Deploy the malicious contract

The attacker deploys, on Aurora, a contract that makes the delegatecall to the native contract ExitToNear, that is, to [redacted].

Step 3: Call the exploit function

The attacker calls the exploit function of the malicious contract. Aurora is tricked at this point into sending nETH to the caller on NEAR from the Aurora bridge contract. The balance of the attacker does not change on Aurora.

Step 4: Deposit the nETH back

The attacker deposits the nETH back into Aurora, doubling the attacker's balance.

Step 5: Repeat

The attacker repeats from step 3.

The report notes that the bug incurs the complex integration logic of the NEAR and Aurora runtimes, and that such a discovery was impossible without deep analysis from pwning.eth.

Root cause

The engine decides whether to schedule a transfer promise based on two things: the log was emitted by the hardcoded ExitTo(Near|Ethereum)::ADDRESS, and msg.value > 0 is taken to mean that ETH was actually sent to that address. Under DELEGATECALL the second assumption is false: msg.value is inherited from the original calling context while no ETH moves to the native contract. The log therefore looks exactly like a genuine exit, and filter_promises_from_logs processes it as a real withdrawal.

For ERC20 tokens the burn happens before the call to the built-in contract, so the same trick does not produce free value there.

Impact

If exploited by a malicious user, the vulnerability could have led to a direct loss of 70k ETH and $200m in other assets. It was responsibly disclosed rather than exploited, no user funds were lost, and Aurora quickly patched the bug. The bug was assessed at a severity rating of critical, and the whitehat received a $6 million payout from Aurora, the second largest bounty payout in history.

Fix

An exit error is now returned if the address given does not match the inputs' address. This disables the ability to call the contract with DELEGATECALL, in a similar way to how Aurora disabled the STATICCALL. Aurora also developed a test to ensure the vulnerability is tracked, in case a logic change might cause it to appear again.

  1. Other precompiled or native contracts that emit logs consumed by off-chain or cross-runtime logic. Try reaching them with DELEGATECALL from a contract you control and check whether the resulting log is treated as a genuine action. A positive result is a log accepted by the consuming code while no value or state change occurred in the calling context.
  2. Value-bearing entry points that trust msg.value without checking the caller's balance movement. Call them through DELEGATECALL with a non-zero msg.value inherited from the outer call. A positive result is the contract acting on the value as if it had been received.
  3. Bridges whose destination-side release is driven purely by an event. Look for paths where the event is the only evidence of a deposit. A positive result is a release on the destination chain that is not matched by a lock or burn on the source chain.
  4. Token templates where the burn and the cross-chain call are not atomic in the same order as the ERC20 template. Check whether any path reaches the built-in contract before the burn. A positive result is a withdrawal promise generated without the corresponding balance reduction.
  5. STATICCALL-style restrictions applied to one call type but not its siblings. Where a runtime bans one opcode or call type, test the neighbouring ones for the same class of context confusion. A positive result is a call type that reaches the same sensitive code with attacker-controlled context.

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

Aurora is a system that lets people use Ethereum-style programs (called smart contracts) on top of another network called NEAR. It also runs a bridge called the Rainbow Bridge, which moves money and tokens between Ethereum, NEAR and Aurora. A security researcher who goes by the name pwning.eth found a serious flaw in how Aurora handled withdrawals. If someone had abused it, they could have drained a huge amount of value: the report puts the possible direct loss at 70k ETH plus $200m in other assets. Instead of being exploited, the bug was reported privately through the Immunefi bug bounty platform on April 26. Aurora fixed it quickly, no user funds were lost, and the researcher was paid $6 million, which the report describes as the second largest bounty payout in history.

This article walks through that bug from the beginning, assuming you have never studied security. We will explain what a blockchain, a bridge, a smart contract and a contract call are as we go, and then follow the researcher's reasoning step by step.

From the original on medium.com

Background: what a bridge is, and why it matters

There are many blockchains in use, such as Ethereum, NEAR, Polygon, Fantom, Avalanche, BSC and Solana. Each one is a separate record of who owns what, and they do not normally talk to each other. If you own a token on Ethereum, another chain has no idea you own it.

A bridge is software that connects two chains so you can move assets between them. Most bridges use what the report calls the IOU ("I owe you") model. You send your funds to the bridge on the chain you are leaving, and the bridge locks them in a contract there. On the destination chain, the bridge hands you an equivalent token, often called a wrapped token or IOU token. If you later want your original funds back, you return the wrapped token and the bridge releases the locked original. For every dollar moved across, the bridge must hold a dollar on the native chain, so bridges end up holding enormous amounts of money. That makes them attractive targets.

Bridges also have more ways in for an attacker than a normal DeFi project. A yield farm or exchange might be a set of contracts plus a website. A bridge usually also needs monitoring nodes, validator keys and secure communication channels. More parts means more places for a bug to hide.

What Aurora is

Aurora is an implementation of the EVM built on NEAR. The EVM, the Ethereum Virtual Machine, is the standard engine that runs Ethereum smart contracts; building an EVM-compatible layer means existing Ethereum tools and contracts can be used there. Aurora also built the Rainbow Bridge, which lets users move assets between Ethereum, NEAR and Aurora. In practice you can deposit ETH and ERC20 tokens (tokens built to the ERC20 standard on Ethereum) from Ethereum mainnet into Aurora, which is a nested layer of NEAR.

The part of Aurora this bug lives in is withdrawal: moving value out of Aurora, either back to NEAR or back to Ethereum. Two contracts in the Aurora Engine handle this: ExitToNear and ExitToEthereum. They are special, built-in contracts, also called precompiled contracts, meaning they are part of the engine itself rather than ordinary contracts someone deployed.

For ERC20 tokens, the template contract that represents mapped tokens on Aurora triggers these two contracts. The logic there looks fine: the function first burns the ERC20 tokens (destroys them so they can no longer be used) and only then calls the special built-in contract. Burning first is what keeps the books balanced.

ETH is handled a little differently. The special contract is still called, but the user simply sends ETH to it, and the contract generates an ExitToNear event recording the sender, the destination and the amount. An event (or log) is a note a contract writes into the transaction record; other software can read it later. At the end of the ExitToNear execution, a log called exit_event_log containing the event information is returned. When the main execution finishes, these logs, along with every other log produced during execution, are checked by a function called filter_promises_from_logs in aurora-engine/engine/src/engine.rs. As long as a log was generated with the hardcoded address ExitTo(Near|Ethereum)::ADDRESS, its log.data is processed as a new promise to be scheduled. A promise here is an action the system commits to carry out later, in this case releasing funds on the other side. So transfer promises can be generated whenever the code of the native contracts is invoked.

From the original on medium.com

The building blocks: three kinds of contract call

To understand the bug you need to know how one smart contract calls another. In Ethereum there are three major types: regular CALL, STATICCALL and DELEGATECALL. STATICCALL is explicitly prohibited in the Aurora Engine, so the interesting pair is CALL and DELEGATECALL.

When contract A makes a CALL to contract B by calling a function foo(), the function runs using contract B's storage, and msg.sender (the recorded caller) is set to contract A. Because contract A made the call, msg.sender is A's address and msg.value (the amount of ETH sent with the call) is the ETH that travelled with it. Any changes to state during that call can only affect contract B. A simple comparison: you walk into a shop and pay at its counter. The shop's till changes, not yours.

From the original on medium.com

With DELEGATECALL, foo() is still the code of contract B, but it runs in the context of contract A. The logic is B's, yet any state changes made by foo() affect the storage of contract A, and msg.sender points to the original person (the EOA, externally owned account, meaning a normal wallet rather than a contract) who started the whole thing. Crucially for this bug, msg.value points to the first call context, not the second: Ether is not actually sent along with a delegatecall. The comparison: you borrow a shop's cash register software and run it on your own counter. The software is theirs, but the receipts land in your till, and no money moved to the shop.

From the original on medium.com

Finding the flaw

The researcher knew that transfer promises from Aurora to NEAR can be generated by calling the ExitToNear built-in contract directly. The question was whether DELEGATECALL could trick the Aurora Engine into thinking Ether had really been sent. It could, because the engine only checks two things: that the log came from the built-in address, and that msg.value > 0, which it takes to mean ETH was actually sent to that address. With delegatecall() the msg.value is inherited from the original calling context, while the ETH is not passed to the native contract at all. The log therefore looks exactly like a genuine exit.

Exploitation, step by step

The malicious contract takes the ETH value from the caller, triggers the exit event in the ExitToNear contract, and then sends the nETH (the NEAR-side representation of the deposited ETH) back to the caller. That nETH can be deposited into the Aurora EVM again through the Aurora Bridge, which effectively doubles the attacker's original balance.

From the original on medium.com
  1. Bridge Ether from Ethereum to Aurora. The attacker uses the Rainbow Bridge (Aurora Bridge) to move ETH from Ethereum to Aurora.
  2. Deploy the malicious contract. On Aurora, the attacker deploys a contract that makes the delegatecall to the native contract ExitToNear, at the address [redacted].
  3. Call the exploit function. The attacker calls the exploit function of that contract. Aurora is now tricked into sending nETH to the caller on NEAR from the Aurora bridge contract, while the attacker's balance on Aurora does not change.
  4. Deposit the nETH back. The attacker deposits the nETH back into Aurora, doubling the balance.
  5. Repeat. The attacker goes back to step 3.

The report notes that the bug involves the complex integration logic of the NEAR and Aurora runtimes, and that discovering it was impossible without deep analysis from pwning.eth.

Why it worked

The engine decides whether to schedule a transfer promise based on two things: the log was emitted by the hardcoded ExitTo(Near|Ethereum)::ADDRESS, and msg.value > 0 is taken to mean that ETH was actually sent to that address. Under DELEGATECALL the second assumption is false: msg.value is inherited from the original calling context while no ETH moves to the native contract. The log looks exactly like a genuine exit, and filter_promises_from_logs processes it as a real withdrawal.

For ERC20 tokens the burn happens before the call to the built-in contract, so the same trick does not produce free value there.

Impact

If exploited by a malicious user, the vulnerability could have led to a direct loss of 70k ETH and $200m in other assets. It was responsibly disclosed rather than exploited, no user funds were lost, and Aurora quickly patched the bug. The bug was assessed at a severity rating of critical, and the whitehat received a $6 million payout from Aurora, the second largest bounty payout in history.

The fix

An exit error is now returned if the address given does not match the inputs' address. This disables the ability to call the contract with DELEGATECALL, in a similar way to how Aurora disabled the STATICCALL. Aurora also developed a test to ensure the vulnerability is tracked, in case a logic change might cause it to appear again.

Variants worth testing

  1. Other precompiled or native contracts that emit logs consumed by off-chain or cross-runtime logic. Try reaching them with DELEGATECALL from a contract you control and check whether the resulting log is treated as a genuine action. A positive result is a log accepted by the consuming code while no value or state change occurred in the calling context.
  2. Value-bearing entry points that trust msg.value without checking the caller's balance movement. Call them through DELEGATECALL with a non-zero msg.value inherited from the outer call. A positive result is the contract acting on the value as if it had been received.
  3. Bridges whose destination-side release is driven purely by an event. Look for paths where the event is the only evidence of a deposit. A positive result is a release on the destination chain that is not matched by a lock or burn on the source chain.
  4. Token templates where the burn and the cross-chain call are not atomic in the same order as the ERC20 template. Check whether any path reaches the built-in contract before the burn. A positive result is a withdrawal promise generated without the corresponding balance reduction.
  5. STATICCALL-style restrictions applied to one call type but not its siblings. Where a runtime bans one opcode or call type, test the neighbouring ones for the same class of context confusion. A positive result is a call type that reaches the same sensitive code with attacker-controlled context.

Glossary

  • Blockchain: a shared, append-only record of transactions and balances, kept by many computers instead of one company.
  • Smart contract: a program that lives on a blockchain and runs automatically when someone calls it.
  • EVM (Ethereum Virtual Machine): the standard engine that runs Ethereum smart contracts; other chains can implement it so Ethereum tools work there.
  • ERC20: a common standard for tokens on Ethereum, which defines how they can be transferred and queried.
  • Bridge: software that connects two blockchains so assets can move from one to the other.
  • IOU model: a bridge design where funds are locked on the source chain and an equivalent wrapped token is issued on the destination chain.
  • Wrapped token / IOU token: a token on one chain that stands for an asset locked on another chain.
  • nETH: the representation of ETH used on the NEAR side in this setup.
  • Precompiled (native) contract: a contract built into the engine itself rather than deployed by a user.
  • Withdrawal: moving value out of a chain or layer, here from Aurora back to NEAR or Ethereum.
  • Burn: destroying tokens so they can no longer be used, usually to keep a bridge's books balanced.
  • Event / log: a note a contract writes into the transaction record, which other software can read later.
  • Promise: in this context, an action the system commits to carry out later, such as releasing funds on another chain.
  • CALL: a contract call where the called contract's own storage is used and the caller is recorded as msg.sender.
  • DELEGATECALL: a contract call that runs another contract's code in the caller's context, so state changes hit the caller's storage and msg.value is inherited from the outer call.
  • STATICCALL: a read-only contract call that cannot change state; it is prohibited in the Aurora Engine.
  • msg.sender: the address recorded as the caller of a function.
  • msg.value: the amount of ETH sent along with a call.
  • EOA (externally owned account): a normal wallet controlled by a person or key, as opposed to a contract.
  • Storage: the persistent data a contract keeps on the blockchain.
  • State change: any update to data stored on the blockchain, such as a balance going up or down.
  • Infinite spend: a bug where the same funds can be spent over and over because the balance is never reduced.
  • Attack surface: the total set of places where an attacker could try to get in.
  • Whitehat: a security researcher who finds bugs and reports them responsibly instead of exploiting them.
  • Bug bounty: a programme that pays researchers for reporting vulnerabilities.
  • Severity rating: a label such as critical that describes how serious a bug is.
  • Responsible disclosure: reporting a bug privately to the affected project so it can be fixed before details become public.
  • Runtime: the environment in which code executes on a chain or layer.
  • Atomic: happening as a single indivisible step, so either all of it happens or none of it does.

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.