On a Linear or Exponential Descending Sale Model, a user that mints on the last block.timestamp mints at an unexpected price.
The price calculation for descending sales uses strict inequalities, so when a user mints exactly at the public end time, the function returns the initial mint cost instead of the final cost. This causes the user to pay an incorrect price, either too high or too low.
Fulum Follow with DeFiHackLabs, bird-flu and 17 others
Originally disclosed on Code4rena
Submitted by Fulum, also found by DeFiHackLabs, bird-flu, t0x1c, Krace, xAriextz, merlin, phoenixV110, 00xSEV, nuthan2x, zhaojie, xuwinnie, PENGUN, Jorgect, oakcobalt, VAD37, twcctop, Juntao, DanielArmstrong, and evmboi32
A user can mint a token at an incorrect price and lose funds if they mint on the last authorized block.timestamp during a Linear or Exponential Descending Sale Model.
Proof of Concept
On a Linear or Exponential Descending Sale Model, the admin sets the collectionMintCost and the collectionEndMintCost. In context of these sale models, the collectionMintCost is the price for minting a token at the beginning and the collectionEndMintCost the price at the end of the sale.
The minting methods use the function MinterContract::getPrice() to compute the correct price at the actual timing, check the function with the branch for a Linear or Exponential Descending Sale Model:
function getPrice(uint256 _collectionId) public view returns (uint256) {
uint tDiff;
//@audit If Periodic Sale
if (collectionPhases[_collectionId].salesOption == 3) {
...
} else if (collectionPhases[_collectionId].salesOption == 2 && block.timestamp > collectionPhases[_collectionId].allowlistStartTime && block.timestamp < collectionPhases[_collectionId].publicEndTime){
tDiff = (block.timestamp - collectionPhases[_collectionId].allowlistStartTime) / collectionPhases[_collectionId].timePeriod;
uint256 price;
uint256 decreaserate;
//@audit If Exponential Descending Sale
if (collectionPhases[_collectionId].rate == 0) {
price = collectionPhases[_collectionId].collectionMintCost / (tDiff + 1);
decreaserate = ((price - (collectionPhases[_collectionId].collectionMintCost / (tDiff + 2))) / collectionPhases[_collectionId].timePeriod) * ((block.timestamp - (tDiff * collectionPhases[_collectionId].timePeriod) - collectionPhases[_collectionId].allowlistStartTime));
//@audit If Linear Descending Sale
} else {
if (((collectionPhases[_collectionId].collectionMintCost - collectionPhases[_collectionId].collectionEndMintCost) / (collectionPhases[_collectionId].rate)) > tDiff) {
price = collectionPhases[_collectionId].collectionMintCost - (tDiff * collectionPhases[_collectionId].rate);
} else {
price = collectionPhases[_collectionId].collectionEndMintCost;
}
}
if (price - decreaserate > collectionPhases[_collectionId].collectionEndMintCost) {
return price - decreaserate;
} else {
return collectionPhases[_collectionId].collectionEndMintCost;
}
} else {
// fixed price
return collectionPhases[_collectionId].collectionMintCost;
}
}
We can see that if the collectionPhases[_collectionId].salesOption == 2 (it’s the number for a descending sale model), and if the block.timestamp is > allowlistStartTime and < publicEndTime. The price is correctly computed. A little check on the mint() function:
function mint(uint256 _collectionID, uint256 _numberOfTokens, uint256 _maxAllowance, string memory _tokenData, address _mintTo, bytes32[] calldata merkleProof, address _delegator, uint256 _saltfun_o) public payable {
...
} else if (block.timestamp >= collectionPhases[col].publicStartTime && block.timestamp <= collectionPhases[col].publicEndTime) {
...
} else {
// fixed price
return collectionPhases[_collectionId].collectionMintCost;
}
}
But if the publicEndTime is strictly equal to publicEndTime, the returned price is the collectionMintCost instead of collectionEndMintCost because the logic go to the “else” branch. It’s an incorrect price because it’s the price at the beginning of the collection. As you can see on mint(), a user can mint a token on the block.timestamp publicEndTime.
Users that mint on the last block.timstamp mint at an unexpected price and for all the minting methods which use the getPrice() function.
Logs
You can see the logs of the test with this image:
Note: to view the image, please see the original submission here.
Here’s the gist if you want to execute the PoC directly. You can execute the test with this command:
forge test --mt testgetWrongPriceAtEnd -vvv (or with -vvvvv to see all the transaction logs)
Recommended Mitigation Steps
Change the < & > to <= & => on the else/if branch inside the getPrice() function.
function getPrice(uint256 _collectionId) public view returns (uint256) {
uint tDiff;
//@audit-info If Periodic Sale
if (collectionPhases[_collectionId].salesOption == 3) {
...
-- } else if (collectionPhases[_collectionId].salesOption == 2 && block.timestamp > collectionPhases[_collectionId].allowlistStartTime && -- block.timestamp < collectionPhases[_collectionId].publicEndTime){
++ } else if (collectionPhases[_collectionId].salesOption == 2 && block.timestamp => collectionPhases[_collectionId].allowlistStartTime && ++ block.timestamp <= collectionPhases[_collectionId].publicEndTime){
tDiff = (block.timestamp - collectionPhases[_collectionId].allowlistStartTime) / collectionPhases[_collectionId].timePeriod;
uint256 price;
uint256 decreaserate;
//@audit If Exponential Descending Sale
if (collectionPhases[_collectionId].rate == 0) {
price = collectionPhases[_collectionId].collectionMintCost / (tDiff + 1);
decreaserate = ((price - (collectionPhases[_collectionId].collectionMintCost / (tDiff + 2))) / collectionPhases[_collectionId].timePeriod) * ((block.timestamp - (tDiff * collectionPhases[_collectionId].timePeriod) - collectionPhases[_collectionId].allowlistStartTime));
//@audit If Linear Descending Sale
} else {
if (((collectionPhases[_collectionId].collectionMintCost - collectionPhases[_collectionId].collectionEndMintCost) / (collectionPhases[_collectionId].rate)) > tDiff) {
price = collectionPhases[_collectionId].collectionMintCost - (tDiff * collectionPhases[_collectionId].rate);
} else {
price = collectionPhases[_collectionId].collectionEndMintCost;
}
}
if (price - decreaserate > collectionPhases[_collectionId].collectionEndMintCost) {
return price - decreaserate;
} else {
return collectionPhases[_collectionId].collectionEndMintCost;
}
} else {
// fixed price
return collectionPhases[_collectionId].collectionMintCost;
}
}
Assessed type
Invalid Validation
a2rocket (NextGen) confirmed via duplicate issue #1391
The Warden specifies an inconsistency within the code whereby the price calculation function will misbehave when measured for a sale type of
2combined with theblock.timestampbeing thepublicEndTimeof the collection’s sale period.The finding is correct given that the relevant mint functions in the
MinterContractperform an inclusive evaluation for both theallowlistStartTimeand thepublicEndTime, permitting this vulnerability to manifest when theblock.timestampis exactly equal to thepublicEndTime.The Sponsor has confirmed this in #1391 and I accept the medium severity classification based on the submission’s likelihood being low (due to the strict
block.timestamprequirement), but the impact being high as an NFT would either be bought at a discount an arbitrary number of times, hurting the artist, or at a high markup, hurting the buyer.The Warden’s submission was selected as the best due to the presence of a PoC with logs that properly emphasize how the issue can be exacerbated depending on the configuration of a sale and its recommended mitigation being sufficient in rectifying the vulnerability.
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 getPrice function uses strict inequalities (block.timestamp < publicEndTime) while the mint function allows minting at block.timestamp <= publicEndTime, causing a mismatch at the boundary.
Impact
A user minting at the exact end time could pay either a much higher price (if the sale was descending) or a lower price, harming either the buyer or the artist; the likelihood is low due to the strict timestamp requirement.
What the attacker needed
Descending sale model configured; Mint exactly at publicEndTime
The fix
The fix is to change the strict inequalities in getPrice to inclusive ones (<= and >=) to match the mint function's boundaries.
How it was found
- Set up a collection with a linear or exponential descending sale model.
- Ensure the current block.timestamp is exactly equal to the publicEndTime.
- Call a mint function that uses getPrice().
- Observe that the price returned is the initial collectionMintCost instead of the expected end cost.
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 a descending sale, the price of an NFT drops over time. The code that calculates the price checks if the current time is before the end time, but it uses a strict 'less than' instead of 'less than or equal to'. So if someone buys exactly at the end time, the code thinks the sale hasn't started and charges the starting price, which is wrong.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.