Debt is not updated when removing margin from a position
The total debt shares are not updated before checking if a position is liquidable when removing margin, so a trader can remove collateral from a position that should be liquidable. This allows a trader to withdraw margin from an unhealthy position.
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-06-unstoppable-judging/issues/9
Found by
stopthecap
Summary
Debt is not updated when removing margin from a position
Vulnerability Detail
Traders are allowed to remove margin/collateral from their positions: https://github.com/sherlock-audit/2023-06-unstoppable/blob/94a68e49971bc6942c75da76720f7170d46c0150/unstoppable-dex-audit/contracts/margin-dex/Vault.vy#L528-L546
as you can see, the position can only have collateral removed if it is not liquidable: https://github.com/sherlock-audit/2023-06-unstoppable/blob/94a68e49971bc6942c75da76720f7170d46c0150/unstoppable-dex-audit/contracts/margin-dex/Vault.vy#L487
The problem is that the total total_debt_shares[_debt_token] is not updated when checking whether the positon is liquidable neither when calling remove_margin You can see on the following links the progression of the function calls to calculate whether a position is liquidable or not:
As seen, total_debt_shares[_debt_token] is not updated to the current total_debt_shares[_debt_token] by calling _update_debt and therefore a stale total_debt_shares[_debt_token] is used to calculate whether a position is liquidable. Allowing positions that are in fact liquidable remove margin because the debt is not updated.
Impact
Not updating the debt before removing collateral (margin) from your position does allow a trader to remove collateral from a liquidable position
Code Snippet
Tool used
Manual Review
Recommendation
Call _update_debt at the beginning of the remove margin function
Discussion
Unstoppable-DeFi
https://github.com/Unstoppable-DeFi/unstoppable-dex-audit/pull/1
maarcweiss
Fixed by calling update debt when removing margin from positions
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 function that checks liquidability uses a stale total debt shares value because it does not call the debt update function before the check.
Impact
A trader could remove collateral from a position that is in fact liquidable, potentially leaving the system with bad debt.
What the attacker needed
A margin position; the position is actually liquidable
The fix
The issue was fixed by calling the debt update function at the beginning of the remove margin function.
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 or margin system, a position becomes 'liquidable' when its debt is too high relative to its collateral. The contract checks this before allowing collateral removal, but it uses an outdated debt total, so the check can wrongly pass. This lets a trader take out collateral even though the position is actually unsafe.
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.