Skip to content
Smart contracts Audit finding Intermediate

Rounding error in buyQuote might result in free tokens

The buyQuote function in a Solidity contract rounded the required base-token payment down instead of up, so a buyer could sometimes pay zero base tokens for a small amount of fractional tokens. An attacker could repeatedly buy tiny amounts of fractional tokens for free, reducing the fractional token reserve and short-changing liquidity providers on fees.

Zarf Follow with minhtrng, Franfran and 16 others

Originally disclosed on Code4rena

Submitted by Zarf, also found by minhtrng, Franfran, Apocalypto, adriro, 0xDave, wait, unforgiven, Jeiwan, hansfriese, bytehat, UNCHAIN, rajatbeladiya, CRYP70, hihen, koxuan, kiki_dev, yixxas, and chaduke

In order to guarantee the contract does not become insolvent, incoming assets should be rounded up, while outgoing assets should be rounded down.

The function buyQuote() calculates the amount of base tokens required to buy a given amount of fractional tokens. However, this function rounds down the required amount, which is in favor of the buyer (i.e. he/she has to provide less base tokens for the amount of receiving fractional tokens.

Depending on the amount of current token reserves and the amount of fractional tokens the user wishes to buy, it might be possible to receive free fractional tokens.

Assume the following reserve state:

  • base token reserve: 0,1 WBTC (=1e7)
  • fractional token reserve: 10.000.000 (=1e25)

The user wishes to buy 0,9 fractional tokens (=9e17). Then, the function buyQuote() will calculate the amount of base tokens as follows:

(9e17 * 1000 * 1e7) / ((1e25 - 9e17) * 997) = 0,903

As division in Solidity will round down, the amount results in 0 amount of base tokens required (WBTC) to buy 0,9 fractional tokens.

Impact

Using the example above, 0,9 fractional tokens is a really small amount (0,1 BTC / 1e7 = +- $0,00017). Moreover, if the user keeps repeating this attack, the fractional token reserve becomes smaller, which will result in a buyQuote amount of >1, after which the tokens will not be free anymore.

Additionally, as the contract incorporates a fee of 30bps, it will likely not be insolvent. The downside would be the LP holder, which will receive a fee of less than 30bps. Hence, the impact is rated as medium.

Recommended Mitigation Steps

For incoming assets, it’s recommended to round up the required amount. We could use solmate’s FixedPointMathLib library to calculate the quote and round up. This way the required amount will always at least be 1 wei:

function buyQuote(uint256 outputAmount) public view returns (uint256) {
  return mulDivUp(outputAmount * 1000, baseTokenReserves(), (fractionalTokenReserves() - outputAmount) * 997);
}

outdoteth (Caviar) confirmed and commented:

Fixed in: https://github.com/outdoteth/caviar/pull/4

Uses muldivup from solmate to round up the calculation in buyQuote.


The original report as published on Code4rena, by Zarf. 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

Likely the quote calculation used plain division, which rounds down, for an amount the user must pay in, when incoming amounts should be rounded up.

Impact

The gain per purchase is tiny and repeating it shrinks the fractional reserve until the quote exceeds one, and the 30bps fee likely keeps the contract solvent, so the main loss falls on liquidity providers receiving less than the full fee; rated medium.

What the attacker needed

Ability to call buyQuote and buy; small fractional token reserve relative to base

The fix

The quote calculation was fixed to round up using solmate's mulDivUp so the required amount is always at least one wei.

How it was found

  1. Note the current base token and fractional token reserves.
  2. Call buyQuote for a small amount of fractional tokens.
  3. Observe the calculated base token amount rounds down to zero.
  4. Buy the fractional tokens while paying no base tokens.

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 holds two kinds of tokens and lets people swap between them. When it works out how much you must pay, it does a division, and division in Solidity throws away any remainder, so the price can come out as zero. That means someone could take a small amount of the other token without paying anything.

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 Zarf

View all

2 more disclosed reports, best first.

More from Caviar Private Pools

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.