YieldWUSDStaking.repay() may incorrectly over-refund remainAmount to user
In YieldWUSDStaking.repay(), when the remaining amount is less than the unstake fine, the code fails to reset the remaining amount to zero, causing an over-refund to the user. This can also make the contract balance insufficient, preventing others from repaying.
bin2chen Follow with oakcobalt and SpicyMeatball
Originally disclosed on Code4rena
Submitted by bin2chen, also found by oakcobalt and SpicyMeatball
Code reference: YieldWUSDStaking.sol#L468
In YieldWUSDStaking.repay(), we will calculate the amount that needs to be refunded to the user remainAmount:
function _repay(uint32 poolId, address nft, uint256 tokenId) internal virtual {
...
// compute fine value
if (vars.remainAmount >= sd.unstakeFine) {
vars.remainAmount = vars.remainAmount - sd.unstakeFine;
} else {
vars.extraAmount = vars.extraAmount + (sd.unstakeFine - vars.remainAmount);
@> // missing clear vars.remainAmount = 0
}
@> sd.remainYieldAmount = vars.remainAmount;
...
// send remain funds to owner
if (sd.remainYieldAmount > 0) {
@> underlyingAsset.safeTransfer(vars.nftOwner, sd.remainYieldAmount);
sd.remainYieldAmount = 0;
}
The problem is that when use call repay(), if vars.remainAmount < sd.unstakeFine, it doesn’t clear vars.remainAmount to 0, which results in a refund to the user, which should be treated as unstakeFine.
For example: unstakeFine = 10 , remainAmount = 5
As currently calculated
- extraAmount = (unstakeFine - remainAmount ) = 10 - 5 = 5
- remainYieldAmount = 5 =====> ( should 0)
Impact
Over-refund remainAmount to user.
At the same time, it will also cause the contract balance to be insufficient (after collectFeeToTreasury ()) and others will not be able to repay.
Recommended Mitigation
function _repay(uint32 poolId, address nft, uint256 tokenId) internal virtual {
...
// compute fine value
if (vars.remainAmount >= sd.unstakeFine) {
vars.remainAmount = vars.remainAmount - sd.unstakeFine;
} else {
vars.extraAmount = vars.extraAmount + (sd.unstakeFine - vars.remainAmount);
+ if(msg.sender != botAdmin) {
+ vars.remainAmount = 0;
+ }
}
sd.remainYieldAmount = vars.remainAmount;
...
// send remain funds to owner
if (sd.remainYieldAmount > 0) {
underlyingAsset.safeTransfer(vars.nftOwner, sd.remainYieldAmount);
sd.remainYieldAmount = 0;
}
thorseldon (BendDAO) confirmed and commented:
Fixed in commit 75055c9
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
Likely a missing assignment to zero in the else branch when remainAmount is less than unstakeFine.
Impact
Over-refund remainAmount to the user, and the contract balance may become insufficient after collectFeeToTreasury(), preventing others from repaying.
What the attacker needed
RemainAmount < unstakeFine; caller is not botAdmin
The fix
The issue was fixed by adding a conditional reset of remainAmount to zero when the sender is not the bot admin.
How it was found
- Call repay() with a remainAmount less than unstakeFine.
- Observe that remainAmount is not cleared to zero.
- The contract refunds remainAmount to the user.
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 repay a loan, the contract calculates how much of your payment is left after covering a fee. If your payment is smaller than the fee, the code should set the leftover to zero, but it forgets to do so. As a result, you get back money that you actually still owe, and the contract may not have enough funds for other users.
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.