Skip to content
Smart contracts Audit finding Intermediate

getPrice salesOption 2 can round down to the lower barrier, skipping the last time period

The getPrice function in salesOption 2 uses integer division that rounds down, causing the price to skip the final time period and drop directly to the end price. An attacker could exploit this to purchase NFTs at a lower price than intended during the skipped period.

0x3b Follow with dimulski, KupiaSec and 4 others

Originally disclosed on Code4rena

Submitted by 0x3b, also found by dimulski, KupiaSec, t0x1c, ZdravkoHr, degensec, and REKCAH

getPrice in salesOption 2 can round down to the lower barrier, effectively skipping the last time period. In simpler terms, if the price is scheduled to decrease over 4 hours, it will decrease for 2.999… hours and then jump to the price at the end of the 4th hour.

Proof of Concept

getPrice uses this else-if calculation to determine whether it should return the price as an equation or just the final price.

if (((collectionMintCost - collectionEndMintCost) / rate) > tDiff) {
    price = collectionPhases[_collectionId].collectionMintCost - (tDiff * collectionPhases[_collectionId].rate);
} else {
    price = collectionPhases[_collectionId].collectionEndMintCost;
}

The if statement’s method of division by rate is incorrect, as it results in rounding down and incorrect price movements. This can cause the price to jump over the last time period, effectively decreasing faster than intended.

Example:

Values | |

collectionMintCost | 0.49 ETH |

collectionEndMintCost | 0.1 ETH |

rate | 0.1 ETH (per hour) |

timePeriod | 1 hour - 3600 sec |

Hour 0 to 1 - 0.49 ETH. Hour 1 to 2 - 0.39 ETH - as it crosses the 1st hour, the price becomes 0.39 ETH. Hour 2 to 3 - 0.29 ETH - same here. It should go from 3 to 4 to 0.19 ETH, but that step is missed, and it goes directly to 0.1 ETH - the final price. Hour 3 to 4 - 0.10 ETH - the final price.

Math

The equation for calculating tDiff using the provided variables is:

Note: see original submission for math breakdown provided.

We want the crossover when hour 2 switches to 3, which is when (block.timestamp - collectionPhases[_collectionId].allowlistStartTime) = 3 h or 10800 sec.

Note: see original submission for math breakdown provided.

We plug in tDiff and the other variables into the if.

if (((collectionPhases[_collectionId].collectionMintCost - collectionPhases[_collectionId].collectionEndMintCost)
                / (collectionPhases[_collectionId].rate)) > tDiff) {

Note: see original submission for math breakdown provided.

We obtain 3 as the answer, which is due to rounding down, as we divide 0.39 by 0.1. Solidity only works with whole numbers, causing the if to fail as 3 > 3 is false. This leads to entering the else statement where the last price of 0.1 ETH is returned, effectively missing one step of the 4-hour decrease.

PoC

Gist here.

Add contracts/=smart-contracts/ to mappings and run it with forge test --match-test test_changeProof --lib-paths ../smart-contracts -vv.

Logs:

[PASS] test_changeProof() (gas: 745699)
Logs:
  4900000000000000000
  3900000000000000000
  2900000000000000000
  1000000000000000000

Recommended Mitigation Steps

A simple yet effective suggestion is to add = in the if.

-  if (...) > tDiff){
+  if (...) >= tDiff){

Assessed type

Error

a2rocket (NextGen) confirmed via duplicate issue #393

0xsomeone (judge) commented:

The Warden illustrates that a rounding error can result in a price that is lower than the actual one for a particular period.

The relevant rounding issue has been detected by the bot in L-17, however, it is designated as a low-risk finding and the attack path that the Warden illustrates is not immediately clear by consuming the bot report submission.

The C4 Supreme Court verdict did not finalize on issues based entirely on bot findings and as such, I will deem this exhibit a valid medium-severity submission given that:

  • It takes a low-risk bot report and illustrates that it should be treated as a medium-severity issue that is not immediately discernible by consuming the bot report alone.

  • The bot report’s mitigation would not lead to the loss of precision being resolved as it states that:

    …it’s recommended to require a minimum numerator amount to ensure that it is always greater than the denominator

The Warden’s submission was selected as the best due to their clear depiction of the issue using mathematical formulas, a provision of both textual and coded PoCs, and a simple remediation path.


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

Impact

The price can be lower than intended for a period, allowing buyers to purchase at a reduced price.

What the attacker needed

Collection uses salesOption 2; price decreases over time

The fix

The condition should use >= instead of > to include the boundary case.

How it was found

  1. Set up a collection with salesOption 2 and a decreasing price schedule.
  2. Call getPrice at a time when the price should be in the final period.
  3. Observe that the returned price is the final price instead of the expected intermediate price.

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.

In smart contracts, numbers are often whole numbers only, so division can lose precision. Here, a price calculation divides two amounts and rounds down, which makes the contract think a time period has ended early. As a result, the price drops to its final value sooner than it should, letting someone buy cheaper.

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 0x3b

View all

23 more disclosed reports, best first.

More from NextGen

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.