repayDebt in Vault.sol could DOS functionality for markets
The repayDebt function allows any user to repay any borrower's debt, setting that borrower's debt to zero and reducing totalDebt. If a malicious user repays all borrowers' debts, totalDebt becomes zero, and later attempts to decrease it during borrowing cause an underflow, blocking borrowing functionality.
Submitted by p4st13r4
Any user can pay the debt for any borrower in Vault.sol, by using repayDebt(). This function allows anyone to repay any amount of borrowed value, up-to and including the totalDebt value; it works by setting the debts[_target] to zero, and decreasing totalDebt by the given amount, up to zero. However, all debts of the other borrowers are left untouched.
If a malicious (but generous) user were to repay the debt for all the borrowers, markets functionality regarding borrowing would be DOSed: the vault would try to decrease the debt of the market, successfully, but would fail to decrease totalDebt as it would result in an underflow
Proof of Concept
https://github.com/code-423n4/2022-01-insure/blob/main/contracts/Vault.sol#L257
Recommended Mitigation Steps
Make repayDebt() accept an amount up-to and including the value of the debt for the given borrower
oishun1112 (Insure) confirmed:
this needs to be specified how in more detail.
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 repayDebt function decreases totalDebt without ensuring it does not underflow when totalDebt is already zero, and it allows repaying more than the individual borrower's debt.
Impact
Borrowing functionality for all markets could be blocked because totalDebt underflows when trying to decrease it after all debts are repaid.
What the attacker needed
Any user can call repayDebt; sufficient funds to repay all debts
The fix
The repayDebt function should only accept an amount up to and including the borrower's debt, preventing totalDebt from being reduced below 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.
In a lending system, a vault tracks how much each person owes and the total owed by everyone. The repay function lets anyone pay off someone else's debt, but it reduces the total owed without checking if that total is already zero. If all debts are paid off, the total becomes zero, and the next time someone tries to borrow, the system tries to subtract from zero and crashes, stopping all borrowing.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.