Trading.sol: addToPosition function does allow trader to increase position size without paying fees #194
Labels
3 (High Risk)
Assets can be stolen/lost/compromised directly
bug
Something isn't working
duplicate-659
satisfactory
satisfies C4 submission criteria; eligible for awards
upgraded by judge
Original issue severity upgraded from QA/Gas by judge
Lines of code
https://github.com/code-423n4/2022-12-tigris/blob/496e1974ee3838be8759e7b4096dbee1b8795593/contracts/Trading.sol#L255-L305
https://github.com/code-423n4/2022-12-tigris/blob/496e1974ee3838be8759e7b4096dbee1b8795593/contracts/Trading.sol#L278
https://github.com/code-423n4/2022-12-tigris/blob/496e1974ee3838be8759e7b4096dbee1b8795593/contracts/Trading.sol#L293-L294
Vulnerability details
Impact
When opening a position, an opening fee has to be paid.
You can see this in the
Trading.initiateMarketOrder
function (https://github.com/code-423n4/2022-12-tigris/blob/496e1974ee3838be8759e7b4096dbee1b8795593/contracts/Trading.sol#L163-L210).It deposits the whole
_tradeInfo.margin
(https://github.com/code-423n4/2022-12-tigris/blob/496e1974ee3838be8759e7b4096dbee1b8795593/contracts/Trading.sol#L180) but the trade is only created with the_marginAfterFees
(https://github.com/code-423n4/2022-12-tigris/blob/496e1974ee3838be8759e7b4096dbee1b8795593/contracts/Trading.sol#L185), i.e. the margin minus the opening fees.This is how paying the opening fee should work.
There is an issue in the
Trading.addToPosition
function (https://github.com/code-423n4/2022-12-tigris/blob/496e1974ee3838be8759e7b4096dbee1b8795593/contracts/Trading.sol#L255-L305). Instead of depositing the whole_addMargin
which is the margin that is added to the position, the_addMargin - _fee
(https://github.com/code-423n4/2022-12-tigris/blob/496e1974ee3838be8759e7b4096dbee1b8795593/contracts/Trading.sol#L278) is deposited. This is the same amount that is then added to the position (https://github.com/code-423n4/2022-12-tigris/blob/496e1974ee3838be8759e7b4096dbee1b8795593/contracts/Trading.sol#L293-L294).In effect this means that no opening fees are deducted from the margin that is deposited.
Therefore increasing a position is free for the trader (except gas fees).
A trader can exploit this by opening a small position first (which must be as big as the minimum position size).
The trader can then increase his position to the desired size using the
Trading.addToPosition
function. So the trader only needs to pay the opening fee for the minimum position size.Proof of Concept
Trading.initiateMarketOrder
function. The position is big enough to meet the minimum position size requirement (https://github.com/code-423n4/2022-12-tigris/blob/496e1974ee3838be8759e7b4096dbee1b8795593/contracts/TradingExtension.sol#L218)Trading.addToPosition
function to increase his position to the desired size.The amount he needs to deposit into the
StableVault
is_addMargin - fee
:The
_newMargin
is calculated as_trade.margin + _addMargin - fee
:The margin of the trade is then changed to be the
_newMargin
:position.addToPosition( _trade.id, _newMargin, _newPrice );
Code from
Position.addToPosition
:In the end the trader deposited
_addMargin - fee
and the position was also increased by_addMargin - fee
. So the trader did not pay fees for increasing his position.Tools Used
VSCode
Recommended Mitigation Steps
The solution is very simple. The trader must deposit the whole
_addMargin
into the vault.The text was updated successfully, but these errors were encountered: