Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trading.sol: executeLimitOrder function increases open interest by amount that is too large due to not accounting for opening fee #137

Closed
code423n4 opened this issue Dec 13, 2022 · 2 comments
Labels
2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working duplicate-576 satisfactory satisfies C4 submission criteria; eligible for awards

Comments

@code423n4
Copy link
Contributor

Lines of code

https://github.com/code-423n4/2022-12-tigris/blob/496e1974ee3838be8759e7b4096dbee1b8795593/contracts/PairsContract.sol#L8
https://github.com/code-423n4/2022-12-tigris/blob/496e1974ee3838be8759e7b4096dbee1b8795593/contracts/Trading.sol#L480
https://github.com/code-423n4/2022-12-tigris/blob/496e1974ee3838be8759e7b4096dbee1b8795593/contracts/Position.sol#L99
https://github.com/code-423n4/2022-12-tigris/blob/496e1974ee3838be8759e7b4096dbee1b8795593/contracts/Trading.sol#L314
https://github.com/code-423n4/2022-12-tigris/blob/496e1974ee3838be8759e7b4096dbee1b8795593/contracts/Trading.sol#L480
https://github.com/code-423n4/2022-12-tigris/blob/496e1974ee3838be8759e7b4096dbee1b8795593/contracts/Trading.sol#L163

Vulnerability details

Impact

The PairsContract (https://github.com/code-423n4/2022-12-tigris/blob/496e1974ee3838be8759e7b4096dbee1b8795593/contracts/PairsContract.sol#L8) keeps track of the open interest for each [asset][tigAsset] pair.

The Trading.executeLimitOrder (https://github.com/code-423n4/2022-12-tigris/blob/496e1974ee3838be8759e7b4096dbee1b8795593/contracts/Trading.sol#L480) function causes the open interest to be increased.
However the amount by which it is increased is too big because it does not reduce the margin by the amount of fees that is paid upon opening a position.

Also once the position is closed the amount by which the open interest is reduced DOES respect the opening fee.
So the amount by which the open interest is increased is bigger than the amount by which the open interest is reduced when the trade is closed.

Therefore every trade that is opened by executing a limit order causes the open interest to rise by a small amount.

This in turn causes issues in the calculation of the funding rate which relies on the open interst (https://github.com/code-423n4/2022-12-tigris/blob/496e1974ee3838be8759e7b4096dbee1b8795593/contracts/Position.sol#L99).

There is no immediate loss of funds for a single trade. However in the long run the wrong funding rate calculation can make the Exchange unusable.

So I mark this issue as "Medium".

Proof of Concept

A limit order is created by calling the Trading.initiateLimitOrder function (https://github.com/code-423n4/2022-12-tigris/blob/496e1974ee3838be8759e7b4096dbee1b8795593/contracts/Trading.sol#L314).

It mints a trade and saves the full margin in it. The opening fees are only later incorporated. After the open interest is increased.

So once the limit order is created, Trading.executeLimitOrder must be called (https://github.com/code-423n4/2022-12-tigris/blob/496e1974ee3838be8759e7b4096dbee1b8795593/contracts/Trading.sol#L480).

In this function the trade is read into memory:

IPosition.Trade memory trade = position.trades(_id);

Then, without adjusting the trade.margin, the open interest is increased:

if (trade.direction) {
    tradingExtension.modifyLongOi(trade.asset, trade.tigAsset, true, trade.margin*trade.leverage/1e18);
} else {
    tradingExtension.modifyShortOi(trade.asset, trade.tigAsset, true, trade.margin*trade.leverage/1e18);
}

The trade margin is only later adjusted:

position.executeLimitOrder(_id, trade.price, trade.margin - _fee);

You can also easily see that this is a vulnerability by comparing the Trading.executeLimitOrder function to e.g. Trading.initiateMarketOrder (https://github.com/code-423n4/2022-12-tigris/blob/496e1974ee3838be8759e7b4096dbee1b8795593/contracts/Trading.sol#L163), which increases the open interest by the _positionSize amount:

if (_tradeInfo.direction) {
    tradingExtension.modifyLongOi(_tradeInfo.asset, _tigAsset, true, _positionSize);
} else {
    tradingExtension.modifyShortOi(_tradeInfo.asset, _tigAsset, true, _positionSize);
}

This amount uses the margin adjusted by fees:

uint256 _positionSize = _marginAfterFees * _tradeInfo.leverage / 1e18;

Tools Used

VSCode

Recommended Mitigation Steps

Fix:

                 trade.price -= trade.price * _spread / DIVISION_CONSTANT;
             }
             if (trade.direction) {
-                tradingExtension.modifyLongOi(trade.asset, trade.tigAsset, true, trade.margin*trade.leverage/1e18);
+                tradingExtension.modifyLongOi(trade.asset, trade.tigAsset, true, (trade.margin-_fee)*trade.leverage/1e18);
             } else {
-                tradingExtension.modifyShortOi(trade.asset, trade.tigAsset, true, trade.margin*trade.leverage/1e18);
+                tradingExtension.modifyShortOi(trade.asset, trade.tigAsset, true, (trade.margin-_fee)*trade.leverage/1e18);
             }
             _updateFunding(trade.asset, trade.tigAsset);
             position.executeLimitOrder(_id, trade.price, trade.margin - _fee);
@code423n4 code423n4 added 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working labels Dec 13, 2022
code423n4 added a commit that referenced this issue Dec 13, 2022
@c4-judge
Copy link
Contributor

GalloDaSballo marked the issue as duplicate of #576

@c4-judge
Copy link
Contributor

GalloDaSballo marked the issue as satisfactory

@c4-judge c4-judge added the satisfactory satisfies C4 submission criteria; eligible for awards label Jan 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working duplicate-576 satisfactory satisfies C4 submission criteria; eligible for awards
Projects
None yet
Development

No branches or pull requests

2 participants