Skip to content
Audit finding Intermediate

Rounding error when closing quote

A rounding error in the closeQuote function can cause the locked balance to remain unchanged when a position is partially closed with a very small filledAmount. An attacker could repeatedly close tiny amounts to accumulate locked balances, reducing the available balance they can withdraw.

mstpr-brainbot Follow with xiaoming90

Originally disclosed on Sherlock

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

Found by

mstpr-brainbot, xiaoming90

Summary

Rounding errors could occur if the provided filledAmount is too small, resulting in the locked balance of an account remains the same even though a certain amount of the position has been closed.

Vulnerability Detail

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

Solidity
File: LibQuote.sol
149:     function closeQuote(Quote storage quote, uint256 filledAmount, uint256 closedPrice) internal {
150:         QuoteStorage.Layout storage quoteLayout = QuoteStorage.layout();
151:         AccountStorage.Layout storage accountLayout = AccountStorage.layout();
152: 
153:         quote.modifyTimestamp = block.timestamp;
154: 
155:         LockedValues memory lockedValues = LockedValues(
156:             quote.lockedValues.cva -
157:                 ((quote.lockedValues.cva * filledAmount) / (LibQuote.quoteOpenAmount(quote))),
158:             quote.lockedValues.mm -
159:                 ((quote.lockedValues.mm * filledAmount) / (LibQuote.quoteOpenAmount(quote))),
160:             quote.lockedValues.lf -
161:                 ((quote.lockedValues.lf * filledAmount) / (LibQuote.quoteOpenAmount(quote)))
162:         );
163:         accountLayout.lockedBalances[quote.partyA].subQuote(quote).add(lockedValues);
164:         accountLayout.partyBLockedBalances[quote.partyB][quote.partyA].subQuote(quote).add(
165:             lockedValues
166:         );
167:         quote.lockedValues = lockedValues;
168: 
169:         (bool hasMadeProfit, uint256 pnl) = LibQuote.getValueOfQuoteForPartyA(
170:             closedPrice,
171:             filledAmount,
172:             quote
173:         );
174:         if (hasMadeProfit) {
175:             accountLayout.allocatedBalances[quote.partyA] += pnl;
176:             accountLayout.partyBAllocatedBalances[quote.partyB][quote.partyA] -= pnl;
177:         } else {
178:             accountLayout.allocatedBalances[quote.partyA] -= pnl;
179:             accountLayout.partyBAllocatedBalances[quote.partyB][quote.partyA] += pnl;
180:         }

In Lines 157, 159, and 161 above, a malicious user could make the numerator smaller than the denominator (LibQuote.quoteOpenAmount(quote)), and the result will be zero due to a rounding error in Solidity.

In this case, the quote.lockedValues will not decrease and will remain the same. As a result, the locked balance of the account will remain the same even though a certain amount of the position has been closed. This could cause the account's locked balance to be higher than expected, and the errors will accumulate if it happens many times.

Impact

When an account's locked balances are higher than expected, their available balance will be lower than expected. The available balance affects the amount that users can withdraw from their accounts. The "silent" increase in their locked values means that the amount that users can withdraw becomes lesser over time, and these amounts are lost due to the errors.

Code Snippet

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

Tool used

Manual Review

Recommendation

When the ((quote.lockedValues.cva * filledAmount) / (LibQuote.quoteOpenAmount(quote))) rounds down to zero, this means that a rounding error has occurred as the numerator is smaller than the denominator. The CVA, filledAmount or both might be too small.

Consider performing input validation against the filledAmount within the fillCloseRequest function to ensure that the provided values are sufficiently large and will not result in a rounding error.

Discussion

sherlock-admin2

Escalate

Given that:

The amounts remaining locked are very small (magnitude of few wei per call), this issue should be of low severity per sherlock standards

You've deleted an escalation for this issue.

xiaoming9090

Escalate

Escalate

Given that:

The amounts remaining locked are very small (magnitude of few wei per call), this issue should be of low severity per sherlock standards

Regarding the second bullet point, the checks will only be relevant if the position is fully closed via the forceClosePosition or emergencyClosePosition function, as they are forced to close the entire quantity of the position. However, if the position is partially filled via the fillCloseRequest, the error might still occur since the caller can specify an arbitrary filledAmount.

If an account has only traded a few times and the error only happens a few times, it might be fine. However, this is a trading protocol, and it is expected that there will be users who are active traders or entities that perform high-frequency or algorithmic trading.

Thus, the error will eventually accumulate into a significant value for these types of traders, and they will suffer the consequences as mentioned in my impact section of the report. In addition, it breaks an important protocol invariant where the locked balance does not decrease when a position is closed, which is unacceptable.

sherlock-admin2

Escalate

Escalate

Given that:

The amounts remaining locked are very small (magnitude of few wei per call), this issue should be of low severity per sherlock standards

Regarding the second bullet point, the checks will only be relevant if the position is fully closed via the forceClosePosition or emergencyClosePosition function, as they are forced to close the entire quantity of the position. However, if the position is partially filled via the fillCloseRequest, the error might still occur since the caller can specify an arbitrary filledAmount.

If an account has only traded a few times and the error only happens a few times, it might be fine. However, this is a trading protocol, and it is expected that there will be users who are active traders or entities that perform high-frequency or algorithmic trading.

Thus, the error will eventually accumulate into a significant value for these types of traders, and they will suffer the consequences as mentioned in my impact section of the report. In addition, it breaks an important protocol invariant where the locked balance does not decrease when a position is closed, which is unacceptable.

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.

SergeKireev

Escalate

This issue is invalid because LibQuote.quoteOpenAmount(quote)) is defined as:

