Liquidation incorrectly tries to transfer token from Market instead of liquidator if remainingMargin is negative
During liquidation, if the vault has negative margin (bad debt), the code attempts to transfer the bad debt amount from the Market contract instead of the liquidator. This makes liquidation impossible for such vaults, and the bad debt continues to grow due to accumulating lending fees.
The liquidator would call a Market contract, which triggers the liquidation logic in PredyPool. The tokens would then be swapped accordingly using the SettlementCallbackLib.
The issue is during the end of liquidation, if the position is entirely wiped out (e.g. hasPosition == false in the following code), the code checks whether there is still margin left in the vault.
If there is still margin left, the remaining margin would be sent to the vault.recipient address.
If there is bad debt (negative margin value), the liquidator must pay for this.
For case 2, the code tries to transfer token from msg.sender to PredyPool. However, the msg.sender is the Market protocol, and not the liquidator. This means liquidating a vault with negative margin is impossible, and this bad debt will never be cleared. What’s worse, the lending fees would still accumulate for this vault, and the bad debt keeps getting larger.
function liquidate(
uint256 vaultId,
uint256 closeRatio,
GlobalDataLibrary.GlobalData storage globalData,
bytes memory settlementData
) external returns (IPredyPool.TradeResult memory tradeResult) {
...
if (!hasPosition) {
int256 remainingMargin = vault.margin;
if (remainingMargin > 0) {
if (vault.recipient != address(0)) {
// Send the remaining margin to the recipient.
vault.margin = 0;
sentMarginAmount = uint256(remainingMargin);
ERC20(pairStatus.quotePool.token).safeTransfer(vault.recipient, sentMarginAmount);
}
} else if (remainingMargin < 0) {
vault.margin = 0;
> // To prevent the liquidator from unfairly profiting through arbitrage trades in the AMM and passing losses onto the protocol,
> // any losses that cannot be covered by the vault must be compensated by the liquidator
> ERC20(pairStatus.quotePool.token).safeTransferFrom(msg.sender, address(this), uint256(-remainingMargin));
}
}
...
}
Recommended Mitigation Steps
Transfer quoteTokens from the liquidator instead of the Market protocol.
The code outlined represents the intended design as the caller’s funds (i.e., liquidators) must be used, and the markets are responsible for acquiring the relevant funds from their respective callers.
PredyPool callsback PerpMarket SettlementCallbackLib to handle token swap. Take SettlementCallbackLib sell() as an example. The sender here is the user’s address, instead of the PerpMarket’s. Tokens are swapped between user and PredyPool:
if (settlementParams.contractAddress == address(0)) {
// direct fill
uint256 quoteAmount = sellAmount * price / Constants.Q96;
predyPool.take(false, sender, sellAmount);
ERC20(quoteToken).safeTransferFrom(sender, address(predyPool), quoteAmount);
return;
}
If the liquidation leads to negative margin, someone should pay the bad debt.
else if (remainingMargin < 0) {
vault.margin = 0;
> // To prevent the liquidator from unfairly profiting through arbitrage trades in the AMM and passing losses onto the protocol,
> // any losses that cannot be covered by the vault must be compensated by the liquidator
> ERC20(pairStatus.quotePool.token).safeTransferFrom(msg.sender, address(this), uint256(-remainingMargin));
}
In step 5, it transfers tokens from PerpMarket instead of from the user. However, PerpMarket doesn’t have token allowance to PredyPool, so the transfer would always fail. Secondly, I did not find documentation saying markets are responsible acquiring the relevant funds from their respective callers.
So maybe I’m missing something here. I also did not find any unit tests related to such scenario.
I missed this report. As @pkqs90 mentioned, regarding non-performing loans, they are compensated from the market contract within the liquidate function’s safeTransferFrom. The processing on the market contract side has not yet been implemented. This should have been pointed out beginning the audit.
This issue is the liquidationCall version of Issue #26. Therefore, this report is valid.
I agree with the facts shared here, and this exhibit will be re-opened as a valid high-risk submission. To note, I had mentioned that rulings might change after PJQA and Sponsor input for all exhibits that were marked as unsatisfactory during the validation round and would be revisited after appropriate feedback has been gathered.
@0xsomeone - I agree there is an issue in the BaseMarket contract and all the contracts that inherit from it, but it does not affect the ability to liquidate any position.
Every position can be liquidated directly by calling PreayPool.execLiquidationCall() with the same parameters as in Base.execLiquidationCall() there is no blocker for liquidators to build its own solution to support IHooks.predySettlementCallback(), and the position will be liquidated even when the vault position is negative.
Therefore, I disagree with categorizing this issue as High.
I will maintain the high severity rating for this submission due to simply pointing out an egregious error in the code which, per relevant SC rulings, falls under the definition of a “high-risk” vulnerability.
While direct interaction may be possible, that never was the intended use of the markets and their being effectively inoperable in the regard described by the exhibit is a significant flaw.
The original report as published on Code4rena, by pkqs90. 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.
Generating the AI write-up for this report.
This usually takes about 30 seconds. 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 liquidation logic uses `msg.sender` (the Market contract) as the source of funds for covering negative margin, instead of the original liquidator's address.
Impact
Liquidating a vault with negative margin is impossible, so bad debt will never be cleared and will keep growing due to accumulating lending fees.
What the attacker needed
Vault has negative margin after liquidation
The fix
The code should transfer the bad debt amount from the liquidator's address instead of the Market contract.
How it was found
Liquidator calls PerpMarket's execLiquidationCall() to start a liquidation.
PredyPool calls back PerpMarket's SettlementCallbackLib to handle token swap.
After swap, PredyPool checks the swap price is in a valid range.
If the liquidation leads to negative margin, the code attempts to transfer the bad debt from msg.sender (the Market) instead of the liquidator.
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 30 seconds. 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
When a borrower's collateral is worth less than their debt, a liquidator can step in to close the position. If the debt is still not fully covered, the liquidator is supposed to pay the shortfall. But here, the code mistakenly tries to take the money from the wrong account (the market contract), so the payment always fails and the bad debt can never be cleared.
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.