In certain cases, users are unable to settle their orders with the PartialFill trade type
A smart contract bug in order settlement logic fails to account for previously filled amounts when checking a ReduceOnly order, causing legitimate partial settlements to revert. An attacker could exploit this to cause a denial of service, forcing users to waste gas fees.
Source: https://github.com/sherlock-audit/2024-02-perpetual-judging/issues/95
Found by
ether_sky
Summary
There are 2 trade types available: FoK or PartialFill.
Users have the option to partially settle their orders.
However, in some cases, they can't settle their `orders.
Vulnerability Detail
A user creates an order with the PartialFill trade type and a size of S.
Initially, he settles 50% of this order.
At this point, the totalFilledAmount of this order has a value of S/2.
function _fillTakerOrder(
InternalContext memory context,
SettleOrderParam memory settleOrderParams
) internal returns (InternalWithdrawMarginParam memory, uint256) {
_getOrderGatewayV2Storage().totalFilledAmount[takerOrder.getKey()] += settleOrderParams.fillAmount;
}
After some time, the user attempts to settle the remaining 50% of that order.
In the _verifyOrder function, we check whether there is enough positionSize available to settle this order if the action of the order is ReduceOnly.
However, the issue arises from not accounting for the previously settled amount.
function _verifyOrder(IVault vault, Order memory order, uint256 fillAmount) internal view {
uint256 totalFilledAmount = getOrderFilledAmount(order.owner, order.id);
if (fillAmount > openAmount - totalFilledAmount) {
revert LibError.ExceedOrderAmount(order.owner, order.id, totalFilledAmount);
}
if (order.action == ActionType.ReduceOnly) {
int256 ownerPositionSize = vault.getPositionSize(order.marketId, order.owner);
if (order.amount * ownerPositionSize > 0) {
revert LibError.ReduceOnlySideMismatch(order.owner, order.id, order.amount, ownerPositionSize);
}
if (openAmount > ownerPositionSize.abs()) { // @audit, here
revert LibError.UnableToReduceOnly(order.owner, order.id, openAmount, ownerPositionSize.abs());
}
}
}
In the initial settlement, half of the order was settled.
After the settlement, the positionSize decreases by S/2 also and there are only S/2 remaining in the order.
Therefore, we should compare S/2 with the current positionSize.
However, we compare S again without accounting for the previously settled amount.
As a result, the current positionSize might be lower than S, leading to the potential reversal of the settlement.
Impact
This is a DoS and users can lose funds as gas fees.
Code Snippet
https://github.com/sherlock-audit/2024-02-perpetual/blob/main/perp-contract-v3/src/orderGatewayV2/OrderGatewayV2.sol#L336 https://github.com/sherlock-audit/2024-02-perpetual/blob/main/perp-contract-v3/src/orderGatewayV2/OrderGatewayV2.sol#L513-L515
Tool used
Manual Review
Recommendation
function _verifyOrder(IVault vault, Order memory order, uint256 fillAmount) internal view {
uint256 totalFilledAmount = getOrderFilledAmount(order.owner, order.id);
if (fillAmount > openAmount - totalFilledAmount) {
revert LibError.ExceedOrderAmount(order.owner, order.id, totalFilledAmount);
}
if (order.action == ActionType.ReduceOnly) {
int256 ownerPositionSize = vault.getPositionSize(order.marketId, order.owner);
if (order.amount * ownerPositionSize > 0) {
revert LibError.ReduceOnlySideMismatch(order.owner, order.id, order.amount, ownerPositionSize);
}
- if (openAmount > ownerPositionSize.abs()) {
+ if (openAmount - totalFilledAmount > ownerPositionSize.abs()) {
revert LibError.UnableToReduceOnly(order.owner, order.id, openAmount, ownerPositionSize.abs());
}
}
}
Discussion
sherlock-admin2
1 comment(s) were left on this issue during the judging contest.
santipu_ commented:
Medium
sherlock-admin4
The protocol team fixed this issue in the following PRs/commits: https://github.com/perpetual-protocol/perp-contract-v3/pull/6
sherlock-admin3
The Lead Senior Watson signed off on the fix.
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
The _verifyOrder function compares the full order amount (openAmount) with the current position size instead of the remaining unfilled amount (openAmount - totalFilledAmount) when validating ReduceOnly orders.
Impact
This is a denial of service that prevents users from settling orders and causes them to lose funds as gas fees.
What the attacker needed
User has an open order with PartialFill type; Order action is ReduceOnly; Partial settlement has already occurred
The fix
The issue was fixed by subtracting totalFilledAmount from openAmount in the comparison.
How it was found
- Create an order with PartialFill trade type and size S.
- Settle 50% of the order, updating totalFilledAmount to S/2.
- Attempt to settle the remaining 50% of the order.
- The settlement reverts because the check compares the full size S with the reduced position size.
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 bug in a smart contract that handles trading orders. When a user tries to partially settle an order, the contract incorrectly checks the full order size instead of the remaining size, so the settlement fails and the user loses gas fees.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.