Open sherlock-admin2 opened 7 months ago
1 comment(s) were left on this issue during the judging contest.
takarez commented:
valid: high(1)
The protocol team fixed this issue in PR/commit https://github.com/dhedge/flatcoin-v1/pull/264.
The Lead Senior Watson signed off on the fix.
xiaoming90
high
Incorrect price used when updating the global position data
Summary
Incorrect price used when updating the global position data leading to a loss of assets for LPs.
Vulnerability Detail
Near the end of the liquidation process, the
updateGlobalPositionData
function at Line 159 will be executed to update the global position data. However, when executing theupdateGlobalPositionData
function, the code sets the price at Line 160 below to the position's last price (position.lastPrice
), which is incorrect. The price should be set to the current price instead, and not the position's last price.https://github.com/sherlock-audit/2023-12-flatmoney/blob/main/flatcoin-v1/src/LiquidationModule.sol#L160
The reason why the
updateGlobalPositionData
function expects a current price to be passed in is that within thePerpMath._profitLossTotal
function, it will compute the price shift between the current price and the last price to obtain the PnL of all the open positions. Also, per the comment at Line 170 below, it expects the current price of the collateral to be passed in.Thus, it is incorrect to pass in the individual position's last/entry price, which is usually the price of the collateral when the position was first opened or adjusted some time ago.
Thus, if the last/entry price of the liquidated position is higher than the current price of collateral, the PnL will be inflated, indicating more gain for the long traders. Since this is a zero-sum game, this also means that the LP loses more assets than expected due to the inflated gain of the long traders.
https://github.com/sherlock-audit/2023-12-flatmoney/blob/main/flatcoin-v1/src/FlatcoinVault.sol#L173
Impact
Loss of assets for the LP as mentioned in the above section.
Code Snippet
https://github.com/sherlock-audit/2023-12-flatmoney/blob/main/flatcoin-v1/src/LiquidationModule.sol#L160
Tool used
Manual Review
Recommendation
Use the current price instead of liquidated position's last price when update the global position data