Open code423n4 opened 1 year ago
Primary because of clean code snippets
GalloDaSballo marked the issue as primary issue
TriHaz marked the issue as sponsor confirmed
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.
GalloDaSballo changed the severity to 3 (High Risk)
GalloDaSballo marked the issue as selected for report
Lines of code
https://github.com/code-423n4/2022-12-tigris/blob/588c84b7bb354d20cbca6034544c4faa46e6a80e/contracts/Trading.sol#L275-L282
Vulnerability details
Impact
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 and lowering the collateralization ratio of
tigAsset
.Proof of Concept
In
Trading.addToPosition
the_handleDeposit
function is called like this:The third parameter with the value of
_addMargin - _fee
is the amount pulled (or burned in the case of usingtigAsset
) from the user. The_fee
value is calculated as part of the position size like this:The
_handleOpenFees
function mints_tigAsset
to the referrer, to themsg.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
:Here the third parameter to
_handleDeposit
is not_marginAfterFees
but_tradeInfo.margin
which is what the user has input and is supposed to pay.Tools Used
Manual Review
Recommended Mitigation Steps
In
Trading.addToPosition
call the_handleDeposit
function without subtracting the_fee
value: