The tier setting parameter are unsafely downcasted from type uint256 to type uint80 / uint48 / uint40 / uint16
The smart contract stores tier settings by casting uint256 values to smaller types without checking, so large values are truncated. An attacker could exploit this to underpay for an NFT if the owner sets a high contribution floor.
Submitted by ladboy233, also found by brgltd
https://github.com/jbx-protocol/juice-nft-rewards/blob/f9893b1497098241dd3a664956d8016ff0d0efd0/contracts/JBTiered721Delegate.sol#L240 https://github.com/jbx-protocol/juice-nft-rewards/blob/f9893b1497098241dd3a664956d8016ff0d0efd0/contracts/JBTiered721DelegateStore.sol#L628 https://github.com/jbx-protocol/juice-nft-rewards/blob/f9893b1497098241dd3a664956d8016ff0d0efd0/contracts/JBTiered721DelegateStore.sol#L689
The tier setting parameter are unsafely downcasted from uint256 to uint80 / uint48 / uint16
the tier is setted by owner is crucial because the parameter affect how nft is minted.
the callstack is
JBTiered721Delegate.sol#initialize -> Store#recordAddTiers
function recordAddTiers(JB721TierParams[] memory _tiersToAdd)
what does the struct JB721TierParams look like? all parameter in JB721TierParams is uint256 type
struct JB721TierParams {
uint256 contributionFloor;
uint256 lockedUntil;
uint256 initialQuantity;
uint256 votingUnits;
uint256 reservedRate;
address reservedTokenBeneficiary;
bytes32 encodedIPFSUri;
bool allowManualMint;
bool shouldUseBeneficiaryAsDefault;
}
however in side the function
// Record adding the provided tiers.
if (_pricing.tiers.length > 0) _store.recordAddTiers(_pricing.tiers);
all uint256 parameter are downcasted.
// Add the tier with the iterative ID.
_storedTierOf[msg.sender][_tierId] = JBStored721Tier({
contributionFloor: uint80(_tierToAdd.contributionFloor),
lockedUntil: uint48(_tierToAdd.lockedUntil),
remainingQuantity: uint40(_tierToAdd.initialQuantity),
initialQuantity: uint40(_tierToAdd.initialQuantity),
votingUnits: uint16(_tierToAdd.votingUnits),
reservedRate: uint16(_tierToAdd.reservedRate),
allowManualMint: _tierToAdd.allowManualMint
});
uint256 contributionFloor is downcasted to uint80,
uint256 lockedUntil is downcasted to uint48
uint256 initialQuantity and initialQuantity are downcasted to uint40
uint256 votingUnits and uint256 reservedRate are downcasted to uint16
this means the original setting is greatly truncated.
For example, the owner wants to set the initial supply to a number larger than uint40, but the supply is truncated to type(uint40).max
The owner wants to set the contribution floor price above uint80,but the contribution floor price is truncated to type(uint80).max, the user may underpay the price and get the NFT price at a discount.
Proof of Concept
We can add POC
function testJBTieredNFTRewardDelegate_mintFor_mintArrayOfTiers_downcast_POC() public {
uint256 nbTiers = 1;
vm.mockCall(
mockJBProjects,
abi.encodeWithSelector(IERC721.ownerOf.selector, projectId),
abi.encode(owner)
);
JB721TierParams[] memory _tiers = new JB721TierParams[](nbTiers);
uint16[] memory _tiersToMint = new uint16[](nbTiers);
// Temp tiers, will get overwritten later (pass the constructor check)
uint256 originalFloorPrice = 10000000000000000000000000 ether;
for (uint256 i; i < nbTiers; i++) {
_tiers[i] = JB721TierParams({
contributionFloor: originalFloorPrice,
lockedUntil: uint48(0),
initialQuantity: 20,
votingUnits: uint16(0),
reservedRate: uint16(0),
reservedTokenBeneficiary: reserveBeneficiary,
encodedIPFSUri: tokenUris[i],
allowManualMint: true, // Allow this type of mint
shouldUseBeneficiaryAsDefault: false
});
_tiersToMint[i] = uint16(i)+1;
_tiersToMint[_tiersToMint.length - 1 - i] = uint16(i)+1;
}
ForTest_JBTiered721DelegateStore _ForTest_store = new ForTest_JBTiered721DelegateStore();
ForTest_JBTiered721Delegate _delegate = new ForTest_JBTiered721Delegate(
projectId,
IJBDirectory(mockJBDirectory),
name,
symbol,
IJBFundingCycleStore(mockJBFundingCycleStore),
baseUri,
IJBTokenUriResolver(mockTokenUriResolver),
contractUri,
_tiers,
IJBTiered721DelegateStore(address(_ForTest_store)),
JBTiered721Flags({
lockReservedTokenChanges: false,
lockVotingUnitChanges: false,
lockManualMintingChanges: true,
pausable: true
})
);
_delegate.transferOwnership(owner);
uint256 floorPrice = _delegate.test_store().tier(address(_delegate), 1).contributionFloor;
console.log("original floor price");
console.log(originalFloorPrice);
console.log("truncated floor price");
console.log(floorPrice);
}
note, our initial contribution floor price setting is
uint256 originalFloorPrice = 10000000000000000000000000 ether;
for (uint256 i; i < nbTiers; i++) {
_tiers[i] = JB721TierParams({
contributionFloor: originalFloorPrice,
then we run our test
forge test -vv --match testJBTieredNFTRewardDelegate_mintFor_mintArrayOfTiers_downcast_POC
the result is
[PASS] testJBTieredNFTRewardDelegate_mintFor_mintArrayOfTiers_downcast_POC() (gas: 7601212)
Logs:
original floor price
10000000000000000000000000000000000000000000
truncated floor price
863278115882885135204352
Test result: ok. 1 passed; 0 failed; finished in 10.43ms
clearly the floor price is unsafed downcasted and truncated.
Tools Used
Foundry, Manual Review
Recommended Mitigation Steps
We recommend the project either change the data type in the struct
struct JB721TierParams {
uint256 contributionFloor;
uint256 lockedUntil;
uint256 initialQuantity;
uint256 votingUnits;
uint256 reservedRate;
address reservedTokenBeneficiary;
bytes32 encodedIPFSUri;
bool allowManualMint;
bool shouldUseBeneficiaryAsDefault;
}
or safely downcast the number to make sure the number is not shortened unexpectedly.
https://docs.openzeppelin.com/contracts/3.x/api/utils#SafeCast
drgorillamd (Juicebox DAO) commented:
Thank you for the real poc:)
The warden showed how due to casting the original parameters could be truncated
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 contract downcasts uint256 parameters to smaller types (uint80, uint48, uint40, uint16) without checking for overflow, causing silent truncation.
Impact
If the owner sets a contribution floor above uint80 max, it is truncated, allowing users to underpay and mint NFTs at a discount.
What the attacker needed
Owner sets a tier with a value exceeding the smaller type's…
The fix
The project should either change the struct fields to uint256 or use safe casting to prevent truncation.
How it was found
- Deploy the contract with a tier whose contributionFloor is set to a very large uint256 value.
- Call the function that records the tier, which downcasts the value.
- Read the stored tier's contributionFloor and observe it is truncated.
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 Ethereum smart contracts, numbers have fixed sizes. If you try to store a very large number in a smaller slot, it gets cut off, like pouring a gallon into a pint pot. Here, the contract does that without warning, so a price meant to be huge becomes much smaller, letting someone buy an NFT cheaply.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.