Skip to content
Audit finding Intermediate

convertFromStorage() fails to use rounding-up when converting a negative storedCashBalance into signedPrimeSupplyValue

The convertFromStorage function in Notional's PrimeRateLib rounds down when converting a negative stored cash balance (debt) to signed prime supply value, causing users to owe slightly less than they should. This rounding error could accumulate or be exploited to drain small amounts from the protocol.

chaduke Follow

Originally disclosed on Sherlock

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

Found by

chaduke

Summary

convertFromStorage() fails to use rounding-up when converting a negative storedCashBalance into signedPrimeSupplyValue.

Vulnerability Detail

convertFromStorage() is used to convert storedCashBalance into signedPrimeSupplyValue. When storedCashBalance is negative, it represents a debt - positive prime cash owed.

https://github.com/sherlock-audit/2023-03-notional/blob/main/contracts-v2/contracts/internal/pCash/PrimeRateLib.sol#L60-L74

Unfortunately, when converting a negative storedCashBalance into signedPrimeSupplyValue, the following division will apply a rounding-down (near zero) mode, leading to a user to owe less than it is supposed to be.

JavaScript

return storedCashBalance.mul(pr.debtFactor).div(pr.supplyFactor);

This is not acceptable. Typically, rounding should be in favor of the protocol, not in favor of the user to prevent draining of the protocol and losing funds of the protocol.

The following POC shows a rounding-down will happen for a negative value division. The result of the following test is -3.

JavaScript

function testMod() public {
         
         int256 result = -14;
         result = result / 4;
         console2.logInt(result);
    }


Impact

convertFromStorage() fails to use rounding-up when converting a negative storedCashBalance into signedPrimeSupplyValue. The protocol is losing some dusts amount, but it can be accumulative or a vulnerability that can be exploited.

Code Snippet

Tool used

VSCode

Manual Review

Recommendation

Use rounding-up instead.

Diff
function convertFromStorage(
        PrimeRate memory pr,
        int256 storedCashBalance
    ) internal pure returns (int256 signedPrimeSupplyValue) {
        if (storedCashBalance >= 0) {
            return storedCashBalance;
        } else {
            // Convert negative stored cash balance to signed prime supply value
            // signedPrimeSupply = (negativePrimeDebt * debtFactor) / supplyFactor

            // cashBalance is stored as int88, debt factor is uint80 * uint80 so there
            // is no chance of phantom overflow (88 + 80 + 80 = 248) on mul
-           return storedCashBalance.mul(pr.debtFactor).div(pr.supplyFactor);
+         return (storedCashBalance.mul(pr.debtFactor).sub(pr.supplyFactor-1)).div(pr.supplyFactor);
        }
    }

The original report as published on Sherlock, by chaduke. 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

Likely, the division operation in convertFromStorage rounds towards zero for negative numbers, which favours the user instead of the protocol.

Impact

The protocol loses small amounts (dust) that could accumulate or be exploited, but the text does not claim a specific exploit or quantify the loss.

What the attacker needed

None

The fix

The code should be changed to round up when converting negative balances, for example by adding (supplyFactor - 1) before dividing.

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 smart contract converts between different units of value, it sometimes has to round a number. Rounding down means the result is slightly smaller. Here, the code rounds down when calculating how much a user owes, so the user ends up owing a tiny bit less than they should. Over many transactions, this could add up and cost the protocol money.

More by chaduke

View all

21 more disclosed reports, best first.

More from Notional Update #4

View all

The newest disclosed reports for this program.

Similar reports

Same bug class and techniques, clearest first.