Missing compensation for the ````21,000```` intrinsic gas cost
The executeOrder function measures gas from inside the function, missing the 21,000 intrinsic gas cost charged before execution. As a result, the keeper is undercompensated for each transaction.
Source: https://github.com/sherlock-audit/2024-05-elfi-protocol-judging/issues/142
Found by
KingNFT
Summary
Every EVM transaction (on both L1 and L2) has an immediate 21,000 intrinsic gas cost, it's charged before any execution of smart contract code. The current impletmentation is missing to compensate this portion of gas cost, the keeper would suffer lost on each transaction.
Vulnerability Detail
The current startGas(L67) can't account for the 21,000 intrinsic gas cost.
File: contracts\facets\OrderFacet.sol
66: function executeOrder(uint256 orderId, OracleProcess.OracleParam[] calldata oracles) external override {
// @audit at least 21,000+ gas is consumed before this line
67: uint256 startGas = gasleft();
68: RoleAccessControl.checkRole(RoleAccessControl.ROLE_KEEPER);
69: Order.OrderInfo memory order = Order.get(orderId);
70: if (order.account == address(0)) {
71: revert Errors.OrderNotExists(orderId);
72: }
73: OracleProcess.setOraclePrice(oracles);
74: OrderProcess.executeOrder(orderId, order);
75: OracleProcess.clearOraclePrice();
76: GasProcess.processExecutionFee(
77: GasProcess.PayExecutionFeeParams(
78: order.isExecutionFeeFromTradeVault
79: ? IVault(address(this)).getTradeVaultAddress()
80: : IVault(address(this)).getPortfolioVaultAddress(),
81: order.executionFee,
82: startGas,
83: msg.sender,
84: order.account
85: )
86: );
87: }
Impact
The keeper will suffer continuing 21,000 intrinsic gas losses on each transaction
Code Snippet
Tool used
Manual Review
Recommendation
File: contracts\facets\OrderFacet.sol
66: function executeOrder(uint256 orderId, OracleProcess.OracleParam[] calldata oracles) external override {
-67: uint256 startGas = gasleft();
+67: uint256 startGas = gasleft() + 30000; // @audit 21000 intrinsic gas plus 9000 extra gas for calldata and facet lookup in diamond fallback() function
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/0xCedar/elfi-perp-contracts/pull/48
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
The gas measurement starts inside the function, so it excludes the intrinsic gas cost of the transaction.
Impact
The keeper suffers a continuing loss of 21,000 gas per transaction.
What the attacker needed
Keeper role required
The fix
The issue was fixed by adding 30,000 to the startGas value to cover the intrinsic gas and extra costs.
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.
Every Ethereum transaction costs at least 21,000 gas just to be included, before any code runs. The contract only measures gas from the start of the function, so it doesn't count that base cost. The person running the transaction (the keeper) ends up paying that extra gas themselves.
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.