Skip to content
Business logic Audit finding Advanced

Quote that have already been liquidated can be liquidated again in some cases

The liquidation of PartyB and the removal of their open positions are separate steps, so if the second step is not called, the positions remain open. Later, when PartyA is liquidated, these already-liquidated positions are still included in the profit and loss calculation, which can divert funds to the already-liquidated PartyB and cause a loss to other PartyBs.

nobody2018 Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-06-symmetrical-judging/issues/206

Found by

nobody2018

Summary

LiquidationFacetImpl.liquidatePartyB is used to liquidate PartyB, which directly adds partyBAllocatedBalances[partyB][partyA] to allocatedBalances[partyA] [here](https://github.com/sherlock-audit/2023-06-symmetrical/blob/main/symmio-core/contracts/facets/liquidation/LiquidationFacetImpl.sol#L294-L296). In this way, the liquidation against PartyB has been completed. The main purpose of LiquidationFacetImpl.liquidatePositionsPartyB is to remove the [OpenPositions](https://github.com/sherlock-audit/2023-06-symmetrical/blob/main/symmio-core/contracts/facets/liquidation/LiquidationFacetImpl.sol#L369) of both parties. If this function is not called, the OpenPosition of both parties will always exist. The reasons for not calling it maybe:

  1. [partyBPositionLiquidatorsShare](https://github.com/sherlock-audit/2023-06-symmetrical/blob/main/symmio-core/contracts/facets/liquidation/LiquidationFacetImpl.sol#L373) is 0, there is no incentive for liquidator.
  2. Liquidator (bot) is not working due to downtime/network issues.
  3. Intentionally not called.
  4. tx is deferred processing (pending in mempool), resulting in [here](https://github.com/sherlock-audit/2023-06-symmetrical/blob/main/symmio-core/contracts/facets/liquidation/LiquidationFacetImpl.sol#L318-L322) revert.

Afterwards, if PartyA is liquidated due to quote with other PartyB, LiquidationFacetImpl.liquidatePositionsPartyA will process the quotes with the original PartyB. These quotes should have been removed.

Vulnerability Detail

For simplicity, the numbers mentioned below will not be exact values, it is just to describe the issue. Suppose the following scenario:

There are 3 users: PartyA(A1), PartyB(B1), PartyB(B2). The symbol is ETH/USDT.

allocatedBalances[A1]=1000

partyBAllocatedBalances[B1][A1]=1000

partyBAllocatedBalances[B2][A1]=1000

  1. A1 created a short quote1 via PartyAFacet.sendQuote. pendingLockedBalances[A1]=500. The current symbol price is 100.
  2. B1 opens quote1 via PartyBFacet.lockAndOpenQuote. lockedBalances[A1]=500, partyBLockedBalances[B1][A1]=500. [LibQuote.addToOpenPositions](https://github.com/sherlock-audit/2023-06-symmetrical/blob/main/symmio-core/contracts/facets/PartyB/PartyBFacetImpl.sol#L253) will add quote1 to the OpenPositions array of both parties. The status of quote1 is QuoteStatus.OPENED.
  3. As time goes by, the price dumps to 50. B1 can be liquidated.
  4. The liquidator initiates liquidation against B1 via LiquidationFacet.liquidatePartyB. allocatedBalances[A1]=1000+partyBAllocatedBalances[B1][A1]=2000, partyBAllocatedBalances[B1][A1]=0.
  5. liquidatePositionsPartyB is not called, so the OpenPositions array of both parties still contains quote1, and its status is still QuoteStatus.OPENED.
  6. A1 thinks that the price will continue to dump, and creates a short quote2. pendingLockedBalances[A1]=500. The current price is 50. Because allocatedBalances[A1]=2000 at this time, when the price pump to 200, A1 will be liquidated.
  7. B2 opens quote2. lockedBalances[A1]=500, partyBLockedBalances[B2][A1]=500. The OpenPositions array of A1 contains quote1 and quote2. The OpenPositions array of B2 only has quote2.
  8. As time goes by, the price pumps to 200. A1 can be liquidated.
  9. The liquidator initiates liquidation of A1. This process requires 3 calls: LiquidationFacet.liquidatePartyA/setSymbolsPrice/liquidatePositionsPartyA. The [priceSig](https://github.com/sherlock-audit/2023-06-symmetrical/blob/main/symmio-core/contracts/facets/liquidation/LiquidationFacet.sol#L28) of setSymbolsPrice is calculated off-chain. From the flow of the [uPnlPartyA](https://github.com/sherlock-audit/2023-06-symmetrical/blob/main/symmio-core/muon/crypto_v3.js#L238-L267) function in crypto_v3.js, all quotes in the OpenPositions array of A1 will be calculated into upnl. Because quote1 was opened when the price was 100, and the current price is 200, so B1 is profitable. This means that B1 that has been liquidated can also be allocated part of allocatedBalances[A1]. This is obviously unreasonable. This is a loss of part of the funds for B2.

Impact

Quotes that have already been liquidated can be liquidated again in some cases.

Code Snippet

https://github.com/sherlock-audit/2023-06-symmetrical/blob/main/symmio-core/contracts/facets/liquidation/LiquidationFacetImpl.sol#L294-L301

https://github.com/sherlock-audit/2023-06-symmetrical/blob/main/symmio-core/muon/crypto_v3.js#L238-L267

https://github.com/sherlock-audit/2023-06-symmetrical/blob/main/symmio-core/contracts/facets/liquidation/LiquidationFacetImpl.sol#L52-L88

https://github.com/sherlock-audit/2023-06-symmetrical/blob/main/symmio-core/contracts/facets/liquidation/LiquidationFacetImpl.sol#L152-L197

Tool used

Manual Review

Recommendation

The logic of liquidatePartyB and liquidatePositionsPartyB should be merged. But this maybe trigger OOG because the OpenPosition array is large.

Discussion

securitygrid

Escalate for 10 usdc This issue is not dup of #113. please review it.

sherlock-admin2

Escalate for 10 usdc This issue is not dup of #113. please review it.

You've created a valid escalation!

To remove the escalation from consideration: Delete your comment.

You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.

panprog

Escalate

This issue is invalid (will underflow), because it assumes that liquidatePositionsPartyA can be called after liquidatePartyB but before liquidatePositionsPartyB removes all quotes, but it's impossible, because:

  1. liquidatePartyB zeroes out partyB locked balances:

https://github.com/sherlock-audit/2023-06-symmetrical/blob/6d2b64b6732fcfbd07c8217897dd233dbb6cd1f5/symmio-core/contracts/facets/liquidation/LiquidationFacetImpl.sol#L300

  1. liquidatePositionsPartyA will underflow when trying to reduce partyB locked balances (because locked balances are 0):

https://github.com/sherlock-audit/2023-06-symmetrical/blob/6d2b64b6732fcfbd07c8217897dd233dbb6cd1f5/symmio-core/contracts/facets/liquidation/LiquidationFacetImpl.sol#L198

This is probably duplicate of #142 which is also invalid.

sherlock-admin2

Escalate

This issue is invalid (will underflow), because it assumes that liquidatePositionsPartyA can be called after liquidatePartyB but before liquidatePositionsPartyB removes all quotes, but it's impossible, because:

  1. liquidatePartyB zeroes out partyB locked balances: https://github.com/sherlock-audit/2023-06-symmetrical/blob/6d2b64b6732fcfbd07c8217897dd233dbb6cd1f5/symmio-core/contracts/facets/liquidation/LiquidationFacetImpl.sol#L300

  2. liquidatePositionsPartyA will underflow when trying to reduce partyB locked balances (because locked balances are 0): https://github.com/sherlock-audit/2023-06-symmetrical/blob/6d2b64b6732fcfbd07c8217897dd233dbb6cd1f5/symmio-core/contracts/facets/liquidation/LiquidationFacetImpl.sol#L198

This is probably duplicate of #142 which is also invalid.

You've created a valid escalation!

To remove the escalation from consideration: Delete your comment.

You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.

securitygrid

I agree that this issue is invalid due to revert. What I'm trying to say is that another problem arises here. all quotes in the OpenPositions array of A1 will be calculated into upnl(from offchain). This is wrong, the correct upnl should exclude quote1.

hrishibhat

@securitygrid confirming that this is not a valid issue?

securitygrid

If the scenario in the report discusses a problem with the calculation of upnl in step 9, it should be a valid issue. So, I suggest that sponsors take a look at this report.

panprog

If the scenario in the report discusses a problem with the calculation of upnl in step 9, it should be a valid issue.

I think that this report shows a valid problem (quotes still open after liquidation and some actions allowed), however it didn't demonstrate the valid scenario to show the impact: it demonstrates liquidation after liquidation which isn't valid as it will revert. Upnl in muon app still calculating the profit from these quotes is out of scope of this audit.

I think this should be low.

securitygrid

Looking at this report again, I‘d like to argue that this issue should be a valid M. The liquidatePositionsPartyA function can liquidate the specified quote due to the second parameter.

  1. It is no problem to liquidate quote2 via liquidatePositionsPartyA.
  2. Liquidating quote1 via liquidatePositionsPartyA will revert. But it doesn't matter anymore. Because this funds should be given to B2. B2 caused the loss. This is impact.
  3. The title of this report mentions "liquidated again". Although revert will occur, it has been counted into unpl. This can also be regarded as "liquidated again".

panprog

@securitygrid I agree that the impact is medium (although very very unlikely: the scenario presented assumes no partyB liquidation finalization for a very long time, enough for the price to grow significantly. And then again, in order for partyB2 to not receive its necessary funds from partyA liquidation - partyA should be liquidated as LATE or OVERDUE, which is unlikely by itself). However, the reason it happens is the muon app which is out of scope

9. From the flow of the [uPnlPartyA](https://github.com/sherlock-audit/2023-06-symmetrical/blob/main/symmio-core/muon/crypto_v3.js#L238-L267) function in crypto_v3.js, all quotes in the OpenPositions array of A1 will be calculated into upnl.

That's exactly why it happens: quote1 shouldn't be included in upnl calculation (quotes with liquidated partyB must be excluded, because they're already accounted for right after liquidatePartyB). But this is the bug on the muon app side, not on the smart contract.

securitygrid

#160 also use meon app bug: muon app doesn't check for liquidation of active position's parties, and smart contract code also ignores this.
if this issue is not valid due to muon app, #160 should be not valid.

I've also noticed that discussions in other issues mention code from crypto_v3.js. If its code doesn't need to be considered, then those discussions don't make sense.

panprog

#160 is about deallocate and some other functions (including liquidatePartyA) being allowed for partyA while some partyB is not finished liquidating. It's not about muon app bug specifically, it just uses muon app bug to demonstrate the impact, and as I said in comments, even if muon app bug is fixed or not used, there are the other fully in-scope ways (multiple ways) to exploit that bug.

This report talks about liquidatePartyB and liquidatePositionsPartyB being not atomic, which I don't think is a bug as this separation is to avoid out of gas when liquidating many positions. The scenario presented in this report reverts, and what you say about partyB2 not getting paid happens only because of muon app bug. So muon app is the main reason of this bug. Besides, the scenario is so unlikely that I'm not fully sure it can be medium impact.

It does use the liquidatePartyA mentioned in #160, so indirectly it does imply usage of partyA functionality while partyB is liquidated, so I'm not really sure how to treat this. Maybe it's a duplicate of #160 (overlaps some of it description). But #160 is definitely valid and in-scope as the reason is not muon app.

MoonKnightDev

Fixed code PR link: https://github.com/SYMM-IO/symmio-core/pull/22

hrishibhat

Sponsor comment:

the auditor has accurately pinpointed the problem. We were aware of a similar issue and have since addressed and resolved it. Added the proper tags

hrishibhat

Result: Medium Unique After considering the comments and based on Sponsor comments, considering this issue a valid medium

sherlock-admin2

Escalations have been resolved successfully!

Escalation status:

The original report as published on Sherlock, by nobody2018. 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.

How it worked

Impact

Quotes that have already been liquidated can be liquidated again in some cases, potentially causing a loss of funds for other PartyBs, though the scenario is described as unlikely and may involve out-of-scope components.

What the attacker needed

PartyB liquidation without position removal; PartyA later becomes liquidatable; Price movement as described

The fix

The logic of liquidatePartyB and liquidatePositionsPartyB should be merged, but this may cause out-of-gas issues if there are many open positions.

How it was found

  1. PartyA creates a quote and PartyB1 opens it, adding it to both parties' open positions.
  2. The price moves against PartyB1, making them liquidatable.
  3. A liquidator calls liquidatePartyB on PartyB1, which transfers PartyB1's allocated balance to PartyA but does not remove the open position.
  4. PartyA creates another quote with PartyB2, which is also opened.
  5. The price moves against PartyA, making them liquidatable.
  6. A liquidator liquidates PartyA, and the profit and loss calculation includes the still-open position from PartyB1, causing funds to be allocated to the already-liquidated PartyB1.

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 this trading system, when one side (PartyB) is liquidated, their open trades should be closed and removed. But the closing step is separate and might not happen. If it doesn't, those trades stay in the system. Later, when the other side (PartyA) is liquidated, the system still counts those old trades when calculating who gets what, so money can go to the already-liquidated PartyB instead of the correct PartyB.

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.

More by nobody2018

View all

7 more disclosed reports, best first.

More from Symmetrical Update

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.