Users can gas grief or completely block keepers from executing orders
The protocol refunds excess gas fees to users by sending ether with unlimited gas, allowing a malicious user contract to consume all gas or revert, causing keeper transactions to fail or become permanently blocked.
ZeroTrust Follow with mstpr-brainbot
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-05-elfi-protocol-judging/issues/107
Found by
ZeroTrust, mstpr-brainbot
Summary
When keepers cancel users' orders, the refund gas is sent back to the user. If the user has a fallback function that specifically reverts or consumes excessive gas, the keeper's transaction can fail or exhaust its gas, causing economic harm to the keeper.
Vulnerability Detail
Almost every facet function uses the following pattern. For example:
function executeOrder(uint256 orderId, OracleProcess.OracleParam[] calldata oracles) external override {
uint256 startGas = gasleft();
RoleAccessControl.checkRole(RoleAccessControl.ROLE_KEEPER);
Order.OrderInfo memory order = Order.get(orderId);
if (order.account == address(0)) {
revert Errors.OrderNotExists(orderId);
}
OracleProcess.setOraclePrice(oracles);
OrderProcess.executeOrder(orderId, order);
OracleProcess.clearOraclePrice();
// @review gas sent to keeper and refund sent to user and loss is accounted if there are any
GasProcess.processExecutionFee(
GasProcess.PayExecutionFeeParams(
order.isExecutionFeeFromTradeVault
? IVault(address(this)).getTradeVaultAddress()
: IVault(address(this)).getPortfolioVaultAddress(),
order.executionFee,
startGas,
msg.sender,
order.account
)
);
}
In the GasProcess::processExecutionFee function, if there is an excess amount to be refunded, the ether is sent to the user with infinite gas:
function processExecutionFee(PayExecutionFeeParams memory cache) external {
// ...
VaultProcess.withdrawEther(cache.keeper, executionFee);
if (refundFee > 0) {
VaultProcess.withdrawEther(cache.account, refundFee);
}
// ...
}
Users can perform two malicious actions:
- Spend all the remaining gas from the keeper's transaction, causing gas griefing and loss of funds for the keeper.
- If this is a cancel transaction, which can only be triggered by the keeper, or any action the user does not want the keeper to succeed in, the user can set the fallback function of the contract account to revert, blocking the keeper from calling the transaction indefinitely.
Impact
Since this involves permanent blocking and loss of funds for keepers I will label this as high.
Code Snippet
Tool used
Manual Review
Recommendation
There can be several fixes here but the best is probably to check if the account has code or not and send ether accordingly.
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/0xCedar/elfi-perp-contracts/pull/49
sherlock-admin2
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
Likely the refund ether transfer to the user is made without limiting gas, so a malicious contract can consume all gas or revert.
Impact
Keepers can suffer gas griefing and loss of funds, and in cancel transactions the keeper can be permanently blocked from executing, causing economic harm.
What the attacker needed
User is a smart contract; user can set a malicious fallback; keeper executes or cancels order
The fix
The issue was fixed by checking whether the account has code and sending ether accordingly.
How it was found
- A user places an order with an execution fee.
- A keeper calls executeOrder or a cancel function.
- The protocol processes the execution fee and attempts to refund excess to the user.
- The user's contract fallback reverts or consumes excessive gas, causing the keeper's transaction to fail.
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 (a bot that executes orders) cancels an order, any leftover gas fee is sent back to the user. If the user is a smart contract with a fallback function that uses a lot of gas or always fails, the keeper's whole transaction can run out of gas or fail. This means the keeper loses money and cannot complete the cancellation.
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.