Skip to content
Business logic Audit finding Intermediate

Wrong calculation of solvency after request to close and after close position

The solvency check functions for closing a position fail to account for extra profit from the difference between the close price and the current price, causing transactions to revert even when the user would be solvent after closing. This prevents users from closing positions in cases where the price difference would make them solvent.

shaka Follow

Originally disclosed on Sherlock

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

Found by

shaka

Summary

isSolventAfterClosePosition and isSolventAfterRequestToClosePosition do not account for the extra profit that the user would get from closing the position.

Vulnerability Detail

When a party A creates a request for closing a position, the isSolventAfterRequestToClosePosition function is called to check if the user is solvent after the request. In the same way, when someone tries to close a position, the isSolventAfterClosePosition function is called to check if both party A and party B are solvent after closing the position.

Both functions calculate the available balance for party A and party B, and revert if it is lower than zero. After that, the function accounts for the the extra loss that the user would get as a result of the difference between closePrice and upnlSig.price, and checks if the user is solvent after that.

The problem is that the function does not account for the opposite case, that is the case where the user would get an extra profit as a result of the difference between closePrice and upnlSig.price. This means that the user would not be able to close the position, even if at the end of the transaction they would be solvent.

Proof of Concept

There is an open position with:

  • Position type: LONG
  • Quantity: 1
  • Locked: 50
  • Opened price: 100
  • Current price: 110
  • Quote position uPnL Party A: 10

Party B calls fillCloseRequest with:

  • Closed price: 120

In isSolventAfterClosePosition the following is calculated:

Solidity
partyAAvailableBalance = freeBalance + upnl + unlockedAmount = -5

And it reverts on:

Solidity
require(
    partyBAvailableBalance >= 0 && partyAAvailableBalance >= 0,
    "LibSolvency: Available balance is lower than zero"
);

However, the extra profit for closedPrice - upnlSig.price = 120 - 110 = 10 is not accounted for in the partyAAvailableBalance calculation, that should be partyAAvailableBalance = - 5 + 10 = 5. Party A would be solvent after closing the position, but the transaction reverts.

Impact

In a situation where the difference between the closed price and the current price will make the user solvent, users will not be able to close their positions, even if at the end of the transaction they would be solvent.

Code Snippet

https://github.com/sherlock-audit/2023-06-symmetrical/blob/main/symmio-core/contracts/libraries/LibSolvency.sol#L109-L152

https://github.com/sherlock-audit/2023-06-symmetrical/blob/main/symmio-core/contracts/libraries/LibSolvency.sol#L166-L184

Tool used

Manual Review

Recommendation

Add the extra profit to the partyAAvailableBalance calculation.

Discussion

MoonKnightDev

The line that is checking whether the available balance is less than zero or not and reverts, is assessing the user's status if the position were to close right now. If it doesn't meet these conditions and doesn't pass, it implies that the user is already insolvent.

shaka0x

Escalate.

Even if the user is not solvent at the beginning of the close process, I cannot find any reason for not allowing a user to close a position if the outcome is that they are not insolvent anymore and both parties receive what they are due, in the same way as a user may increase their collateral to recover solvency.

sherlock-admin2

Escalate.

Even if the user is not solvent at the beginning of the close process, I cannot find any reason for not allowing a user to close a position if the outcome is that they are not insolvent anymore and both parties receive what they are due, in the same way as a user may increase their collateral to recover solvency.

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

I agree with @shaka0x that this is a valid issue, even though very unlikely, but still possible, and it's more fair to let partyB close position with a price more favorable to partyA than requested, if it leads to solvent accounts after closure (even if partyA is insolvent at the current price).

On the other hand, I also understand the other point of view: if the user is already insolvent at the current price, he can be denied all position actions which will also be fair (as he's insolvent - he should be liquidated and no other actions allowed).

Ultimately I think this is a design choice, so it's up to developers to decide what is the expected behavior.

MoonKnightDev

The system is designed to verify the user's solvency after a request to close, but I agree that this step may be unnecessary. It might be sufficient to check solvency solely within the fillCloseRequest() function.

panprog

The system is designed to verify the user's solvency after a request to close, but I agree that this step may be unnecessary. It might be sufficient to check solvency solely within the fillCloseRequest() function.

The report is not about user being insolvent at the time of request to close, it's about user being insolvent at market price, but solvent at the requested price (during request to close) or at the closePrice (during fill close request).

During request (long position):

  • Current price is $1000. User is insolvent at this price, but solvent at price $1010. He requests to close at $1010, but transaction reverts (although it should allow user request to be closed at price $1010)

During fillCloseRequest:

  • User requested to close at market price $1000, he was solvent at that time
  • By the time partyB calls fillCloseRequest, market price is still $1000, but user is insolvent at price $1000 (due to the other positions he has), but is solvent at price $1010.
  • PartyB calls fillCloseRequest with a closePrice = $1010 (when market price = $1000). User is solvent at price $1010 and position should close successfully, but it reverts because user is insolvent at current market price ($1000).

MoonKnightDev

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

hrishibhat

Result: Medium Unique Considering this a valid medium based on the above comments

sherlock-admin2

Escalations have been resolved successfully!

Escalation status:

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

Users may be unable to close their positions even when the closing price would make them solvent, potentially leading to forced liquidation or other unintended consequences.

What the attacker needed

Open position with price difference; party B initiates close with favorable price

The fix

The extra profit from the price difference should be added to the available balance calculation.

How it was found

  1. Party A has an open LONG position with quantity 1, locked 50, opened price 100, current price 110, and uPnL 10.
  2. Party B calls fillCloseRequest with a closed price of 120.
  3. The function isSolventAfterClosePosition calculates partyAAvailableBalance as -5 and reverts.
  4. The extra profit of 10 from the price difference is not added, so the correct balance would be 5 and the transaction should succeed.

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 trading system, before you can close a position, the system checks if you have enough balance to cover potential losses. This check sometimes ignores extra profit you would make from the closing price, so it wrongly blocks you from closing even though you'd end up with enough money.

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 shaka

View all

5 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.