Open sherlock-admin2 opened 4 months ago
request poc
PoC requested from @johnson37
Requests remaining: 12
There is a problem here: when cache.recordPnlToken + cache.settledFee is negative.
The protocol team fixed this issue in the following PRs/commits: https://github.com/0xCedar/elfi-perp-contracts/pull/31
The Lead Senior Watson signed off on the fix.
jennifer37
Medium
Incorrect settleFee process for cross-margin account
Summary
Settle fee is processed twice when the closed/decreased position is cross-margin position.
Vulnerability Detail
When the traders want to close or decrease their cross-margin positions, settle fees are generated. Settle fees include borrow fee, funding fee, and close fee. When the settle fee is positive, the traders need to pay for the settle fee, otherwise, the traders will receive settle fees. The vulnerability exists in function
_settleCrossAccount
. In this function, we will update the trader's cross-margin account viasubTokenWithLiability
oraddToken
at the first time. After that, if Pnl is larger than 0, settle fee will be process again, which is in wrong direction.For example, if the settle fee is 10. The function will decrease this settle fees from cross-margin account via
subTokenWithLiability
, and then increase this settle fees to the cross-margin account viaaddToken
. This means that the traders don't need to pay for the settle fee. Of course, the traders cannot gain the settle fee profit if the settle fee is negative.What's more, considering that when
cache.recordPnlToken
is positive andcache.settledFee
is negative, and the sum ofcache.recordPnlToken
andcache.settledFee
is negative, this could cause reverted because(cache.recordPnlToken + cache.settledFee).toUint256()
cast failure.Impact
cache.recordPnlToken + cache.settledFee
is negative.Code Snippet
https://github.com/sherlock-audit/2024-05-elfi-protocol/blob/main/elfi-perp-contracts/contracts/process/DecreasePositionProcess.sol#L338-L368
Tool used
Manual Review
Recommendation
Don't process the settle fee twice.