A trader close to liquidation risks being liquidated by trying to reduce her position
Source: https://github.com/sherlock-audit/2023-05-perennial-judging/issues/76
Found by
cergyk
Summary
A trader close to liquidation risks being liquidated by trying to reduce her position
Vulnerability Detail
We can see in the implementation of closeTake/closeMake that the maintenanceInvariant is not enforced:
https://github.com/sherlock-audit/2023-05-perennial/blob/main/perennial-mono/packages/perennial/contracts/product/Product.sol#L246-L256
and the takerFee is deducted immediately from traders collateral, whereas the position reduction only takes effect at next settlement: https://github.com/sherlock-audit/2023-05-perennial/blob/main/perennial-mono/packages/perennial/contracts/product/Product.sol#L258-L273
This means that a legitimate user close to liquidation, who may want to reduce leverage by closing position will end up immediately liquidatable.
Example:
Maintenance factor is 2% Liquidation fee is 5% Taker fee is 0.1% Funding fee is 0%
Alice has a 50x leverage taker position opened as authorized per maintenance invariant:
Alice collateral: 100 DSU Alice taker position: notional of 5000 DSU
Alice tries to reduce her position to a more reasonable 40x, calls closeTake(1000).
A 1 DSU taker fee is deducted immediately from her collateral. Since the reduction of the position only takes effect on next oracle version, her position becomes immediately liquidatable.
A liquidator closes it entirely, making Alice lose an additional 4 DSU takerFee on next settlement and 250 DSU of liquidationFee.
Impact
A user trying to avoid liquidation is liquidated unjustly
Code Snippet
Tool used
Manual Review
Recommendation
Either:
- Do not deduce takerFee from collateral immediately, but at next settlement, as any other fees
or
- Enforce
maintenanceInvarianton closing of positions, to make transaction revert (except when closing is liquidation already)
Discussion
SergeKireev
Escalate for 10 USDC
Disagree with downgrading of severity. Although it can be argued the loss of funds is self-inflicted, reducing the position when near liquidation is the most reasonable choice to avoid liquidation. Instead of avoiding the liquidation, the user makes himself instantly liquidatable and risks losing 5% to 10% of collateral, which can be an unbounded loss of funds.
sherlock-admin
Escalate for 10 USDC
Disagree with downgrading of severity. Although it can be argued the loss of funds is self-inflicted, reducing the position when near liquidation is the most reasonable choice to avoid liquidation. Instead of avoiding the liquidation, the user makes himself instantly liquidatable and risks losing 5% to 10% of collateral, which can be an unbounded loss of funds.
You've created a valid escalation for 10 USDC!
To remove the escalation from consideration: Delete your comment.
You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.
sherlock-admin
Escalate for 10 USDC This is not valid M. In this situation on the verge of liquidation, no matter what you do, if the price moves in the direction of loss, the liquidation will occur. On the contrary, the position is safe. This is a normal phenomenon. The safe thing to do is to increase the margin.
You've deleted an escalation for this issue.
mstpr
Escalate for 10 USDC
I think this issue is invalid or informational.
First of all, that's a great point. However, I feel like that the users should be aware of this already. Maker/taker fees are actually the open/close position fees which they should be aware all the time. So, if a user closes some of their position they should know that there is a fee that they need to pay. If that fee takes them to liquidation level, this should be ok because that's how the system works.
Here how I see the case: In a product with both open/close position fees; If a user with a perfectly healthy position wants to leverage its position, they will pay an open position fee which is acceptable. When the same users wants to deleverage, it's also acceptable to take the close position fee. However, just because the deleverage now puts the user in liquidation, why is it not acceptable to take the close position fees? I think this is how the system works. Just because user is liquidatable after the position fees shouldn't mean that the protocol should not take fees from their action.
sherlock-admin
Escalate for 10 USDC
I think this issue is invalid or informational.
First of all, that's a great point. However, I feel like that the users should be aware of this already. Maker/taker fees are actually the open/close position fees which they should be aware all the time. So, if a user closes some of their position they should know that there is a fee that they need to pay. If that fee takes them to liquidation level, this should be ok because that's how the system works.
Here how I see the case: In a product with both open/close position fees; If a user with a perfectly healthy position wants to leverage its position, they will pay an open position fee which is acceptable. When the same users wants to deleverage, it's also acceptable to take the close position fee. However, just because the deleverage now puts the user in liquidation, why is it not acceptable to take the close position fees? I think this is how the system works. Just because user is liquidatable after the position fees shouldn't mean that the protocol should not take fees from their action.
You've created a valid escalation for 10 USDC!
To remove the escalation from consideration: Delete your comment.
You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.
SergeKireev
Escalate for 10 USDC
I think this issue is invalid or informational.
First of all, that's a great point. However, I feel like that the users should be aware of this already. Maker/taker fees are actually the open/close position fees which they should be aware all the time. So, if a user closes some of their position they should know that there is a fee that they need to pay. If that fee takes them to liquidation level, this should be ok because that's how the system works.
Here how I see the case: In a product with both open/close position fees; If a user with a perfectly healthy position wants to leverage its position, they will pay an open position fee which is acceptable. When the same users wants to deleverage, it's also acceptable to take the close position fee. However, just because the deleverage now puts the user in liquidation, why is it not acceptable to take the close position fees? I think this is how the system works. Just because user is liquidatable after the position fees shouldn't mean that the protocol should not take fees from their action.
Great point as well. This report does not argue that the protocol should not take a fee in that case, just that it should be taken at next settlement (like funding fees for example). The user reduces position, fee is deducted upon next settlement, and the user is never liquidatable, everybody is happy
The situation described in this report arises from the fact that collateral handling is not consistent: open/close fee is subtracted right away, and all other collateral/position modifications occur at next settlement
KenzoAgada
I think the various points above show why a medium severity is appropriate.
On one hand it is self inflicted, limited in scope, and depends upon a user not leaving any room for error (price movements/fees) at all, so I don't think it deserves high severity.
On the other hand, it is unintuitive that partially closing a dangerous position should make you liquidatable right away. I think there is a seeming inconsistency in the fact that position fees are deducted right away and can make a position that was already safe in the current version liquidatable. When withdrawing collateral there is a check that the user has enough collateral for maintenance of current version, but there's no such check in the scenario this issue describes.
I would maintain medium severity.
jacksanford1
@mstpr Thoughts on the above comments? Seems like it's the ordering of not taking the fee at next settlement?
mstpr
@jacksanford1 I would say it is probably not possible or not a secure way to do that. I would love to hear the protocol teams response on that since taking the fee after settlement or before settlement is their design choice. In my opinion, it looks too risky or undoable to take the fee after settlement so that's why they went for this implementation.
SergeKireev
@jacksanford1 @mstpr additional remarks:
-
It is understood that unifying this fees handling is a bit more than a trivial fix. A temporary solution could just to add an invariant which checks if the user becomes liquidatable by doing this call and revert in that case.
-
There is another really weird quirk with current implementation: If the user goes through the scenario outlined in the report, and closes his position entirely, the liquidator just takes some of the collateral without providing any utility.
Indeed the positions are already being closed, so this call is a noop: https://github.com/sherlock-audit/2023-05-perennial/blob/main/perennial-mono/packages/perennial/contracts/product/Product.sol#L362-L372
The liquidator does not help the protocol but takes a potentially hefty fee at the expense of an innocent user.
jacksanford1
@mstpr @SergeKireev I hear your points on the difficulty of the fix, but the difficulty of the fix should not have an effect on the severity of the issue.
Is there an argument for why this issue is invalid? @mstpr
And what is the argument for High vs. Medium @SergeKireev (after seeing Kenzo's latest comment)?
SergeKireev
@mstpr @SergeKireev I hear your points on the difficulty of the fix, but the difficulty of the fix should not have an effect on the severity of the issue.
Is there an argument for why this issue is invalid? @mstpr
And what is the argument for High vs. Medium @SergeKireev (after seeing Kenzo's latest comment)?
My argument for saying this should be High is an assessment of impact and probability:
- Impact: Loss of user funds for a large amount (The whole amount of collateral of the user).
- Probability:
If the user is on the brink of liquidation, closing or reducing her position seems like the most sensible action. With regards to latest @KenzoAgada comment, a user does not necessarily need to open a 50x (max leverage) position, he can also get into this situation simply by accumulating negative uPnl
mstpr
I am still seeing this issue as a design choice. Users are aware that there is a maker fee and it's updatable by the protocol team. Traders should always be cautious on their position and maintain it, that's their responsibility and that's a risk they took by taking the position. If user doesn't want to get liquidated, they can add some collateral and deleverage in the current design. It's not that the user has no other ways to dodge liquidation. @jacksanford1 @SergeKireev
jacksanford1
Thanks @SergeKireev @mstpr
I see it pretty clearly as a Medium. This is not a "blackhat can exploit the whole protocol" type of magnitude, it's just one user who is likely already near the brink of liquidation.
But I think the issue is real and worth potentially fixing (or at least knowing about) because it could have a very negative unintended effect for a user who wants to derisk their position.
jacksanford1
Result: Medium Unique Please see above comment for full reasoning.
sherlock-admin2
Escalations have been resolved successfully!
Escalation status:
- SergeKireev: rejected
- mstpr: rejected
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is known
A medium-severity business logic bug in Perennial V2 Update #1. Sherlock made it public on 30 June 2023. Found in the 79 audit contest (finding M-9).
About Business logic
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.