Gas pricing can be used to extort funds from users of SChain owner
The refund calculation in CommunityPool.refundGasByUser uses tx.gasprice, which the node submitting the transaction controls, allowing a malicious node to set an excessively high gas price and drain the user's or SChain owner's balance. The user may also be locked out of further exits if their balance becomes insufficient.
Originally disclosed on Code4rena
Submitted by kirk-baird, also found by leastwood
https://github.com/skalenetwork/ima-c4-audit/blob/main/contracts/mainnet/CommunityPool.sol#L82-L112
Impact
The function refundGasByUser() can be exploited by the message sender to drain nodes and SChain owners of their balances when processing incoming messages.
When a node collates a set of exits from an SChain to Ethereum, they are submitted on-chain via MessageProxyForMainnet.sol. For each message to a registered contract the user is required to pay for the refund via CommunityPool.refundGasByUser().
The issue occurs in CommunityPool.refundGasByUser() as the amount to be refunded is calculated as uint amount = tx.gasprice * gas;, where gas is the gas used by the message. Since tx.gasprice is set by the node and there is no upper bounds on the price. Since EIP1559 the gas price is BaseFee + Tip and although Base is predetermined Tip is any arbitrary non-zero integer.
The attack is for a node to set an excessively high tx.gasprice which will be refunded out of the balance of the user who initiated the outgoing transaction or if that user has insufficient balance then from the SChain owner. Since the node submitting the transaction is refunded for their gas they do not lose from setting a higher gas price.
The impact of the attack is that the user requesting the exit and/or the SChain owner may have their ETH balances depleted to refund the submitter. The impact is worsened as if the user has insufficient balance a message will be sent to the SChain preventing them from making further exits until they have sufficient balance.
Note a similar issue may be seen in IWallets.refundGasBySchain() depending on how the gas calculations are performed (they are not in scope but the TestWallet also uses tx.gasprice in the same manner).
Proof of Concept
Processing incoming messages in MessageProxyForMainnet.sol
for (uint256 i = 0; i < messages.length; i++) {
gasTotal = gasleft();
if (isContractRegistered(bytes32(0), messages[i].destinationContract)) {
address receiver = _getGasPayer(fromSchainHash, messages[i], startingCounter + i);
_callReceiverContract(fromSchainHash, messages[i], startingCounter + i);
notReimbursedGas += communityPool.refundGasByUser(
fromSchainHash,
payable(msg.sender),
receiver,
gasTotal - gasleft() + additionalGasPerMessage
);
} else {
_callReceiverContract(fromSchainHash, messages[i], startingCounter + i);
notReimbursedGas += gasTotal - gasleft() + additionalGasPerMessage;
}
}
Refunding gas in CommunityPool.sol
function refundGasByUser(
bytes32 schainHash,
address payable node,
address user,
uint gas
)
external
override
onlyMessageProxy
returns (uint)
{
require(node != address(0), "Node address must be set");
if (!activeUsers[user][schainHash]) {
return gas;
}
uint amount = tx.gasprice * gas;
if (amount > _userWallets[user][schainHash]) {
amount = _userWallets[user][schainHash];
}
_userWallets[user][schainHash] = _userWallets[user][schainHash] - amount;
if (!_balanceIsSufficient(schainHash, user, 0)) {
activeUsers[user][schainHash] = false;
messageProxy.postOutgoingMessage(
schainHash,
schainLinks[schainHash],
Messages.encodeLockUserMessage(user)
);
}
node.sendValue(amount);
return (tx.gasprice * gas - amount) / tx.gasprice;
}
Recommended Mitigation Steps
One solution to avoid excessive over refunding of gas fees is to use a gas price oracle rather than tx.gasprice.
An alternate solution is to set a maximum gas price and have some incentives for the node submitting at a gas price below the maximum.
Resolved via https://github.com/skalenetwork/IMA/pull/1165/
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 refund amount is calculated as tx.gasprice * gas, and tx.gasprice is set by the node with no upper bound.
Impact
The user requesting the exit and/or the SChain owner may have their ETH balances depleted, and if the user's balance is insufficient a message is sent to the SChain preventing further exits until they have sufficient balance.
What the attacker needed
Attacker controls a node submitting messages; victim has an active user wallet or is SChain owner
The fix
The refund should use a gas price oracle rather than tx.gasprice, or a maximum gas price should be set with incentives for nodes to submit below it; the report states the issue was resolved via a pull request.
How it was found
- A node collates exits from an SChain to Ethereum and submits them on-chain via MessageProxyForMainnet.
- For each message to a registered contract, CommunityPool.refundGasByUser is called to refund the node.
- The node sets an excessively high tx.gasprice, which is used in the refund calculation.
- The refund is paid from the user's balance, or from the SChain owner's balance if the user has insufficient funds.
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 message is sent from an SChain to Ethereum, a node pays the Ethereum gas fee and is later refunded from the user's or SChain owner's balance. The refund amount is calculated using the gas price the node chose, and the node can choose an arbitrarily high price. A malicious node can therefore make the refund very large and drain the victim's funds.
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.