Liquidation of PartyA will fail due to underflow errors
Liquidation of PartyA fails due to an underflow when deducting a profit amount from PartyB's allocated balance, which may be smaller than the profit. As a result, liquidation cannot complete, assets are stuck, and the creditor (PartyB) cannot receive the liquidated assets.
bin2chen Follow with cergyk, mstpr-brainbot and 3 others
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-06-symmetrical-judging/issues/241
Found by
bin2chen, cergyk, mstpr-brainbot, panprog, simon135, xiaoming90
Summary
Liquidation of PartyA will fail due to underflow errors. As a result, assets will be stuck, and there will be a loss of assets for the counterparty (the creditor) since they cannot receive the liquidated assets.
Vulnerability Detail
File: LiquidationFacetImpl.sol
126: function liquidatePositionsPartyA(
127: address partyA,
128: uint256[] memory quoteIds
129: ) internal returns (bool) {
..SNIP..
152: (bool hasMadeProfit, uint256 amount) = LibQuote.getValueOfQuoteForPartyA(
153: accountLayout.symbolsPrices[partyA][quote.symbolId].price,
154: LibQuote.quoteOpenAmount(quote),
155: quote
156: );
..SNIP..
163: if (
164: accountLayout.liquidationDetails[partyA].liquidationType == LiquidationType.NORMAL
165: ) {
166: accountLayout.partyBAllocatedBalances[quote.partyB][partyA] += quote
167: .lockedValues
168: .cva;
169: if (hasMadeProfit) {
170: accountLayout.partyBAllocatedBalances[quote.partyB][partyA] -= amount;
171: } else {
172: accountLayout.partyBAllocatedBalances[quote.partyB][partyA] += amount;
173: }
174: } else if (
175: accountLayout.liquidationDetails[partyA].liquidationType == LiquidationType.LATE
176: ) {
177: accountLayout.partyBAllocatedBalances[quote.partyB][partyA] +=
178: quote.lockedValues.cva -
179: ((quote.lockedValues.cva * accountLayout.liquidationDetails[partyA].deficit) /
180: accountLayout.lockedBalances[partyA].cva);
181: if (hasMadeProfit) {
182: accountLayout.partyBAllocatedBalances[quote.partyB][partyA] -= amount;
183: } else {
184: accountLayout.partyBAllocatedBalances[quote.partyB][partyA] += amount;
185: }
186: } else if (
187: accountLayout.liquidationDetails[partyA].liquidationType == LiquidationType.OVERDUE
188: ) {
189: if (hasMadeProfit) {
190: accountLayout.partyBAllocatedBalances[quote.partyB][partyA] -= amount;
191: } else {
192: accountLayout.partyBAllocatedBalances[quote.partyB][partyA] +=
193: amount -
194: ((amount * accountLayout.liquidationDetails[partyA].deficit) /
195: uint256(-accountLayout.liquidationDetails[partyA].totalUnrealizedLoss));
196: }
197: }
Assume that at this point, the allocated balance of PartyB (accountLayout.partyBAllocatedBalances[quote.partyB][partyA]) only has 1000 USD.
In Line 152 above, the getValueOfQuoteForPartyA function is called to compute the PnL of a position. Assume the position has a huge profit of 3000 USD due to a sudden spike in price. For this particular position, PartyA will profit 3000 USD while PartyB will lose 3000 USD.
In this case, 3000 USD needs to be deducted from PartyB's account. However, when the accountLayout.partyBAllocatedBalances[quote.partyB][partyA] -= amount; code at Line 170, 182, or 190 gets executed, an underflow error will occur, and the transaction will revert. This is because partyBAllocatedBalances is an unsigned integer, and PartyB only has 1000 USD of allocated balance, but the code attempts to deduct 3000 USD.
Impact
Liquidation of PartyA will fail. Since liquidation cannot be completed, the assets that are liable to be liquidated cannot be transferred from PartyA (the debtor) to the counterparty (the creditor). Assets will be stuck, and there will be a loss of assets for the counterparty (the creditor) since they cannot receive the liquidated assets.
Code Snippet
Tool used
Manual Review
Recommendation
Consider implementing the following fixes to ensure that the amount to be deducted will never exceed the allocated balance of PartyB to prevent underflow errors from occurring.
if (hasMadeProfit) {
+ amountToDeduct = amount > accountLayout.partyBAllocatedBalances[quote.partyB][partyA] ? accountLayout.partyBAllocatedBalances[quote.partyB][partyA] : amount
+ accountLayout.partyBAllocatedBalances[quote.partyB][partyA] -= amountToDeduct
- accountLayout.partyBAllocatedBalances[quote.partyB][partyA] -= amount;
} else {
accountLayout.partyBAllocatedBalances[quote.partyB][partyA] += amount;
}
Discussion
MoonKnightDev
Fixed code PR link: https://github.com/SYMM-IO/symmio-core/pull/22
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
The code subtracts the profit amount from PartyB's allocated balance without checking if the balance is sufficient, leading to an unsigned integer underflow.
Impact
Liquidation of PartyA will fail, causing assets to be stuck and a loss for the counterparty (the creditor) who cannot receive the liquidated assets.
What the attacker needed
PartyA has a position with PartyB; PartyA's position has a large profit; PartyB's allocated balance is less than the profit
The fix
The code was fixed by capping the deducted amount at PartyB's allocated balance to prevent underflow.
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 smart contract, numbers that represent balances cannot go below zero. If the code tries to subtract more than the balance, the transaction fails with an 'underflow' error. Here, when liquidating a position, the code subtracts a profit amount from a balance that might be too small, causing the whole liquidation to fail and leaving funds stuck.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.