When LockManager.lockOnBehalf is called from MigrationManager , the user’s reminder will be set to 0, resulting in fewer received MunchableNFTs
When the MigrationManager calls LockManager.lockOnBehalf, the _lock function skips the remainder calculation and resets the stored remainder to zero. This causes users to lose any previously accumulated remainder, resulting in fewer MunchableNFTs being minted and also resets the unlock time, preventing users from accessing their funds.
PetarTolev Follow with joaovwfreire, bearonbike and 2 others
Originally disclosed on Code4rena
Submitted by PetarTolev, also found by joaovwfreire, bearonbike, Drynooo, and MrPotatoMagic
Lines of code
https://github.com/code-423n4/2024-05-munchables/blob/57dff486c3cd905f21b330c2157fe23da2a4807d/src/managers/MigrationManager.sol#L264 https://github.com/code-423n4/2024-05-munchables/blob/57dff486c3cd905f21b330c2157fe23da2a4807d/src/managers/MigrationManager.sol#L285 https://github.com/code-423n4/2024-05-munchables/blob/57dff486c3cd905f21b330c2157fe23da2a4807d/src/managers/MigrationManager.sol#L335-L347 https://github.com/code-423n4/2024-05-munchables/blob/57dff486c3cd905f21b330c2157fe23da2a4807d/src/managers/LockManager.sol#L293 https://github.com/code-423n4/2024-05-munchables/blob/57dff486c3cd905f21b330c2157fe23da2a4807d/src/managers/LockManager.sol#L345-L379
LockManager.lockOnBehalf is called when the user has MunchableNFT which should be migrated.
The LockManager._lock function called by the lockOnBehalf handles the actual locking process. This function does several important steps:
- It verifies that the account is registered and that there is sufficient allowance for the tokens to be locked.
- It calculates the total amount to lock, including any
remainderfrom previous locks. - It calculates the number of NFTs the user will receive based on the locked amount, only if
msg.sender != MigrationManager - It transfers the tokens to the contract.
- It updates the locked amount and the remainder (here is the issue, when called from the
MigrationManager) - It sets the unlock time based on the lock duration.
The remainder is an important part of this process. It represents any leftover amount that wasn’t enough to mint an additional NFT. Normally, this remainder is added to the next lock amount, allowing users to efficiently utilize all their tokens over time.
However, when _lock is called from MigrationManager, it improperly resets the remainder to 0 (step 5). This happens because the function doesn’t differentiate whether it’s being called from the migration process or a standard lock request. As a result, users end up receiving fewer MunchableNFTs than they should because the small leftover amounts are discarded instead of being carried over to the next lock.
Impact
The user’s funds won’t be used efficiently, resulting in fewer received MunchableNFTs. Also, the user won’t be able to unlock these unused funds and relock them to be used. This happens because the MigrationManager resets the unlock time, forcing the user to wait an extra lockDuration.
Proof of Concept
When MigrationManager.migrateAllNFTs or MigrationManager.migratePurchasedNFTs is called
- It calls
_migrateNFTs, which in turn callsLockManager.lockOnBehalf
function _migrateNFTs(
address _user,
address _tokenLocked,
uint256[] memory tokenIds
) internal {
// ...
uint256 quantity = (totalLockAmount * discountFactor) / 10e12;
if (_tokenLocked == address(0)) {
_lockManager.lockOnBehalf{value: quantity}(
_tokenLocked,
quantity,
_user
);
} else if (_tokenLocked == address(WETH)) {
WETH.approve(address(_lockManager), quantity);
_lockManager.lockOnBehalf(_tokenLocked, quantity, _user);
} else if (_tokenLocked == address(USDB)) {
USDB.approve(address(_lockManager), quantity);
_lockManager.lockOnBehalf(_tokenLocked, quantity, _user);
}
emit MigrationSucceeded(_user, migratedTokenIds, newTokenIds);
}
lockOnBehalfcalls the_lockfunction- When
_lockis called fromMigrationManager, theremaindercalculation is skipped and is effectively reset to 0, causing users to lose potentialMunchableNFTsfrom their leftover token amounts
function _lock(
address _tokenContract,
uint256 _quantity,
address _tokenOwner,
address _lockRecipient
) private {
// ...
// add remainder from any previous lock
uint256 quantity = _quantity + lockedToken.remainder;
@> uint256 remainder;
uint256 numberNFTs;
uint32 _lockDuration = playerSettings[_lockRecipient].lockDuration;
if (_lockDuration == 0) {
_lockDuration = lockdrop.minLockDuration;
}
if (
lockdrop.start <= uint32(block.timestamp) &&
lockdrop.end >= uint32(block.timestamp)
) {
if (
_lockDuration < lockdrop.minLockDuration ||
_lockDuration >
uint32(configStorage.getUint(StorageKey.MaxLockDuration))
) revert InvalidLockDurationError();
@> if (msg.sender != address(migrationManager)) {
// calculate number of nfts
@> remainder = quantity % configuredToken.nftCost;
numberNFTs = (quantity - remainder) / configuredToken.nftCost;
if (numberNFTs > type(uint16).max) revert TooManyNFTsError();
// Tell nftOverlord that the player has new unopened Munchables
nftOverlord.addReveal(_lockRecipient, uint16(numberNFTs));
}
}
// ...
@> lockedToken.remainder = remainder;
lockedToken.quantity += _quantity;
lockedToken.lastLockTime = uint32(block.timestamp);
lockedToken.unlockTime = uint32(block.timestamp) + uint32(_lockDuration);
// ...
}
Coded POC
Place the following test case in src/test/SpeedRun.t.sol and run it with the command forge test --mt test_reminderWillBeResetWhenLockMigrateNFTMigrationManager -vvv.
address alice = makeAddr("alice");
function test_reminderWillBeResetWhenLockMigrateNFTMigrationManager() public {
deployContracts();
vm.prank(alice);
amp.register(MunchablesCommonLib.Realm(3), address(0));
uint256 lockAmount = 1.9e18;
deal(alice, 2e18);
deal(address(migm), 2e18);
uint256 totalLocked;
vm.prank(alice);
lm.lock{value: lockAmount}(address(0), lockAmount);
totalLocked += lockAmount;
// skip lockDuration
skip(lm.getPlayerSettings(alice).lockDuration);
// Alice can unlock here
vm.prank(alice);
lm.unlock(address(0), 0.1e18);
totalLocked -= 0.1e18;
vm.prank(address(migm));
lm.lockOnBehalf{value: 1e18}(address(0), 1e18, alice);
// Alice cannot unlock here, because MigrationManager has reset the unlockTime
vm.expectRevert();
vm.prank(alice);
lm.unlock(address(0), 0.1e18);
// But also, if lock the remaining amount to fill the remainder, won't receive an NFT.
vm.prank(alice);
lm.lock{value: 0.2e18}(address(0), 0.2e18);
totalLocked += 0.2e18;
console.log("totalLocked: ", totalLocked);
console.log("unrevealedNFTs: ", nfto.getUnrevealedNFTs(alice));
}
Logs:
totalLocked: 2000000000000000000
unrevealedNFTs: 1
Recommended Mitigation Steps
Do not reset the remainder storage variable when the _lock function is called from the MigrationManager. Instead, ensure the remainder is correctly accumulated and carried over, allowing users to receive the correct amount of MunchableNFTs based on their total locked amount. This can be done by adding a condition to check the caller and handle the remainder accordingly.
Assessed type
Context
The Warden and its duplicates outline a behavior whereby the migration manager will overwrite the
remainderof a lock entry even if it is within the lock drop period. This is invalid behavior and will result in unaccounted remainders that will not carry over to the next lock operation properly.I believe a medium-severity rating is acceptable given that it affects how NFTs are awarded in the lock drop system and I have selected this submission as the best due to describing the vulnerability in great and accurate detail.
0xinsanity (Munchables) commented:
If its coming from the migration manager, we don’t want to count that quantity towards the remainder calculations since we are choosing to migrate over old NFTs, not mint new ones.
Thanks to everyone who contributed to this submission’s discussion including the Sponsor @0xinsanity, and the Wardens @Tomiwasa0, @PetarTolev, @niser93, @0jovi0, and @Ys-Prakash!
I will address multiple concerns raised in as much of a concise manner as possible.
Addressing the Sponsor @0xinsanity and discussing from a security auditor perspective, I would like to note that the vulnerability does not relate to the remainder that should have been assigned for a migrated NFT, but rather the remainder a user has accumulated with previous locks. If they have accumulated a non-zero remainder with previous locks and migrated afterward, they would lose that remainder. As such, I implore you to revisit this submission.
Addressing the Wardens’ concerns and discussing from a C4 perspective:
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 _lock function does not differentiate between a normal lock and a migration lock, so it resets the remainder to zero when called from MigrationManager.
Impact
Users receive fewer MunchableNFTs because their leftover token amounts are discarded, and they cannot unlock and relock these funds due to the reset unlock time, forcing them to wait an extra lockDuration.
What the attacker needed
User has a non-zero remainder from previous locks; migration is triggered
The fix
The _lock function should not reset the remainder when called from MigrationManager; instead, it should accumulate and carry over the remainder correctly.
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 you lock tokens, any leftover amount that isn't enough to mint an NFT is saved as a 'remainder' and added to your next lock. The migration process, which moves old NFTs to a new system, accidentally wipes out that saved remainder. So you end up with fewer NFTs than you should, and your tokens stay locked longer.
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.