function quoteOpenAmount(Quote storage quote) internal view returns (uint256) {
    return quote.quantity - quote.closedAmount;
}

in the closeQuote function, quote.closedAmount is also updated as follows:

quote.closedAmount += filledAmount;

Which means that when the user closes the position completely filledAmount == LibQuote.quoteOpenAmount(quote)) (which also means filledAmount/LibQuote.quoteOpenAmount(quote)) == 1 and without rounding error) and the whole amount is unlocked in any case

sherlock-admin2

Escalate

This issue is invalid because LibQuote.quoteOpenAmount(quote)) is defined as:

function quoteOpenAmount(Quote storage quote) internal view returns (uint256) {
    return quote.quantity - quote.closedAmount;
}

in the closeQuote function, quote.closedAmount is also updated as follows:

quote.closedAmount += filledAmount;

Which means that when the user closes the position completely filledAmount == LibQuote.quoteOpenAmount(quote)) (which also means filledAmount/LibQuote.quoteOpenAmount(quote)) == 1 and without rounding error) and the whole amount is unlocked in any case

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.

Evert0x

@xiaoming9090 any comment on the escalation by @SergeKireev ?

MoonKnightDev

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

xiaoming9090

Escalate

This issue is invalid because LibQuote.quoteOpenAmount(quote)) is defined as:

function quoteOpenAmount(Quote storage quote) internal view returns (uint256) {
    return quote.quantity - quote.closedAmount;
}

in the closeQuote function, quote.closedAmount is also updated as follows:

quote.closedAmount += filledAmount;

Which means that when the user closes the position completely filledAmount == LibQuote.quoteOpenAmount(quote)) (which also means filledAmount/LibQuote.quoteOpenAmount(quote)) == 1 and without rounding error) and the whole amount is unlocked in any case

Disagree. This does not make the issue invalid. If the trader executes many small partial closes without completely closing the position, the accumulated error in the locked values will exist. It is unacceptable for the protocol to have inaccurate locked values at any single point in time. The locked values are used to determine many crucial decisions throughout the protocol, such as in assessing the solvency of an account.

hrishibhat

Result: Medium Has duplicates Agree with the fix and the comment here: https://github.com/sherlock-audit/2023-06-symmetrical-judging/issues/251#issuecomment-1685574052 maintaining severity as is

sherlock-admin2

Escalations have been resolved successfully!

Escalation status:

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

Root cause

Likely integer division truncation in Solidity when calculating the reduction in locked values, where the numerator (locked value multiplied by filledAmount) is smaller than the denominator (open amount), resulting in zero reduction.

Impact

The locked balance of an account could become higher than expected, reducing the available balance that users can withdraw, and the error accumulates with repeated small partial closes.

What the attacker needed

A trading account on the platform; ability to call fillCloseRequest with small filledAmount

The fix

The fix was to add input validation on filledAmount in the fillCloseRequest function to ensure it is sufficiently large to avoid rounding errors, as indicated by the fixed code PR link.

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.

When you close part of a trade, the smart contract should reduce the amount of money locked as collateral. But if you close a very tiny amount, the maths rounds down to zero, so the locked amount doesn't change. By doing this many times, a trader could end up with more money locked than they should, meaning they can withdraw less.

More by mstpr-brainbot

View all

43 more disclosed reports, best first.

More from Symmetrical Update

View all

The newest disclosed reports for this program.

Similar reports

Same bug class and techniques, clearest first.