Deleting nft Info can cause users’ nft.unpaidRewards to be permanently erased
When a user withdraws a staked NFT, the contract deletes all stored information for that NFT, including any unpaid rewards that were recorded because the contract lacked sufficient balance to pay them at that moment. This permanently erases the user's claim to those rewards, so they can never be recovered.
Submitted by WatchPug, also found by 0xDjango and hyh
LiquidityFarming.sol#L229-L253
function withdraw(uint256 _nftId, address payable _to) external whenNotPaused nonReentrant {
address msgSender = _msgSender();
uint256 nftsStakedLength = nftIdsStaked[msgSender].length;
uint256 index;
for (index = 0; index < nftsStakedLength; ++index) {
if (nftIdsStaked[msgSender][index] == _nftId) {
break;
}
}
require(index != nftsStakedLength, "ERR__NFT_NOT_STAKED");
nftIdsStaked[msgSender][index] = nftIdsStaked[msgSender][nftIdsStaked[msgSender].length - 1];
nftIdsStaked[msgSender].pop();
_sendRewardsForNft(_nftId, _to);
delete nftInfo[_nftId];
(address baseToken, , uint256 amount) = lpToken.tokenMetadata(_nftId);
amount /= liquidityProviders.BASE_DIVISOR();
totalSharesStaked[baseToken] -= amount;
lpToken.safeTransferFrom(address(this), msgSender, _nftId);
emit LogWithdraw(msgSender, baseToken, _nftId, _to);
}
LiquidityFarming.sol#L122-L165
function _sendRewardsForNft(uint256 _nftId, address payable _to) internal {
NFTInfo storage nft = nftInfo[_nftId];
require(nft.isStaked, "ERR__NFT_NOT_STAKED");
(address baseToken, , uint256 amount) = lpToken.tokenMetadata(_nftId);
amount /= liquidityProviders.BASE_DIVISOR();
PoolInfo memory pool = updatePool(baseToken);
uint256 pending;
uint256 amountSent;
if (amount > 0) {
pending = ((amount * pool.accTokenPerShare) / ACC_TOKEN_PRECISION) - nft.rewardDebt + nft.unpaidRewards;
if (rewardTokens[baseToken] == NATIVE) {
uint256 balance = address(this).balance;
if (pending > balance) {
unchecked {
nft.unpaidRewards = pending - balance;
}
(bool success, ) = _to.call{value: balance}("");
require(success, "ERR__NATIVE_TRANSFER_FAILED");
amountSent = balance;
} else {
nft.unpaidRewards = 0;
(bool success, ) = _to.call{value: pending}("");
require(success, "ERR__NATIVE_TRANSFER_FAILED");
amountSent = pending;
}
} else {
IERC20Upgradeable rewardToken = IERC20Upgradeable(rewardTokens[baseToken]);
uint256 balance = rewardToken.balanceOf(address(this));
if (pending > balance) {
unchecked {
nft.unpaidRewards = pending - balance;
}
amountSent = _sendErc20AndGetSentAmount(rewardToken, balance, _to);
} else {
nft.unpaidRewards = 0;
amountSent = _sendErc20AndGetSentAmount(rewardToken, pending, _to);
}
}
}
nft.rewardDebt = (amount * pool.accTokenPerShare) / ACC_TOKEN_PRECISION;
emit LogOnReward(_msgSender(), baseToken, amountSent, _to);
}
When withdraw() is called, _sendRewardsForNft(_nftId, _to) will be called to send the rewards.
In _sendRewardsForNft(), when address(this).balance is insufficient at the moment, nft.unpaidRewards = pending - balance will be recorded and the user can get it back at the next time.
However, at L244, the whole nftInfo is being deleted, so that nft.unpaidRewards will also get erased.
There is no way for the user to get back this unpaidRewards anymore.
Recommended Mitigation Steps
Consider adding a new parameter named force for withdraw(), require(force || unpaidRewards == 0) before deleting nftInfo.
ankurdubey521 (Biconomy) confirmed and commented:
Great catch! Thanks a lot for bringing these up.
HP-25: C4 Audit Fixes, Dynamic Fee Changes bcnmy/hyphen-contract#42
Great find, deserves a severity of high as it may incur in funds lost for the users.
KenzoAgada (warden) commented:
Shouldn’t this be medium severity, as only rewards are lost and not original user funds? As the risk TLDR says -
2 — Med: Assets not at direct risk, but the function of the protocol or its availability could be impacted, or leak value with a hypothetical attack path with stated assumptions, but external requirements.
3 — High: Assets can be stolen/lost/compromised directly (or indirectly if there is a valid attack path that does not have hand-wavy hypotheticals).There are other lost-rewards issues that have been classified as high, this questions pertains to them as well.
0xleastwood (warden) commented:
I would be inclined to keep this as high risk as it is less about the protocol leaking value and more about rewards being completely wiped and lost forever. I would argue, the user’s assets at this point in time DO include all unpaid rewards, so it is perfectly reasonable to treat this as high risk.
Agree that the boundaries are not very clear, this issue might fall somewhere between Medium and High severities. But my initial thought was similar to that of @0xleastwood, the rewards already belong to the user, and losing them will make the user lose on time and other opportunities. Also, this is not a hypothetical attack scenario, but a very real valid execution path, thus I think a high severity is fine here.
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 withdraw function deletes the entire nftInfo record without checking whether it contains a non-zero unpaidRewards balance.
Impact
The user permanently loses any unpaid rewards that were recorded because the contract could not pay them in full at withdrawal time.
What the attacker needed
User has staked an NFT; contract lacks sufficient reward balance
The fix
The withdraw function should require that unpaidRewards is zero before deleting nftInfo, or provide a force parameter to allow deletion only when the user explicitly accepts losing the rewards.
How it was found
- A user stakes an NFT and accrues rewards.
- The contract's balance is insufficient to pay the full pending reward when withdraw is called.
- The shortfall is stored in nft.unpaidRewards.
- The withdraw function then deletes the entire nftInfo record, erasing the unpaidRewards.
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.
A smart contract keeps a record for each staked NFT, including any rewards the user is owed but hasn't been paid yet. When the user withdraws the NFT, the contract deletes that entire record, wiping out the unpaid reward amount. Since the record is gone, the user has no way to claim those rewards later.
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.