YieldStakingBase::collectFeeToTreasury will attempt to transfer funds that are not available, resulting in repetitive revert and potential DOS
The collectFeeToTreasury function attempts to transfer an amount that includes unstaking fines not yet available, because totalUnstakeFine is incremented during unstake before the fine is actually repaid. This can cause the function to revert repeatedly, potentially locking protocol funds.
oakcobalt Follow with SpicyMeatball
Originally disclosed on Code4rena
Submitted by oakcobalt, also found by SpicyMeatball
Code references:
YieldStakingBase::collectFeeToTreasury will attempt to transfer funds that are not available, resulting in repetitive revert.
Proof of Concept
The vulnerability is totalUnstakFine is incremented in the unstake flow by the botAmin. Note that the fund is not available at this point until later when the withdrawal can be claimed in repay flow.
//src/yield/YieldStakingBase.sol
function _unstake(uint32 poolId, address nft, uint256 tokenId, uint256 unstakeFine) internal virtual {
...
// sender must be bot or nft owner
if (msg.sender == botAdmin) {
require(unstakeFine <= nc.maxUnstakeFine, Errors.YIELD_ETH_EXCEED_MAX_FINE);
uint256 hf = calculateHealthFactor(nft, nc, sd);
require(hf < nc.unstakeHeathFactor, Errors.YIELD_ETH_HEATH_FACTOR_TOO_HIGH);
sd.unstakeFine = unstakeFine;
|> totalUnstakeFine += unstakeFine;//@audit at this point, unstakeFine is not availalbe because the position is not claimed until repay() flow.
}
...
In collectFeeToTreasuy, the transfer amount is totalUnstakeFine - claimedUnstakeFine which includes the funds that are not ready to claim.
//src/yield/YieldStakingBase.sol
function collectFeeToTreasury() public virtual onlyPoolAdmin {
address to = addressProvider.getTreasury();
require(to != address(0), Errors.TREASURY_CANNOT_BE_ZERO);
if (totalUnstakeFine > claimedUnstakeFine) {
|> uint256 amountToCollect = totalUnstakeFine - claimedUnstakeFine;
claimedUnstakeFine += amountToCollect;
underlyingAsset.safeTransfer(to, amountToCollect);
emit CollectFeeToTreasury(to, amountToCollect);
}
}
Vulnerable cases:
-
tx racing. Since bot is programmed to call
unstakeanytime based on the set condition of unhealthy positions, its unstake tx might settle before a pendingcollectFeeToTreasuryby poolAdmin, causingcollectFeeToTreasuryto revert; -
No guarantee that the fines can be repaid. When bot can only partially
repaythe staking positions, unstakingFines might not be repaid. It’s up to the user to call repay() to fully cover total debts + fines(vars.extraAmount). This causes further uncertainty as to whentotalUnstakeFinecan be available. And if the user is not able to repay,totalUnstakeFinewill never be recovered. This DOSescollectFeeToTreasury.//src/yield/YieldStakingBase.sol 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); } sd.remainYieldAmount = vars.remainAmount; // transfer eth from sender exclude bot admin |> if ((vars.extraAmount > 0) && (msg.sender != botAdmin)) { underlying asset.safeTransferFrom(msg.sender, address(this), vars.extraAmount); }
Impact
collectFeeToTreasury might face repetitive revert or even DOS. protocol funds can be locked.
Recommended mitigation steps
Consider only incrementing totalUnstakFine in _repay. The increment amount is the actual funds that are claimed or covered by the user.
thorseldon (BendDAO) acknowledged
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
Impact
collectFeeToTreasury may revert repeatedly or become permanently blocked, potentially locking protocol funds.
What the attacker needed
Bot admin unstakes an unhealthy position; pool admin calls collectFeeToTreasury; fine not yet repaid
The fix
The totalUnstakeFine should only be incremented in the repay function, by the amount actually claimed or covered by the user.
How it was found
- The bot admin calls unstake on an unhealthy position, which increments totalUnstakeFine.
- The pool admin calls collectFeeToTreasury, which attempts to transfer totalUnstakeFine minus claimedUnstakeFine.
- The transfer fails because the fine amount is not yet available, causing a revert.
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 someone unstakes early, they pay a fine. The contract records the fine immediately, but the money isn't actually there until the position is repaid. The treasury collection function then tries to move money that isn't available, so it fails and can get stuck.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.