Uranium Finance: $50M Bug in Forked Uniswap Code

How a missing zero in the k invariant check allowed an attacker to drain over $50 million.

Backstory

Uranium Finance (launched April 2021) was a Uniswap V2 fork with added functionality to facilitate yield-farming on Binance Smart Chain (now called BNB Chain), which worked as follows:

  1. Deposit liquidity into a pool containing the platform’s trading token, U92 (92 is uranium’s atomic number), receiving LP tokens representing your share.
  2. Stake the LP tokens into a “farm”, which pays you the platform’s dividend token U235 (uranium-235 is an isotope used as nuclear fuel).
  3. Hold U235 in your wallet to accrue BNB/BUSD rewards, which were collected from swap fees.

The key modification they had made was to Uniswap V2’s swap function. They lowered the swap fee from 0.3% to 0.16%, with the stated purpose of attracting more trading volume.

The bug

To understand the exploit, let’s first walk through the relevant snippets of Uniswap V2’s swap function. Recall that Uniswap V2 is built on the constant product formula, which I explain in further detail in this blog post.

During swaps, the key is for k to remain constant in the formula x * y = k, where x and y represent the total reserves of each token respectively. A 0.3% fee is given to LPs for each swap, calculated by using only 99.7% of the user’s input to calculate the output:

uint balance0Adjusted = balance0 * 1000 - amount0In * 3;
uint balance1Adjusted = balance1 * 1000 - amount1In * 3;

require(
    balance0Adjusted * balance1Adjusted
        >= uint(_reserve0) * _reserve1 * 1000**2,
    "UniswapV2: K"
);

The idea here is that since the Uniswap V2 swap function allows for flash swaps, where users can use the swap function to borrow either or both tokens of the pool, the swap fee can then be applied to up to both tokens. That is why we don’t see the fee applied to only the input token like it was in v1.

In other words, we can have any of the following 3 cases:

  1. Swap token0 for token1, where the fee is applied to only the token0 input.
  2. Swap token1 for token0, where the fee is applied to only the token1 input.
  3. Borrow both token0 and token1, subsequently returning them, where the fee is applied to both input amounts.

Rather than checking which case we are dealing with before applying the fee, we can write code that can handle any of the above cases automatically.

Recall k = x * y, where k must remain constant and x and y represent token0 and token1 reserves respectively. So we have k = reserve0 * reserve1. Since k must remain constant, the swap must satisfy balance0 * balance1 >= (k = reserve0 * reserve1), where balance0 and balance1 are the pool’s token balances after the swap.

Info

We check >= instead of only =, because k increasing is not an issue, and we don’t want to reject those swaps. The reason why k increasing is not an issue is because it means the product of the reserves has increased, which is beneficial to LPs because they own shares of the additional reserves of the pool.

However, we must adjust the above check to handle the 0.3% amount of fees that go to LPs. We deduct the fee from the reserve balance used in the k invariant check, so it doesn’t count towards paying for the output token. In other words, deducting the fee will result in a lower balance0 * balance1, which means less output token must be given to the user to satisfy >= (k = reserve0 * reserve1) (since the output reserve will remain greater if less is given). That gives us the following:

balance0Adjusted = balance0 − amount0In × 0.003
= balance0 − amount0In × (3/1000)
= (balance0 × 1000 − amount0In × 3) / 1000

In case there is also a fee collected for token1, then we apply the same adjustment to balance1:

balance1Adjusted = (balance1 × 1000 − amount1In × 3) / 1000

Therefore, our updated check is:

balance0Adjusted × balance1Adjusted ≥ reserve0 × reserve1

Notice how this formula nicely satisfies each of the three cases we described above.

  1. User swaps token0 for token1. The fee is only applied to balance0, since balance1Adjusted = (balance1 * 1000 - (amount1In = 0) * 3) / 1000 = (balance1 * 1000) / 1000 = balance1. There is no adjustment to balance1, because no fee is applied.
  2. User swaps token1 for token0. Our formulas result in balance0Adjusted = balance0 and balance1Adjusted < balance1, since the fee is applied to token1 only.
  3. User borrows both token0 and token1. The result is both balance0Adjusted < balance0 and balance1Adjusted < balance1, since the 0.3% fee is collected from both tokens.

