Submitted by nnez, also found by Eeyore, pkqs90, and zhaojohnson
Liquidators can profit from protocol insolvency
At the end of liquidation process, in the case of no remaining position, liquidate function checks for remaining margin of the liquidating vault. If there is positive remaining amount, it proceeds to send those margin back to vaults recipient.
On contrary, if there is negative remaining amount, liquidator must pay (compensate) for those negative amounts.
This mechanism exists to protect against protocol insolvency in the case that vaults margin cannot cover the loss from vaults position.
Liquidators close vaults position, settle the trade and profit from slippage tolerance, set by the pools owner. For instance, if the slippage tolerance is set at 5% and lets say liquidator is closing this long position:
Liquidator will get 1 ETH to sell for 2,500 USDC and have to return only 2500 * 0.95 = 2,375 USDC. The remaining short USDC will be deducted from vaults margin, in this case, 3000 - 2375 = 625 USDC.
Considering this mechanism, liquidators will always profit from the slippage.
If we take a look at the snippet of the code responsible for the aforementioned protection mechanism.
LiquidationLogic.sol#L89-L108:
The liquidation function only checks for negative margin if and only if the position is fully closed from liquidation. Therefore, if the position is not fully closed (99.99% closed) and left with negative margin, liquidators dont have to compensate for the loss.
Considering the case where liquidation of a full position would result in negative margin, liquidators should only be incentivized to partially close a position to the point that vaults margin becomes zero because that would be their maximum profit.
However, with the logic flaw mentioned, liquidators can increase their profit by closing nearly full position (99.99% closed), effectively leaving the loss to the protocol.
This test demonstrates that liquidating nearly full position can bypass the negative margin check and leave the loss to the protocol.
If the remaining margin in the vault is equal or less than zero, then there is no need to check whether there is still an open position; because it is already effectively closed (no margin left). Therefore, a check for negative margin should be moved out from if(!hasPosition) block.
Value leaks from protocol (insolvency) but it only happens in certain situations, i.e., price drops sharply.
syuhei176 (Predy) confirmed via duplicate Issue #9
0xsomeone (judge) increased severity to High and commented:
