`Vault._amount_per_base_lp_share` should also consider bad debt when `safety_module_lp_total_amount` is not enough
The vault's share price calculation for base liquidity providers ignores bad debt when the safety module cannot cover it, so early withdrawers can take more than their fair share and later withdrawers get nothing. An attacker could withdraw their full liquidity before others, leaving remaining providers with losses.
Liquidity Providers have the choice to provide liquidity in the "Base LP" pool or the "Safety Module" pool. The Safety Module pool takes a first loss risk protecting the Base LP in exchange for a higher share of the trading and accruing interest.
However, if the safety module liquidity is insufficient, the base liquidity is also used to cover the bad debt. But Vault._amount_per_base_lp_share never take bad debt into consideration, leading to loss of funds.
If the amount of bad debt is more than safe module liquidity, the withdrawal of base liquidity becomes unfair. Early liquidity providers have the advantage of being able to withdraw the full amount, while other liquidity providers are unable to withdraw any liquidity, resulting in a loss of funds for them.
Introduced a new view function to account for when the bad debt surpasses the amount covered by safety_module. Missing
Diff
+ if self.bad_debt[_token] > self.safety_module_lp_total_amount[_token] + self.base_lp_total_amount[_token]:
+ return 0
from watsons recommendation though. Seems good to me but I would like @Unstoppable-DeFi to re-check if letting this check out was intended
Unstoppable-DeFi
Yes, intended.
The definition of bad debt is the amount of liquidity that was borrowed to open a position and not recovered when closing the position (offset by the traders provided margin).
Therefore the maximum amount of bad debt that could theoretically be accrued is the total amount of liquidity available (self.safety_module_lp_total_amount[_token] + self.base_lp_total_amount[_token]). There is no scenario where bad debt could be greater than this since closing a position cannot end in a negative return (i.e. is clamped to 0).
maarcweiss
Introduced a new view function to account for when the bad debt surpasses the amount covered by safety_module. Missing
Diff
+ if self.bad_debt[_token] > self.safety_module_lp_total_amount[_token] + self.base_lp_total_amount[_token]:
+ return 0
from watsons recommendation though. Seems good to me but I would like @Unstoppable-DeFi to re-check if letting this check out was intended
Following the sign-off, the previously explained snippet was not included because bad debt can't actually surpass:
self.safety_module_lp_total_amount[_token] + self.base_lp_total_amount[_token] therefore the case is not accounted for in the contracts
The original report as published on Sherlock, by GimelSec. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Generating the AI write-up for this report.
This usually takes about a minute. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Root cause
Likely the share price function for base liquidity providers uses the raw base pool total without subtracting bad debt that exceeds the safety module balance.
Impact
If bad debt exceeds safety module liquidity, base liquidity providers who withdraw later may be unable to withdraw any funds, resulting in a loss.
What the attacker needed
Bad debt exceeds safety module liquidity; user holds base LP shares
The fix
The share price calculation should account for bad debt that exceeds the safety module balance, as was done in a later pull request.
How it was found
Bad debt exceeds the safety module liquidity, so base liquidity must cover the remainder.
Alice calls withdraw_liquidity to withdraw her base pool tokens.
The share calculation ignores bad debt, so Alice receives the full amount.
Bob tries to withdraw but fails because available liquidity is now 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.
Generating the AI write-up for this report.
This usually takes about a minute. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
A vault holds money from two groups: a safety pool that absorbs losses first, and a base pool. If losses exceed the safety pool, the base pool should shrink, but the code still values base pool shares as if no loss occurred. This lets the first person to withdraw take out too much, leaving later people with less or nothing.
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.