Skip to content
Business logic Audit finding Intermediate

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

https://github.com/code-423n4/2022-12-tigris/blob/588c84b7bb354d20cbca6034544c4faa46e6a80e/contracts/Trading.sol#L178-L179

https://github.com/code-423n4/2022-12-tigris/blob/588c84b7bb354d20cbca6034544c4faa46e6a80e/contracts/Trading.sol#L734-L738

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.


The original report as published on Code4rena, by carlitox477. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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.

More by carlitox477

View all

9 more disclosed reports, best first.

More from Tigris Trade contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.