Secondary debt dust balances are not truncated
The smart contract truncates tiny 'dust' balances in primary debt to zero but fails to do the same for secondary debt. This inconsistency could cause discrepancies in debt accounting and accumulation of dust, with unforeseen consequences.
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-03-notional-judging/issues/210
Found by
xiaoming90 + 0xleastwood
Summary
Dust balances in primary debt are truncated toward zero. However, this truncation was not performed against secondary debts.
Vulnerability Detail
File: VaultAccount.sol
212: function updateAccountDebt(
..SNIP..
230: // Truncate dust balances towards zero
231: if (0 < vaultState.totalDebtUnderlying && vaultState.totalDebtUnderlying < 10) vaultState.totalDebtUnderlying = 0;
..SNIP..
233: }
vaultState.totalDebtUnderlying is primarily used to track the total debt of primary currency. Within the updateAccountDebt function, any dust balance in the vaultState.totalDebtUnderlying is truncated towards zero at the end of the function as shown above.
File: VaultSecondaryBorrow.sol
304: function _updateTotalSecondaryDebt(
305: VaultConfig memory vaultConfig,
306: address account,
307: uint16 currencyId,
308: uint256 maturity,
309: int256 netUnderlyingDebt,
310: PrimeRate memory pr
311: ) private {
312: VaultStateStorage storage balance = LibStorage.getVaultSecondaryBorrow()
313: [vaultConfig.vault][maturity][currencyId];
314: int256 totalDebtUnderlying = VaultStateLib.readDebtStorageToUnderlying(pr, maturity, balance.totalDebt);
315:
316: // Set the new debt underlying to storage
317: totalDebtUnderlying = totalDebtUnderlying.add(netUnderlyingDebt);
318: VaultStateLib.setTotalDebtStorage(
319: balance, pr, vaultConfig, currencyId, maturity, totalDebtUnderlying, false // not settled
320: );
However, this approach was not consistently applied when handling dust balance in secondary debt within the _updateTotalSecondaryDebt function. Within the _updateTotalSecondaryDebt function, the dust balance in secondary debts is not truncated.
Impact
The inconsistency in handling dust balances in primary and secondary debt could potentially lead to discrepancies in debt accounting within the protocol, accumulation of dust, and result in unforeseen consequences.
Code Snippet
Tool used
Manual Review
Recommendation
Consider truncating dust balance in secondary debt within the _updateTotalSecondaryDebt function similar to what has been done for primary debt.
Discussion
jeffywu
Valid, medium severity looks good
jeffywu
Fixed in: https://github.com/notional-finance/contracts-v2/pull/137
xiaoming9090
@0xleastwood + @xiaoming9090 : Understood from the team that the truncation of dust is no longer necessary. Thus, they have been removed from the codebase. Update made in PR https://github.com/notional-finance/contracts-v2/pull/137
jacksanford1
Sherlock note: Classifying this as fixed.
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 secondary debt update function omitted the dust truncation logic that was present in the primary debt update function.
Impact
The inconsistency could potentially lead to discrepancies in debt accounting within the protocol, accumulation of dust, and result in unforeseen consequences.
What the attacker needed
None
The fix
The team removed the dust truncation logic entirely, as it was no longer necessary, and the issue was fixed.
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 lending protocol, very small leftover amounts (called 'dust') are sometimes rounded down to zero to keep accounting clean. The code did this for one type of debt but forgot to do it for another, which could make the totals slightly wrong over time.