getAccountPrimeDebtBalance() always return 0
The getAccountPrimeDebtBalance function always returns zero because it assigns the uninitialised debtBalance variable instead of cashBalance when the balance is negative. An attacker or integrator relying on this view would incorrectly see no debt, potentially causing third-party systems to mishandle accounts.
Source: https://github.com/sherlock-audit/2023-03-notional-judging/issues/173
Found by
bin2chen
Summary
Spelling errors that result in getAccountPrimeDebtBalance() Always return 0
Vulnerability Detail
getAccountPrimeDebtBalance() use for Show current debt
function getAccountPrimeDebtBalance(uint16 currencyId, address account) external view override returns (
int256 debtBalance
) {
mapping(address => mapping(uint256 => BalanceStorage)) storage store = LibStorage.getBalanceStorage();
BalanceStorage storage balanceStorage = store[account][currencyId];
int256 cashBalance = balanceStorage.cashBalance;
// Only return cash balances less than zero
debtBalance = cashBalance < 0 ? debtBalance : 0; //<------@audit wrong, Always return 0
}
In the above code we can see that due to a spelling error, debtBalance always ==0
should use debtBalance = cashBalance < 0 ? cashBalance : 0;
Impact
getAccountPrimeDebtBalance() is the external method to check the debt
If a third party integrates with notional protocol, this method will be used to determine whether the user has debt or not and handle it accordingly, which may lead to serious errors in the third party's business
Code Snippet
Tool used
Manual Review
Recommendation
function getAccountPrimeDebtBalance(uint16 currencyId, address account) external view override returns (
int256 debtBalance
) {
mapping(address => mapping(uint256 => BalanceStorage)) storage store = LibStorage.getBalanceStorage();
BalanceStorage storage balanceStorage = store[account][currencyId];
int256 cashBalance = balanceStorage.cashBalance;
// Only return cash balances less than zero
- debtBalance = cashBalance < 0 ? debtBalance : 0;
+ debtBalance = cashBalance < 0 ? cashBalance : 0;
}
Discussion
jeffywu
Fixed in: https://github.com/notional-finance/contracts-v2/pull/134
xiaoming9090
@0xleastwood + @xiaoming9090 : Verified. Fixed in PR https://github.com/notional-finance/contracts-v2/pull/134
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 ternary expression assigns debtBalance to itself instead of assigning cashBalance, so the return value is always zero.
Impact
Third-party integrations that use this method to check debt may make serious business errors because the method always reports zero debt.
What the attacker needed
None
The fix
The code was fixed by assigning cashBalance instead of debtBalance in the ternary expression.
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.
A smart contract function that is supposed to report how much a user owes always says zero. This happens because the code accidentally uses the wrong variable name when setting the result. Anyone building on top of this contract would get wrong information about debt.
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.