Users can bypass the maxWinPercent limit using a partially closing
The maxWinPercent cap was checked against the full trade margin rather than the margin being closed, so a trader could partially close a position repeatedly and collect far more payout than the intended limit. This let an attacker drain more funds from the protocol than the maximum profit rule allowed.
hansfriese Follow with 0x52, 0xA5DF and 1 other
Originally disclosed on Code4rena
Submitted by hansfriese, also found by 0x52, 0xA5DF, and bin2chen
Users can bypass the maxWinPercent limit using a partial closing.
As a result, users can receive more funds than their upper limit from the protocol.
Proof of Concept
As we can see from the documentation, there is limitation of a maximum PnL.
Maximum PnL is +500%. The trade won't be closed unless the user sets a Take Profit order or closes the position manually.
And this logic was implemented like below in _closePosition().
File: 2022-12-tigris\contracts\Trading.sol
624: _toMint = _handleCloseFees(_trade.asset, uint256(_payout)*_percent/DIVISION_CONSTANT, _trade.tigAsset, _positionSize*_percent/DIVISION_CONSTANT, _trade.trader, _isBot);
625: if (maxWinPercent > 0 && _toMint > _trade.margin*maxWinPercent/DIVISION_CONSTANT) { //@audit bypass limit
626: _toMint = _trade.margin*maxWinPercent/DIVISION_CONSTANT;
627: }
But it checks the maxWinPercent between the partial payout and full margin so the below scenario is possible.
- Alice opened an order of margin = 100 and PnL = 1000 after taking closing fees.
- If
maxWinPercent= 500%, Alice should receive 500 at most. - But Alice closed 50% of the position and she got 500 for a 50% margin because it checks
maxWinPercentwith_toMint = 500and_trade.margin = 100 - After she closed 50% of the position, the remaining margin = 50 and PnL = 500 so she can continue step 3 again and again.
- As a result, she can withdraw almost 100% of the initial PnL(1000) even though she should receive at most 500.
Recommended Mitigation Steps
We should check the maxWinPercent between the partial payout and partial margin like below.
_toMint = _handleCloseFees(_trade.asset, uint256(_payout)*_percent/DIVISION_CONSTANT, _trade.tigAsset, _positionSize*_percent/DIVISION_CONSTANT, _trade.trader, _isBot);
uint256 partialMarginToClose = _trade.margin * _percent / DIVISION_CONSTANT; //+++++++++++++++++++++++
if (maxWinPercent > 0 && _toMint > partialMarginToClose*maxWinPercent/DIVISION_CONSTANT) {
_toMint = partialMarginToClose*maxWinPercent/DIVISION_CONSTANT;
}
TriHaz (Tigris Trade) confirmed, but disagreed with severity and commented:
I would label this as Medium risk as a +500% win is required so assets are not in a direct risk.
Alex the Entreprenerd (judge) commented:
The Warden has shown how, by partially closing an order, it is possible to bypass the
maxWinPercentcap.Per similar discussion to #111 the fact that not every trade can be above 500% in payout is not a guarantee that some trade will be, and those that will, will cause the invariant to be broken and LPs to be deeper in the red than they should.
Because this causes an immediate gain to the attacker, at a loss for LPs, I agree with High Severity.
GainsGoblin (Tigris Trade) resolved:
Mitigation: https://github.com/code-423n4/2022-12-tigris/pull/2#issuecomment-1419176771
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 the cap compared the partial payout against the full trade margin instead of the margin portion being closed.
Impact
A trader could receive more funds than the protocol's maximum profit limit allows, causing a loss to liquidity providers; the project noted a +500% win is required.
What the attacker needed
An open position with over 500% profit; ability to partially close the position
The fix
The cap should be checked against the partial margin being closed rather than the full trade margin; the project later resolved it with a mitigation pull request.
How it was found
- Open a position with a margin of 100 and a PnL of 1000 after closing fees.
- Close 50% of the position; the cap is checked against the full margin of 100, so 500 is paid out.
- The remaining margin is 50 and PnL is 500, so repeat the partial close.
- Continue until almost the entire initial PnL of 1000 has been withdrawn.
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.
A trading protocol caps how much profit a trader can take from a single position. The check compared the payout against the whole original deposit instead of the part being closed, so closing the position in small pieces let the trader collect the capped amount many times over.
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.