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.
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.
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.
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:
All amounts are of precision 18 decimals and multiplication before division rule is implemented
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:
All amounts are of precision 18 decimals and multiplication before division rule is implemented
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:
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:
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 ?
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.
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.
Generating the AI write-up for this report.
This usually takes about a minute. 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 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.
Generating the AI write-up for this report.
This usually takes about a minute. 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 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.