Inaccurate settlement reserve accounting
A signed/unsigned conversion error in the settlement reserve accounting logic caused an event not to be emitted when fCash debt was held and prime cash was non-positive, leading to inaccurate off-chain accounting. This could cause users relying on that accounting to make ill-informed financial decisions.
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-03-notional-judging/issues/196
Found by
xiaoming90 + 0xleastwood
Summary
The off-chain accounting of fCash debt or prime cash in the settlement reserve will be inaccurate due to an error when handling the conversion between signed and unsigned integers.
Vulnerability Detail
Events will be emitted to reconcile off-chain accounting for the edge condition when leveraged vaults lend at zero interest. This event will be emitted if there is fCash debt or prime cash in the settlement reserve.
In an event where s.fCashDebtHeldInSettlementReserve > 0 and s.primeCashHeldInSettlementReserve <= 0, no event will be emitted. As a result, the off-chain accounting of fCash debt or prime cash in the settlement reserve will be off.
The reason is that since fCashDebtInReserve is the negation of s.fCashDebtHeldInSettlementReserve, which is an unsigned integer, fCashDebtInReserve will always be less than or equal to 0. Therefore, fCashDebtInReserve > 0 will always be false and is an unsatisfiable condition.
File: PrimeRateLib.sol
457: // This is purely done to fully reconcile off chain accounting with the edge condition where
458: // leveraged vaults lend at zero interest.
459: int256 fCashDebtInReserve = -int256(s.fCashDebtHeldInSettlementReserve);
460: int256 primeCashInReserve = int256(s.primeCashHeldInSettlementReserve);
461: if (fCashDebtInReserve > 0 || primeCashInReserve > 0) {
462: int256 settledPrimeCash = convertFromUnderlying(settlementRate, fCashDebtInReserve);
463: int256 excessCash;
464: if (primeCashInReserve > settledPrimeCash) {
465: excessCash = primeCashInReserve - settledPrimeCash;
466: BalanceHandler.incrementFeeToReserve(currencyId, excessCash);
467: }
468:
469: Emitter.emitSettlefCashDebtInReserve(
470: currencyId, maturity, fCashDebtInReserve, settledPrimeCash, excessCash
471: );
472: }
Impact
The off-chain accounting of fCash debt or prime cash in the settlement reserve will be inaccurate. Users who rely on inaccurate accounting information to conduct any form of financial transaction will expose themselves to unintended financial risks and make ill-informed decisions.
Code Snippet
Tool used
Manual Review
Recommendation
It is recommended to implement the following fix:
- int256 fCashDebtInReserve = -int256(s.fCashDebtHeldInSettlementReserve);
+ int256 fCashDebtInReserve = int256(s.fCashDebtHeldInSettlementReserve);
Discussion
jeffywu
Fixed: https://github.com/notional-finance/contracts-v2/pull/139
xiaoming9090
@0xleastwood + @xiaoming9090 : Verified. Fixed in PR https://github.com/notional-finance/contracts-v2/pull/139
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 code negated an unsigned integer before converting it to a signed integer, making the resulting value always non-positive and the condition that checks for a positive value unsatisfiable.
Impact
The off-chain accounting of fCash debt or prime cash in the settlement reserve will be inaccurate, which could lead users relying on that information to make ill-informed financial decisions and expose themselves to unintended financial risks.
What the attacker needed
Leveraged vaults lend at zero interest; fCash debt held in settlement reserve > 0; prime cash held in settlement reserve <= 0
The fix
The fix was to remove the negation when converting the unsigned integer to a signed integer, and it was verified as fixed in a pull request.
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.
Smart contracts sometimes need to track debts and reserves. This code mistakenly converted a positive debt number into a negative one, so a check that should have triggered an event never did. That meant off-chain records could be wrong, and people using those records might make bad financial choices.
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.