Keepers can lose compensation fee
A keeper's compensation fee could be reduced to zero by a user front-running the keeper's transaction and updating the order with a lower maxFee. This allowed the user to grief the keeper, causing the keeper to lose the fee.
Source: https://github.com/sherlock-audit/2024-08-perennial-v2-update-3-judging/issues/79
Found by
Nyx
Summary
Vulnerability Detail
When the keeper fulfills the order, they receive a compensation fee from the order owner. The user specifies a maxFee in the order. The _handleKeeperFee function calculates the fee for the compensation, and the keeper receives the lesser of the calculated fee or the maxFee set by the user.
function _raiseKeeperFee(
UFixed18 amount,
bytes memory data
) internal virtual override returns (UFixed18) {
(IMarket market, address account, UFixed6 maxFee) = abi.decode(data, (IMarket, address, UFixed6));
UFixed6 raisedKeeperFee = UFixed6Lib.from(amount, true).min(maxFee);
_marketWithdraw(market, account, raisedKeeperFee);
return UFixed18Lib.from(raisedKeeperFee);
}
The problem is that the user can front-run the keepers' tx and change the maxFee to 0 to grief the keeper.
function placeOrder(IMarket market, uint256 orderId, TriggerOrder calldata order) external {
_placeOrder(market, msg.sender, orderId, order);
}
The user can call the placeOrder function using the same orderId as before and modify the order with a lower maxFee amount.
Impact
The keeper can lose a fee.
Code Snippet
POC:
Manager_Arbitrum.ts
it('test maxFee', async () => {
// userA places a 5k maker order
// maxFee is 0.88e18
const orderId = await placeOrder(userA, Side.MAKER, Compare.LTE, parse6decimal('3993.6'), parse6decimal('55'))
expect(orderId).to.equal(BigNumber.from(501))
const order = {
side: Side.MAKER,
comparison: Compare.LTE,
price: parse6decimal('3993.6'),
delta: parse6decimal('55'),
maxFee: utils.parseEther('0'),
isSpent: false,
referrer: constants.AddressZero,
...NO_INTERFACE_FEE,
}
//before the keepers tx, userA changes the maxFee
await expect(manager.connect(userA).placeOrder(market.address, orderId, order, TX_OVERRIDES))
await commitPrice(parse6decimal('2800'))
await executeOrder(userA, 501)
})
Tool used
Manual Review
Recommendation
Add a minimum fee parameter to the executeOrder function to ensure that the compensation fee is not less than what keepers want.
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/equilibria-xyz/perennial-v2/pull/453
panprog
Fix review:
Still not fixed. The order's maxFee can still be reduced, because the check only requires a different fee if previous order is not empty. The user can easily bypass this check by placing order with the same orderId twice in the same transaction:
- place empty order with the same maxFee as old order. The order is considered empty if
side=comparison=price=delta=0, somaxFeecan remain the same to successfully replace it. - place "old" order with smaller fee
EdNoepel
How would they empty the previous order though? An attempt to replace with an empty order with same maxFee would revert with TriggerOrderInvalidError. Example unit test: https://github.com/equilibria-xyz/perennial-v2/commit/86ee6d9a67e0e7c14aaa6708f71185f168b37b56
panprog
@EdNoepel Sorry, my bad, didn't check that store validates this. It's 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
The order's maxFee could be changed by the user after placement, and the keeper fee calculation used the current maxFee without any minimum guarantee.
Impact
The keeper could lose the compensation fee, as the fee is reduced to the new maxFee set by the user.
What the attacker needed
User can place orders; keeper executes orders; user can front-run keeper's transaction
The fix
The issue was fixed by adding a check that prevents reducing the maxFee when replacing an order, as confirmed in the discussion.
How it was found
- User places an order with a certain maxFee.
- Keeper prepares to execute the order.
- User front-runs the keeper's transaction by calling placeOrder with the same orderId and a lower maxFee (e.g., 0).
- Keeper executes the order and receives the reduced fee.
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 a keeper executes an order, they get paid a fee. The user who placed the order sets a maximum fee they are willing to pay. An attacker could watch for the keeper's transaction and quickly send their own transaction to lower that maximum fee to zero, so the keeper gets nothing. This is a griefing attack where the attacker doesn't gain but causes loss to the keeper.
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.