Still, there is an issue with this formula. We currently have the following:

balance0Adjusted = (balance0 * 1000 - amount0In * 3) / 1000;
balance1Adjusted = (balance1 * 1000 - amount1In * 3) / 1000;
require(balance0Adjusted * balance1Adjusted >= reserve0 * reserve1);

Can you spot what’s wrong? Before I explain, take a moment to think about it, then select the best answer:

Try it

The problem is that since Solidity rounds the adjusted balances down, the adjusted balances may be lower than expected, thus rejecting valid swaps due to the >= check in the require statement.

To resolve this, we can adjust our formulas to avoid the division by multiplying both sides of the inequality by 1000 ** 2, allowing us to remove the division by 1000 for both balance adjustments. We now have our final and correct formula:

balance0Adjusted = balance0 * 1000 - amount0In * 3;
balance1Adjusted = balance1 * 1000 - amount1In * 3;
require(balance0Adjusted * balance1Adjusted >= reserve0 * reserve1 * 1000 ** 2);

Continuing with Uranium Finance’s implementation, recall that they modified the above code so that their swap function only charges a fee of 0.16%, rather than Uniswap V2’s 0.3%. Their implementation was as follows:

balance0Adjusted = balance0 * 10000 - amount0In * 16;
balance1Adjusted = balance1 * 10000 - amount1In * 16;

require(
    balance0Adjusted * balance1Adjusted
        >= reserve0 * reserve1 * 1000 ** 2
);

Can you spot the bug? Before I continue with my explanation, give it your best shot:

Try it

Using 1000 instead of 10000 would create a fee of 1.6% instead of 0.16%, so 10000 is indeed the correct number to use for the adjusted balances. But the problem is that they did not update the right-hand side of the inequality with 10000, and instead continued to use 1000. This makes the required product 100 times lower than it should be, allowing users to receive more tokenOut than they should for a given amount of tokenIn, since the new balances balance0Adjusted * balance1Adjusted will easily be greater than reserve0 * reserve1 * 1000 ** 2.

The exploit

The hacker, who was later caught, executed the following steps to drain 26 pools, as described in a U.S. indictment:

  1. Select a pool and supply tiny inputs: approximately one smallest unit of each token. This satisfied a separate require check, require(amount0In > 0 || amount1In > 0);, within the swap function.
  2. Request enormous outputs (the swap function allows you to specify the amount of output tokens to receive, as it relies on the k invariant check to verify the amounts transferred in and out of the contract) and the broken k invariant check we identified still accepts the swap despite minimal input tokens. The attacker then receives both outputs.

In total, the following tokens were drained:

TokenAmount drainedReported USD value
WBNB34,000$18,000,000
BUSD17,900,000$17,900,000
USDT5,700,000$5,700,000
ETH1,800$4,700,000
BTC80$4,300,000
DOT26,500$800,000
ADA638,000$800,000
U92112,000Not provided
Total of priced assets—$52,200,000

Conclusion

When using or modifying the code from another protocol, it is extremely important that you as a developer truly understand the implementation of the original code. In this case, there was a clear lack of understanding as to why the Uniswap V2 k invariant check was being multiplied by 1000 ** 2, leading to a critical bug that allowed a lone hacker to steal over $50M of user funds.

Test your understanding

As a researcher securing Uranium Finance in 2021, could you have identified this critical vulnerability? Test your understanding with the following questions:

A developer changes the fee representation from 3/1000 to 30/10000, keeping the fee at 0.3%. Which implementation preserves the intended check?

A Uranium pool holds 100 units of each token. An attacker deposits 1 unit of each, then requests 90 units of each. Using Uranium’s full formula, does the swap pass?

balance0Adjusted = balance0 * 10000 - amount0In * 16;
balance1Adjusted = balance1 * 10000 - amount1In * 16;

require(
    balance0Adjusted * balance1Adjusted
        >= reserve0 * reserve1 * 1000 ** 2
);

Tests show that ordinary swaps requesting correctly quoted outputs all succeed. Does that establish that the invariant check is secure?