Skip to content
Smart contracts Audit finding Intermediate

Unsafe cast in IndexPool mint leads to attack

The IndexPool.mint function casts a user-controlled ratio to uint120 without checking for overflow, so an attacker can choose a ratio that truncates to zero and mint LP tokens for free. The attacker can then burn these inflated LP tokens to withdraw the entire pool reserves.

cmichel Follow

Originally disclosed on Code4rena

Submitted by cmichel, also found by cmichel and pauliax

The IndexPool.mint function performs an unsafe cast of ratio to the uint120 type:

uint120 ratio = uint120(_div(toMint, totalSupply));

Note that toMint is chosen by the caller and when choosing toMint = 2**120 * totalSupply / BASE, the ratio variable will be 2**120 and then truncated to 0 due to the cast.

This allows an attacker to mint LP tokens for free. They just need to choose the ratio such that the amountIn = ratio * reserve / BASE variable passes the require(amountIn >= MIN_BALANCE, "MIN_BALANCE"); check. For example, when choosing ratio = 2**120 * totalSupply / BASE + 1e16, an attacker has to pay 1/100th of the current reserves but heavily inflates the LP token supply.

They can then use the inflated LP tokens they received in burn to withdraw the entire pool reserves.

POC

I created this POC that implements a hardhat test and shows how to steal the pool tokens:

Impact

An attacker can inflate the LP token pool supply and mint themselves a lot of LP tokens by providing almost no tokens themselves. The entire pool tokens can be stolen.

Recommended Mitigation Steps

Even though Solidity 0.8.x is used, type casts do not throw an error. A SafeCast library must be used everywhere a typecast is done.

maxsam4 (Sushi) confirmed

The original report as published on Code4rena, by cmichel. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

The code casts a user-controlled value to a smaller integer type without checking for overflow, so a large value wraps to zero.

Impact

An attacker can inflate the LP token supply and mint themselves many LP tokens for almost no cost, potentially stealing the entire pool.

What the attacker needed

Access to the mint function; ability to call burn

The fix

Use a SafeCast library for all type casts, even with Solidity 0.8.x, to prevent silent truncation.

How it was found

  1. Choose a toMint value such that ratio equals 2**120 * totalSupply / BASE, which truncates to 0.
  2. Call mint with that toMint, paying only a small amount that still passes the MIN_BALANCE check.
  3. Receive inflated LP tokens.
  4. Call burn with the inflated LP tokens to withdraw the entire pool reserves.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

A smart contract lets people add funds and get pool tokens in return. The code that calculates how many tokens to give has a bug: if you pick a very large number, it wraps around to zero, so you get tokens without paying. You can then cash out those tokens to steal everyone else's money.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by cmichel

View all

196 more disclosed reports, best first.

More from Sushi Trident contest phase 1

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.