Skip to content
Business logic Audit finding Intermediate

Not enough margin pulled or burned from user when adding to a position

When adding to a position, the contract pulled less margin from the user than intended because it subtracted the fee from the deposit amount. An attacker could therefore open a larger position with less collateral, increasing leverage beyond the protocol's design.

minhtrng Follow with Aymen0909, hansfriese and 6 others

Originally disclosed on Code4rena

Submitted by minhtrng, also found by Aymen0909, hansfriese, 0Kage, Jeiwan, bin2chen, KingNFT, HollaDieWaldfee, and rvierdiiev

When adding to a position, the amount of margin pulled from the user is not as much as it should be, which leaks value from the protocol, lowering the collateralization ratio of tigAsset.

Proof of Concept

In Trading.addToPosition the _handleDeposit function is called like this:

_handleDeposit(
    _trade.tigAsset,
    _marginAsset,
    _addMargin - _fee,
    _stableVault,
    _permitData,
    _trader
);

The third parameter with the value of _addMargin - _fee is the amount pulled (or burned in the case of using tigAsset) from the user. The _fee value is calculated as part of the position size like this:

uint _fee = _handleOpenFees(_trade.asset, _addMargin*_trade.leverage/1e18, _trader, _trade.tigAsset, false);

The _handleOpenFees function mints _tigAsset to the referrer, to the msg.sender (if called by a function meant to be executed by bots) and to the protocol itself. Those minted tokens are supposed to be part of the _addMargin value paid by the user. Hence using _addMargin - _fee as the third parameter to _handleDeposit is going to pull or burn less margin than what was accounted for.

An example for correct usage can be seen in initiateMarketOrder:

uint256 _marginAfterFees = _tradeInfo.margin - _handleOpenFees(_tradeInfo.asset, _tradeInfo.margin*_tradeInfo.leverage/1e18, _trader, _tigAsset, false);
uint256 _positionSize = _marginAfterFees * _tradeInfo.leverage / 1e18;
_handleDeposit(_tigAsset, _tradeInfo.marginAsset, _tradeInfo.margin, _tradeInfo.stableVault, _permitData, _trader);

Here the third parameter to _handleDeposit is not _marginAfterFees but _tradeInfo.margin which is what the user has input on and is supposed to pay.

Recommended Mitigation Steps

In Trading.addToPosition call the _handleDeposit function without subtracting the _fee value:

_handleDeposit(
    _trade.tigAsset,
    _marginAsset,
    _addMargin,
    _stableVault,
    _permitData,
    _trader
);

TriHaz (Tigris Trade) confirmed

Alex the Entreprenerd (judge) increased severity to High and commented:

The Warden has shown how, due to an incorrect computation, less margin is used when adding to a position.

While the loss of fees can be considered Medium Severity, I believe that the lack of checks is ultimately allowing for more leverage than intended which not only breaks invariants but can cause further issues (sponsor cited Fees as a defense mechanism against abuse).

For this reason, I believe the finding to be of High Severity.

GainsGoblin (Tigris Trade) resolved:

Mitigation: https://github.com/code-423n4/2022-12-tigris/pull/2#issuecomment-1419177303


The original report as published on Code4rena, by minhtrng. 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

Root cause

The code subtracted the fee from the deposit amount before pulling funds, instead of pulling the full margin and accounting for the fee separately.

Impact

An attacker could add to a position with less margin than required, gaining higher leverage and potentially causing losses to the protocol.

What the attacker needed

A position already open; ability to call addToPosition

The fix

The fix was to call `_handleDeposit` with the full `_addMargin` amount without subtracting the fee.

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 trading contract, when you add money to a position, the code should take the full amount you specify. Here, it took less because it subtracted a fee first, so you ended up with more leverage than you paid for. This could let someone take on more risk than the system allows.

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 minhtrng

View all

11 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.