Skip to content
Business logic Audit finding Advanced

The implementation of `payExecutionFee()` didn't take `EIP-150` into consideration. Keepers can steal additional execution fee from users

The gas accounting in the execution fee calculation did not account for EIP-150's 63/64 rule, allowing a keeper to manipulate the gas limit and claim more execution fee than actually used, potentially draining the user's entire fee.

blackhole Follow with nikhil840096

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2024-05-elfi-protocol-judging/issues/95

Found by

blackhole, nikhil840096

Summary

The implementation of processExecutionFee() didn't take EIP-150 into consideration. Keepers can steal additional execution fee from users

Vulnerability Detail

The issue arises on L18 of GasProcess.sol:processExecutionFee(), as it's an external function, callingprocessExecutionFee() is subject to EIP-150. Only 63/64 gas is passed to the GasProcess sub-contract(external library), and the remaning 1/64 gas is reserved in the caller contract which will be refunded to keeper after the execution of the whole transaction. But calculation of usedGas includes this portion of the cost as well.

A malicious keeper can exploit this issue to drain out all execution fee, regardless of the actual execution cost. Let's take executeMintStakeToken() operation as an example to show how it works:

Solidity
executionFeeUserHasPaid = 200K Gwei
tx.gasprice = 1 Gwei
actualUsedGas = 100K

actualUsedGas is the gas cost since startGas(L76 of StakeFacet .sol) but before calling processExecutionFee()(L88 of StakeFacet.sol)

Let's say, the keeper sets tx.gaslimit to make

Solidity
startGas = 164K

Then the calculation of usedGas , L18 of GasProcess.sol, would be

Solidity
uint256 usedGas= cache.startGas- gasleft() = 164K - (164K - 100K) * 63 / 64 = 101K

and

Solidity
executionFeeForKeeper = 101K * tx.gasprice = 101K * 1 Gwei = 101K Gwei
refundFeeForUser = 200K - 101K = 99K Gwei

As setting of tx.gaslimit doesn't affect the actual gas cost of the whole transaction, the excess gas will be refunded to msg.sender. Now, the keeper increases tx.gaslimit to make startGas = 6500K, the calculation of usedGas would be

Solidity
uint256 usedGas= cache.startGas- gasleft() = 6500K - (6500K - 100K) * 63 / 64 = 200K

and

Solidity
executionFeeForKeeper = 200K * tx.gasprice = 200K * 1 Gwei = 200K Gwei
refundFeeForUser = 200K - 200K = 0 Gwei

We can see the keeper successfully drain out all execution fee, the user gets nothing refunded.

Impact

Keepers can steal additional execution fee from users.

Code Snippet

https://github.com/sherlock-audit/2024-05-elfi-protocol/blob/main/elfi-perp-contracts/contracts/process/GasProcess.sol#L18C17-L18C25

Tool used

Manual Review

Recommendation

Diff
    function processExecutionFee(PayExecutionFeeParams memory cache) external {
-        uint256 usedGas = cache.startGas - gasleft();
+       uint256 usedGas = cache.startGas - gasleft() * 64 / 63;
        uint256 executionFee = usedGas * tx.gasprice;
        uint256 refundFee;
        uint256 lossFee;

Discussion

sherlock-admin2

The protocol team fixed this issue in the following PRs/commits: https://github.com/0xCedar/elfi-perp-contracts/pull/50

sherlock-admin2

The Lead Senior Watson signed off on the fix.

The original report as published on Sherlock, by blackhole. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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 external call to processExecutionFee() is subject to EIP-150, so only 63/64 of gas is forwarded, but the used gas calculation does not compensate for this, allowing manipulation of the gas limit to inflate the calculated used gas.

Impact

Keepers can steal additional execution fee from users, potentially draining the entire fee.

What the attacker needed

Keeper role; user pays execution fee

The fix

The issue was fixed by adjusting the used gas calculation to multiply gasleft() by 64/63, compensating for the EIP-150 rule.

How it was found

  1. A keeper initiates a transaction that calls executeMintStakeToken(), which later calls processExecutionFee().
  2. The keeper sets a high transaction gas limit, which increases the startGas value recorded before the external call.
  3. Due to EIP-150, only 63/64 of the gas is forwarded to processExecutionFee(), so the gasleft() inside is higher than it would be without the rule.
  4. The usedGas calculation subtracts the higher gasleft() from startGas, resulting in a larger usedGas than the actual gas consumed.
  5. The keeper receives an execution fee based on the inflated usedGas, potentially consuming the entire fee paid by the user.

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 smart contract calls another contract, only 63/64 of the remaining gas is forwarded due to a rule called EIP-150. The fee calculation here ignored that, so a keeper could set a high gas limit and make it look like more gas was used than really was, taking more of the user's fee.

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.

More by blackhole

View all

2 more disclosed reports, best first.

More from Elfi

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.