Skip to content
Business logic Audit finding Intermediate

debtWriteOff updates totalFrozen immaturely, thereby losing staker rewards

The debtWriteOff function updates totalFrozen before withdrawing unionToken rewards, which can cause the staker to lose rewards if totalStaked equals totalFrozen. An attacker could exploit this by calling debtWriteOff when overdue, resulting in zero rewards.

kenzo Follow

Originally disclosed on Code4rena

Submitted by kenzo, also found by itsmeSTYJ

debtWriteOff updates totalFrozen before withdrawing unionToken rewards. As the borrower is overdue, this means the staker calling debtWriteOff will lose his rewards if for example totalStaked == totalFrozen. (Note: If the borrower would to first call withdrawRewards/stake/unstake before calling debtWriteOff, he would get the rewards.)

Impact

Staker loses rewards. (Or at the very least, inconsistency at rewards calculation between debtWriteOff and stake/unstake/withdrawRewards.)

Proof of Concept

debtWriteOff first calls updateTotalFrozen, and then comptroller.withdrawRewards: L710: L712

updateTotalFrozen can update totalFrozen to be same as totalStaked. comptroller.withdrawRewards calls calculateRewardsByBlocks: Comptroller.sol L98

calculateRewardsByBlocks is calculating userManagerContract.totalStaked() - userManagerData.totalFrozen, which can be 0 in certain cases, Comptroller.sol L140

and passing it as the third parameter to calculateRewards: Comptroller.sol L167

In calculateRewards, if the third parameter is 0, the user won’t get any rewards.

Comptroller.sol L253

So in this scenario the user won’t get any rewards after calling debtWriteOff.

If we were to call updateTotalFrozen after the withdrawing of the rewards, or if the staker would call withdrawRewards before calling debtWriteOff, the totalFrozen would not have been updated, and the user would get his rewards by calling debtWriteOff. As I mentioned earlier, if there’s a reason I’m not seeing as to why updateTotalFrozen is updated in debtWriteOff before withdrawRewards is called, then it is not consistent with stake/unstake/withdrawRewards functions.

I have a created an (admittedly hacky) script to show the bug. It will run two scenarios which are almost the same (based on integration/testUserManager.js). In the first the staker will call debtWriteOff at some point, and at the second the staker will call withdrawRewards at the same point, and the test will print the difference in unionToken balance after each call. File password: “union”. https://pastebin.com/xkS0PXtq

Tools Used

Manual analysis, hardhat.

Recommended Mitigation Steps

If I am not missing anything, in debtWriteOff I would move the withdrawRewards to before updateTotalFrozen.

UserManager.sol L710:L712

kingjacob (Union) acknowledged

GalloDaSballo (judge) commented:

As described by the warden, the debtWriteOff function causes users to loose rewards, since this is a loss of yield will maintain a Med Risk severity

The original report as published on Code4rena, by kenzo. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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 the function updates totalFrozen before withdrawing rewards, causing the reward calculation to use a zero denominator.

Impact

The staker loses rewards, or at least there is an inconsistency in reward calculation between debtWriteOff and other functions.

What the attacker needed

Borrower is overdue; totalStaked equals totalFrozen

The fix

The withdrawRewards call should be moved before updateTotalFrozen in debtWriteOff.

How it was found

  1. Call debtWriteOff when the borrower is overdue and totalStaked equals totalFrozen.
  2. Observe that updateTotalFrozen sets totalFrozen equal to totalStaked.
  3. Then withdrawRewards is called, which calculates rewards as totalStaked minus totalFrozen, resulting in zero.
  4. The staker receives no rewards.

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 a smart contract, rewards are calculated based on staked tokens minus frozen tokens. If frozen tokens equal staked tokens, the reward calculation gives zero. The function debtWriteOff updates the frozen amount before calculating rewards, so the user gets nothing. This is a bug because the order of operations is wrong.

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.

More by kenzo

View all

53 more disclosed reports, best first.

More from Union Finance contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.