Skip to content
Business logic Audit finding Intermediate

Wrong shortfall calculation

The settleAccount function in OptimisticLedger incorrectly updates the shortfall by adding the new shortfall twice, causing the total shortfall to double each time a shortfall is created. This could prevent users from withdrawing the correct amount of funds and require donations to resolve the inflated shortfall.

kenzo Follow

Originally disclosed on Code4rena

Submitted by kenzo

Every time an account is settled, if shortfall is created, due to a wrong calculation shortfall will double in size and add the new shortfall.

Impact

Loss of funds: users won’t be able to withdraw the correct amount of funds. Somebody would have to donate funds to resolve the wrong shortfall.

Proof of Concept

We can see in the settleAccount of OptimisticLedger that self.shortfall ends up being self.shortfall+self.shortfall+newShortfall: (Code ref)

function settleAccount(OptimisticLedger storage self, address account, Fixed18 amount)
internal returns (UFixed18 shortfall) {
    Fixed18 newBalance = Fixed18Lib.from(self.balances[account]).add(amount);

    if (newBalance.sign() == -1) {
        shortfall = self.shortfall.add(newBalance.abs());
        newBalance = Fixed18Lib.ZERO;
    }

    self.balances[account] = newBalance.abs();
    self.shortfall = self.shortfall.add(shortfall);
}

Additionally, you can add the following line to the “shortfall reverts if depleted” test in Collateral.test.js, line 190:

await collateral.connect(productSigner).settleAccount(userB.address, -50)

Previously the test product had 50 shortfall. Now we added 50 more, but the test will print that the actual shortfall is 150, and not 100 as it should be.

Recommended Mitigation Steps

Move the setting of self.shortfall to inside the if function and change the line to:

    self.shortfall = shortfall

kbrizzle (Perennial) confirmed:

Excellent find 🙏

Alex the Entreprenerd (judge) commented:

Agree with the finding shortfall = self.shortfall.add(newBalance.abs()); is already shortfal + newBalance.abs() So performing line 73 self.shortfall = self.shortfall.add(shortfall); is adding shortfall again

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

The shortfall is added to self.shortfall twice: once when calculating the shortfall and again when updating self.shortfall.

Impact

Users may be unable to withdraw the correct amount of funds, and someone would have to donate funds to resolve the inflated shortfall.

What the attacker needed

None

The fix

The shortfall should be set to the new shortfall value instead of adding it to the existing shortfall.

How it was found

  1. Add a line to the test to settle an account with a negative amount, creating a shortfall.
  2. Observe that the total shortfall becomes 150 instead of the expected 100.

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 an account is settled and a shortfall occurs, the code mistakenly adds the new shortfall to the total twice. This makes the recorded shortfall larger than it should be, so users might not be able to withdraw their full funds.

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 Perennial 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.