Trading#initiateMarketOrder allows to open a position with more margin than expected due to _handleOpenFees wrong calculation when a trade is referred
The fee calculation in the trading contract ignored referral fees, so when a trade was referred, the margin after fees was overstated, allowing a larger position to be opened than intended. An attacker could exploit this to gain more margin than expected.
carlitox477 Follow with koxuan
Originally disclosed on Code4rena
Submitted by carlitox477, also found by koxuan
When initiateMarketOrder is called, _marginAfterFees are calculated and then used to calculate _positionSize:
uint256 _marginAfterFees = _tradeInfo.margin - _handleOpenFees(_tradeInfo.asset, _tradeInfo.margin*_tradeInfo.leverage/1e18, _trader, _tigAsset, false);
uint256 _positionSize = _marginAfterFees * _tradeInfo.leverage / 1e18;
The problem is that _handleOpenFees does not consider referrer fees when it calculates its output (paidFees), leading to open a position greater than expected.
Impact
For a referred trade, initiateMarketOrder always opens a position greater than the one supposed, by allowing the use of more margin than the one expected.
Proof of Concept
The output of _handleOpenFees is _feePaid, which is calculated once, and it does not consider referralFees:
// No refferal fees are considered
_feePaid =
_positionSize
* (_fees.burnFees + _fees.botFees) // get total fee%
/ DIVISION_CONSTANT // divide by 100%
+ _daoFeesPaid;
Then we can notice that, if the output of _handleOpenFees did not consider referral fees, neither would \_marginAfterFees do:
uint256 _marginAfterFees =
_tradeInfo.margin-
_handleOpenFees(
_tradeInfo.asset,
_tradeInfo.margin*_tradeInfo.leverage/1e18,
_trader,
_tigAsset,
false);
// @audit Then _positionSize would be greater than what is supposed to be, allowing to create a position greater than expected
uint256 _positionSize = _marginAfterFees * _tradeInfo.leverage / 1e18;
Recommended Mitigation steps
Consider referral fees when _feePaid is calculated in _handleOpenFees:
// In _handleOpenFees function
+ uint256 _refFeesToConsider = _referrer == address(0) ? 0 : _fees.referralFees;
_feePaid =
_positionSize
- * (_fees.burnFees + _fees.botFees) // get total fee%
+ * (_fees.burnFees + _fees.botFees + _refFeesToConsider) // get total fee%
/ DIVISION_CONSTANT // divide by 100%
+ _daoFeesPaid;
TriHaz (Tigris Trade) confirmed
Alex the Entreprenerd (judge) commented:
The warden has shown an accounting issue in how fees are calculated, the refactoring is straightforward.
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
Impact
For a referred trade, the function always opens a position greater than supposed, allowing the use of more margin than expected.
What the attacker needed
Trade must be referred
The fix
The fee calculation should include referral fees when computing the total fee, so that the margin after fees is correct.
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 open a trade, the platform takes a fee from your margin before calculating your position size. If you were referred by someone, an extra referral fee should also be taken, but the code forgot to include it. This meant your margin appeared larger than it really was, so you could open a bigger position than you should have been able to.
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.