Skip to content
Business logic Audit finding Intermediate

repayAccountPrimeDebtAtSettlement() user lost residual cash

The function repayAccountPrimeDebtAtSettlement() calculated the refund amount using a variable that had just been set equal to the debt value, so the refund was always zero. As a result, users lost any residual cash that should have been returned when settling a vault account.

bin2chen Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-03-notional-judging/issues/172

Found by

bin2chen, xiaoming90 + 0xleastwood

Summary

in repayAccountPrimeDebtAtSettlement() Incorrect calculation of primeCashRefund value (always == 0) Resulting in the loss of the user's residual cash

Vulnerability Detail

when settle Vault Account will execute settleVaultAccount()->repayAccountPrimeDebtAtSettlement() In the repayAccountPrimeDebtAtSettlement() method the residual amount will be refunded to the user The code is as follows.

Solidity
    function repayAccountPrimeDebtAtSettlement(
        PrimeRate memory pr,
        VaultStateStorage storage primeVaultState,
        uint16 currencyId,
        address vault,
        address account,
        int256 accountPrimeCash,
        int256 accountPrimeStorageValue
    ) internal returns (int256 finalPrimeDebtStorageValue, bool didTransfer) {
...

            if (netPrimeDebtRepaid < accountPrimeStorageValue) {
                // If the net debt change is greater than the debt held by the account, then only
                // decrease the total prime debt by what is held by the account. The residual amount
                // will be refunded to the account via a direct transfer.
                netPrimeDebtChange = accountPrimeStorageValue;
                finalPrimeDebtStorageValue = 0;

                int256 primeCashRefund = pr.convertFromUnderlying(
                    pr.convertDebtStorageToUnderlying(netPrimeDebtChange.sub(accountPrimeStorageValue)) //<--------@audit always ==0
                );
                TokenHandler.withdrawPrimeCash(
                    account, currencyId, primeCashRefund, pr, false // ETH will be transferred natively
                );
                didTransfer = true;
            } else {

From the above code we can see that there is a spelling error

  1. netPrimeDebtChange = accountPrimeStorageValue;
  2. primeCashRefund = netPrimeDebtChange.sub(accountPrimeStorageValue)

so primeCashRefund always ==0

should be primeCashRefund = netPrimeDebtRepaid - accountPrimeStorageValue

Impact

primeCashRefund always == 0 , user lost residual cash

Code Snippet

https://github.com/sherlock-audit/2023-03-notional/blob/main/contracts-v2/contracts/internal/vaults/VaultAccount.sol#L575

Tool used

Manual Review

Recommendation

Solidity
    function repayAccountPrimeDebtAtSettlement(
        PrimeRate memory pr,
        VaultStateStorage storage primeVaultState,
        uint16 currencyId,
        address vault,
        address account,
        int256 accountPrimeCash,
        int256 accountPrimeStorageValue
    ) internal returns (int256 finalPrimeDebtStorageValue, bool didTransfer) {
...

            if (netPrimeDebtRepaid < accountPrimeStorageValue) {
                // If the net debt change is greater than the debt held by the account, then only
                // decrease the total prime debt by what is held by the account. The residual amount
                // will be refunded to the account via a direct transfer.
                netPrimeDebtChange = accountPrimeStorageValue;
                finalPrimeDebtStorageValue = 0;

                int256 primeCashRefund = pr.convertFromUnderlying(
-                   pr.convertDebtStorageToUnderlying(netPrimeDebtChange.sub(accountPrimeStorageValue))
+                   pr.convertDebtStorageToUnderlying(netPrimeDebtRepaid.sub(accountPrimeStorageValue)) 
                );
                TokenHandler.withdrawPrimeCash(
                    account, currencyId, primeCashRefund, pr, false // ETH will be transferred natively
                );
                didTransfer = true;
            } else {

Discussion

jeffywu

Fixed: https://github.com/notional-finance/contracts-v2/pull/135

xiaoming9090

@0xleastwood + @xiaoming9090 : Verified. Fixed in PR https://github.com/notional-finance/contracts-v2/pull/135

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

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 refund calculation subtracted accountPrimeStorageValue from netPrimeDebtChange, but netPrimeDebtChange had just been set to accountPrimeStorageValue, so the result was always zero; it should have used netPrimeDebtRepaid instead.

Impact

Users permanently lose the residual cash that should have been refunded when their vault account is settled.

What the attacker needed

Settling a vault account with residual cash

The fix

The calculation was fixed to use netPrimeDebtRepaid instead of netPrimeDebtChange when computing the refund.

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 a vault account is settled, the code is supposed to return any extra money the user has after paying off debt. A mistake in the calculation made that refund always zero, so the user never got their leftover cash back.

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 bin2chen

View all

139 more disclosed reports, best first.

More from Notional Update #4

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.