Skip to content
Smart contracts Audit finding Intermediate

[PNM-004] Calculation of _secondaryReserveRatio can be overflowed

A calculation of the secondary reserve ratio could overflow due to a cast to uint32, allowing an attacker to bypass checks in the initialize function. This could result in an incorrect configuration and impact subsequent functionality.

PwnedNoMore Follow

Originally disclosed on Code4rena

Submitted by PwnedNoMore, also found by ych18

uint32 _secondaryReserveRatio = uint32((msg.value * SCALE * 1e18) / (_initialTokenSupply * _initialTokenPrice));

_secondaryReserveRatio can be overflowed by setting a relatively small _initialTokenSupply and _initialTokenPrice. The result will be truncated by uint32, causing an overflow.

This overflow can bypass all the checks in function initialize. Any following functionality will be impacted since the _secondaryReserveRatio is incorrect.

Proof of Concept / Attack Scenario

  • The user provide _initialTokenSupply and _initialTokenPrice, which meets SCALE * 1e18 == _initialTokenSupply * _initialTokenPrice
  • The msg.value is set as 2 ** 32 + X, where MIN_SECONDARY_RESERVE_RATIO <= X <= primaryReserveRatio. Note that msg.value is in Wei, so the deposited fund is not huge.

Suggested Fix

Add overflow checks.

mundhrakeshav (Nibbl) acknowledged

HardlyDifficult (judge) commented:

OpenZeppelin has safe cast helpers that could be leveraged here.

It is concerning that due to the truncation here, the configuration would not work how the user expects given the input parameters. And at this point the NFT has been escrowed into the vault. Because of this it seems Medium risk is a fair assessment.


The original report as published on Code4rena, by PwnedNoMore. 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 result of a multiplication and division is cast to uint32 without checking for overflow, so a large value is truncated.

Impact

This overflow could bypass all checks in the initialize function, leading to an incorrect secondary reserve ratio and impacting subsequent functionality; the judge noted it is concerning and assessed it as Medium risk.

What the attacker needed

Ability to call initialize with chosen parameters

The fix

Add overflow checks, for example by using OpenZeppelin's safe cast helpers.

How it was found

  1. The user provides `_initialTokenSupply` and `_initialTokenPrice` such that their product equals `SCALE * 1e18`.
  2. The user sets `msg.value` to `2 ** 32 + X`, where X is between the minimum secondary reserve ratio and the primary reserve ratio.

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.

When a number is too big for the data type it is stored in, it can wrap around to a small value, like a car odometer rolling over. Here, a calculation result was stored in a 32-bit integer, so a large value became small, letting the attacker bypass checks that expect a certain range. This could make the system behave unexpectedly.

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 PwnedNoMore

View all

8 more disclosed reports, best first.

More from Nibbl contest

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.