TriggerOrder.notionalValue() Using the wrong latestPositionLocal to calculate the value causes the user to overpay fees
The TriggerOrder contract calculated the notional value for interface fees using the latest position instead of the current position, which could cause users to overpay fees when closing a position with a pending decrease. An attacker could exploit this to make victims pay more fees, but the impact is limited to fee overpayment.
Source: https://github.com/sherlock-audit/2024-08-perennial-v2-update-3-judging/issues/62
Found by
bin2chen
Summary
When TriggerOrder is executed, notionalValue() * order.interfaceFee.amount is used to calculate the cost
Where notionalValue() calculates the price by closing the position size
The current use of latestPositionLocal is wrong, currentPositionLocal should be used.
Root Cause
When TriggerOrder.delta == MAGIC_VALUE_CLOSE_POSITION, use _position() to calculate the number of closed positions.
_position() use: market.positions(account).maker/long/short; so use: latestPositionLocal
But when the order is actually executed in market.sol: market.update(maker=0) , currentPositionLocal is used to calculate the delta.
Market.sol#L235
delta = | 0 - currentPositionLocal.maker|
These two values are not the same
Internal pre-conditions
No response
External pre-conditions
No response
Attack Path
No response
Impact
If currentPositionLocal < latestPositionLocal, then the user pay more fees.
PoC
No response
Mitigation
function _position(IMarket market, address account, uint8 side) private view returns (UFixed6) {
Position memory current = market.positions(account);
+ Order memory pending = market.pendings(account);
+ current.update(pending)
if (side == 4) return current.maker;
else if (side == 5) return current.long;
else if (side == 6) return current.short;
revert TriggerOrderInvalidError();
}
Discussion
arjun-io
This is valid in that the calculation for the current position might be off, but instead of using the pending and updating, we need to actually calculate the closeable amount.
For example, if there is a current position of 5 long, and a pending open of 10 - passing in the close magic value will only close 5 (not 15). if there is a current position of 5 long, and a pending close of 3 - passing in the close magic value will only close 2 (this is not captured by the current logic).
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/equilibria-xyz/perennial-v2/pull/455
panprog
While the fix solves the issue described here (pending position decrease makes fee incorrect), the opposite case is still/now wrong (pending position increase makes fee incorrect / reverts the execution). So example scenario:
- User has position = 100
- User adds trigger order to close position
- User increases position to 150
- Trigger order executes while increase is still pending: it charges a fee of
150 * interfaceFeeand tries tomarket.update(0,0,0), which reverts, because it tries to decrease full position of 150, but only 100 is closable due to 50 still pending.
To fully fix this one:
- Use
MAGIC_VALUE_FULLY_CLOSED_POSITIONwhen executing market.update for orders withdelta = MAGIC_VALUE_CLOSE_POSITION - For the interface fee - you have to calculate actual closable instead of latest or current position:
closable = latestPosition - pending.neg
EdNoepel
Pending position increase is not possible with the current implementation.
- The problem with
MAGIC_VALUE_FULLY_CLOSED_POSITIONis that it is only designed to prevent over-closing the position. If we have a position and a pending positive position, it will not actually 0 the position as desired. Created this branch to illustrate the behavior with your recommendation. For this use case, a revert is more desirable. - The current implementation uses 0 such that closing with a pending positive position will revert instead of silently leaving the position open. This provides more desirable UX/DX. I created this branch to show the only affected unit test is one which reverts as an integration test.
Note that changing the behavior of Market's MAGIC_VALUE_FULLY_CLOSED_POSITION seems out-of-scope for this release.
panprog
Ok, got it. So it's a design choice, so there is no issue with it. This issue is fixed then.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the fee calculation used the latest position instead of the current position when determining the notional value for closing orders.
What the attacker needed
User has a position; user has a pending decrease in position; user places a trigger order to close position
The fix
The protocol team fixed this issue in a pull request, but a later comment noted that the opposite case (pending increase) still causes incorrect fees or reverts, and a full fix would require calculating the actual closable amount.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
This is a smart contract bug where a fee calculation used the wrong position size. When a user closes a position, the fee should be based on the current position, but the code used a newer, larger position value, so the user paid more than necessary.
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.