Collateral tokens that cannot be automatically swapped to the PnL token, cannot have slippage applied to them
When a decrease order tries to swap collateral to the PnL token but the swap fails, the collateral is returned without any slippage check because the only slippage parameter is in PnL token units. A user with a resting order could therefore receive less value than intended due to price impact.
Source: https://github.com/sherlock-audit/2023-02-gmx-judging/issues/144
Found by
IllIllI
Summary
Collateral tokens that cannot be automatically swapped to the PnL token, cannot have slippage applied to them, since the minOutputAmount is in units of the output token, not the secondary token.
Vulnerability Detail
If a user's order uses the Order.DecreasePositionSwapType.SwapCollateralTokenToPnlToken flag, it's possible for the swap to fail (e.g. because the token is paused), and in such cases, the collateral token is sent back as-is, without being converted to the PnL token. In such cases, it's not possible for the code, as it is written, to support slippage in such scenarios, because there is only one order slippage argument, minOutputAmount, and it's in units of the PnL token, not the collateral token.
Impact
A user that has a resting order open with the flag set, so that they can take profit at the appropriate time, will be forced to incur any price impact slippage present, even if they had specified a valid minOutputAmount that would otherwise have prevented the sub-optimal execution.
Code Snippet
If the swap goes through, the secondaryOutputAmount is cleared and added to the outputAmount, but if the swap fails, it's kept as the values.output.secondaryOutputAmount:
// File: gmx-synthetics/contracts/position/DecreasePositionCollateralUtils.sol : DecreasePositionCollateralUtils.swapWithdrawnCollateralToPnlToken() #1
383 try params.contracts.swapHandler.swap(
...
396 ) returns (address tokenOut, uint256 swapOutputAmount) {
397 if (tokenOut != values.output.secondaryOutputToken) {
398 revert InvalidOutputToken(tokenOut, values.output.secondaryOutputToken);
399 }
400 // combine the values into outputToken and outputAmount
401 values.output.outputToken = tokenOut;
402 @> values.output.outputAmount = values.output.secondaryOutputAmount + swapOutputAmount;
403 values.output.secondaryOutputAmount = 0;
404 } catch Error(string memory reason) {
405 @> emit SwapUtils.SwapReverted(reason, "");
406 } catch (bytes memory reasonBytes) {
407 (string memory reason, /* bool hasRevertMessage */) = ErrorUtils.getRevertMessage(reasonBytes);
408 @> emit SwapUtils.SwapReverted(reason, reasonBytes);
409 }
410 }
411
412 return values;
413: }
And is sent separately, with no slippage checks.
Tool used
Manual Review
Recommendation
Convert the USD value of secondaryOutputAmount to outputAmount, and ensure that the slippage checks against that total
Discussion
xvi10
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 slippage check only applies to the PnL token output, so when the swap fails and collateral is returned directly, no slippage protection is applied to that collateral amount.
Impact
A user with a resting order could incur price impact slippage even if they set a valid minOutputAmount, potentially receiving less value than expected.
What the attacker needed
A resting order with swap flag set; swap fails (e.g., token paused)
The fix
The code was fixed by converting the USD value of the secondary output amount to the output amount and ensuring slippage checks against that total.
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 a trade, the system may try to convert your collateral into a different token. If that conversion fails, you get the original token back, but the system cannot check whether you received a fair amount because its slippage limit only applies to the other token. This means you might get less than you expected.
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.