Partial Fills before amendOrder TX exposes Users to unintended Risks
The amendLimitOrder function does not check whether an order has been partially filled before updating its amount, so a user amending only the expiry can end up with a larger remaining order than intended. An attacker or normal market activity could cause the user to take on more financial exposure than they wanted.
Originally disclosed on Code4rena
Submitted by VinciGearHead
perps/PerpManager.sol #L328-L340
The implementation of the AmendLimitOrder opens a vulnerability that allows a user’s amendment transaction to be executed on an outdated order state, leading to users silently taking on or creating more additional exposure than intended, producing real liquidation/financial risk.
The issue stems from the fact that amendLimitOrder does not validate whether the order has been partially filled before applying modifications the specified baseAmount. In a Perps environment, transactions are processed asynchronously and miners/sequencers may include other trades before the amendLimtiOrder tx is executed. As a result, by the time the amendment transaction reaches execution, the order might have already been partially filled, hence the system might think the user wants to increase their order size.
To amend an order we call this external amendLimitOrder function:
filename : PerpManager.sol (excerpt)
function amendLimitOrder(address account, AmendLimitOrderArgs calldata args)
external
onlySenderOrOperator(account, PerpsOperatorRoles.PLACE_ORDER)
onlyActiveProtocol
returns (int256 collateralDelta)
{
ClearingHouse storage clearingHouse = StorageLib.loadClearingHouse();
collateralDelta = clearingHouse.market[args.asset].amendLimitOrder(account, args, BookType.STANDARD);
StorageLib.loadCollateralManager().handleCollateralDelta({account: account, collateralDelta: collateralDelta});
}
which eventually calls the internal _processAmend function. Now the implementation of the _processAmend determines which type of amendment should take place depending on the user input.
filename : OrderLib.sol (excerpt)
function _processAmend(Book storage ds, Order storage order, AmendLimitOrderArgs calldata args)
internal
returns (int256 notionalDelta, int256 collateralDelta)
{
if (
args.expiryTime.isExpired()
|| args.baseAmount < StorageLib.loadBookSettings(ds.config.asset).minLimitOrderAmountInBase
) {
revert InvalidAmend();
} else if (order.side != args.side || order.price != args.price) {
// change place in book
return _executeAmendNewOrder(ds, order, args);
} else {
// change amount
@>> return _executeAmendAmount(ds, order, args);
}
}
For cases to change not too sensitive properties of the order like (size, Expiry), the _executeAmendAmount() is invoked, which sets the order size directly to the new baseAmount , which can be problematic in some cases.
/// @dev Performs the updating of an amended order with a new amount
function _executeAmendAmount(Book storage ds, Order storage order, AmendLimitOrderArgs calldata args)
internal
returns (int256 notionalDelta, int256 collateralDelta)
{
......................
if (order.reduceOnly != args.reduceOnly) {
if (args.reduceOnly) {
StorageLib.loadMarket(ds.config.asset).linkReduceOnlyOrder(
order.owner, order.subaccount, args.orderId, ds.config.bookType
);
} else {
StorageLib.loadMarket(ds.config.asset).unlinkReduceOnlyOrder(
order.owner, order.subaccount, args.orderId, ds.config.bookType
);
}
}
@>> order.amount = args.baseAmount;
order.reduceOnly = args.reduceOnly;
order.expiryTime = args.expiryTime;
}
Scenario
- Alice places an order to buy
10 ETHat$2,000. - Alice now decides to change the
expirywhile the order is still pending, but as of now the order hasn’t been filled at all. - Before Alice’s
amendLimitOrdertransaction is mined,9 ETHfrom the original order was now partially filled. - Alice’s
amendLimitOrderis processed and still assumes the original10 ETHsize, only modifying the expiry.
Resulting State
9 ETHalready filledRemaining orderincorrectly updated to10 ETH(instead of1 ETH)
Alice has unintentionally taken on almost double the intended financial exposure:
- Initial intended exposure:
10 ETH - Actual exposure after race:
19 ETH
Recommended mitigation steps
Before applying an amendment in the size of the order, we need to know that the order’s filled amount has not changed since tx submission, this way we can protect user against unwanted trades.
View detailed Proof of Concept
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 amend function overwrites the order amount with the new base amount without accounting for the quantity already filled.
Impact
The user could unintentionally take on almost double the intended financial exposure, leading to real liquidation risk.
What the attacker needed
User places a limit order; user submits an amendment; partial fill occurs before amendment is mined
The fix
The amendment should verify that the order's filled amount has not changed since the transaction was submitted before applying the new size.
How it was found
- Alice places a limit buy order for 10 ETH at $2,000.
- Alice submits an amendment to change only the expiry time.
- Before the amendment is mined, 9 ETH of the original order gets filled.
- The amendment executes and sets the remaining order amount to 10 ETH instead of 1 ETH.
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.
When you change an order on a trading platform, the system should check if part of it has already been filled. Here, it does not, so if someone else's trade fills part of your order just before your change is processed, your order can become bigger than you meant. This can lead to losing more money than planned.
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.