It's possible to reset primaryCredit and secondaryCredit for insurance account
The handleBadDebt function can be called on the insurance account when its primaryCredit is negative, resetting both primaryCredit and secondaryCredit to zero. This breaks the insurance accounting and could disrupt the protocol's loss coverage.
GalloDaSballo Follow with p0wd3r and rvierdiiev
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-04-jojo-judging/issues/159
Found by
GalloDaSballo, p0wd3r, rvierdiiev
Summary
When because of negative credit after liquidations of another accounts, insurance address doesn't pass isSafe check, then malicious user can call JOJOExternal.handleBadDebt and reset both primaryCredit and secondaryCredit for insurance account.
Vulnerability Detail
insurance account is handled by JOJO team. Team is responsible to top up this account in order to cover losses. When bad debt is handled, then its negative credit value is added to the insurance account. Because of that it's possible that primaryCredit of insurance account is negative and Liquidation._isSafe(state, insurance) == false.
Anyone can call JOJOExternal.handleBadDebt function.
https://github.com/sherlock-audit/2023-04-jojo/blob/main/smart-contract-EVM/contracts/lib/Liquidation.sol#L399-L418
function handleBadDebt(Types.State storage state, address liquidatedTrader)
external
{
if (
state.openPositions[liquidatedTrader].length == 0 &&
!Liquidation._isSafe(state, liquidatedTrader)
) {
int256 primaryCredit = state.primaryCredit[liquidatedTrader];
uint256 secondaryCredit = state.secondaryCredit[liquidatedTrader];
state.primaryCredit[state.insurance] += primaryCredit;
state.secondaryCredit[state.insurance] += secondaryCredit;
state.primaryCredit[liquidatedTrader] = 0;
state.secondaryCredit[liquidatedTrader] = 0;
emit HandleBadDebt(
liquidatedTrader,
primaryCredit,
secondaryCredit
);
}
}
So it's possible for anyone to call handleBadDebt for insurance address, once its primaryCredit is negative and Liquidation._isSafe(state, insurance) == false. This will reset both primaryCredit and secondaryCredit variables to 0 and break insurance calculations.
Impact
Insurance primaryCredit and secondaryCredit variables are reset.
Code Snippet
Provided above
Tool used
Manual Review
Recommendation
Do not allow handleBadDebt call with insurance address.
Discussion
JoscelynFarr
fix link: https://github.com/JOJOexchange/smart-contract-EVM/commit/78c53b4721ae7bb97fb922f78342d0ee4a1825dd
IAm0x52
Fix looks good. Since the order has been changed, clearing bad debt on the insurance account will result it in still having the same debt before and after the call
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 handleBadDebt function does not check whether the liquidatedTrader is the insurance account, allowing it to be called on the insurance address and reset its credit balances.
Impact
The insurance account's primaryCredit and secondaryCredit are reset to zero, which breaks insurance calculations and could prevent the insurance fund from covering losses.
What the attacker needed
Insurance account has negative primaryCredit; insurance account fails isSafe check
The fix
The fix was to change the order of operations so that clearing bad debt on the insurance account leaves its debt unchanged.
How it was found
- Cause the insurance account's primaryCredit to become negative (e.g., through bad debt from liquidations).
- Call JOJOExternal.handleBadDebt with the insurance address as the liquidatedTrader.
- The function resets primaryCredit and secondaryCredit for the insurance account to zero.
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.
The insurance account is a special account the team uses to cover losses. When it has a negative balance, anyone can call a function that is meant to clear bad debt from a trader, but it also works on the insurance account and wipes its credit values to zero. This messes up the accounting for the insurance fund.
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.