Skip to content
Smart contracts Audit finding Intermediate

FeeRefund.tokenGasPriceFactor is not included in signed transaction data allowing the submitter to steal funds

The tokenGasPriceFactor value used to calculate ERC20 gas refunds was not included in the signed transaction data, so a transaction submitter could change it after the owner signed and receive a larger refund, stealing the wallet's funds.

Ruhum Follow with V_B, adriro and 5 others

Originally disclosed on Code4rena

Submitted by Ruhum, also found by V_B, adriro, immeas, supernova, MalfurionWhitehat, cccz, and ladboy233

contracts/smart-contract-wallet/SmartAccount.sol#L288 contracts/smart-contract-wallet/SmartAccount.sol#L429-L444

The submitter of a transaction is paid back the transaction’s gas costs either in ETH or in ERC20 tokens. With ERC20 tokens the following formula is used: $(gasUsed + baseGas) * gasPrice / tokenGasPriceFactor$. baseGas, gasPrice, and tokenGasPriceFactor are values specified by the tx submitter. Since you don’t want the submitter to choose arbitrary values and pay themselves as much as they want, those values are supposed to be signed off by the owner of the wallet. The signature of the user is included in the tx so that the contract can verify that all the values are correct. But, the tokenGasPriceFactor value is not included in those checks. Thus, the submitter is able to simulate the tx with value $x$, get the user to sign that tx, and then submit it with $y$ for tokenGasPriceFactor. That way they can increase the actual gas repayment and steal the user’s funds.

Proof of Concept

In encodeTransactionData() we can see that tokenGasPriceFactor is not included:

    function encodeTransactionData(
        Transaction memory _tx,
        FeeRefund memory refundInfo,
        uint256 _nonce
    ) public view returns (bytes memory) {
        bytes32 safeTxHash =
            keccak256(
                abi.encode(
                    ACCOUNT_TX_TYPEHASH,
                    _tx.to,
                    _tx.value,
                    keccak256(_tx.data),
                    _tx.operation,
                    _tx.targetTxGas,
                    refundInfo.baseGas,
                    refundInfo.gasPrice,
                    refundInfo.gasToken,
                    refundInfo.refundReceiver,
                    _nonce
                )
            );
        return abi.encodePacked(bytes1(0x19), bytes1(0x01), domainSeparator(), safeTxHash);
    }

The value is used to determine the gas repayment in handlePayment() and handlePaymentRevert():

    function handlePayment(
        uint256 gasUsed,
        uint256 baseGas,
        uint256 gasPrice,
        uint256 tokenGasPriceFactor,
        address gasToken,
        address payable refundReceiver
    ) private nonReentrant returns (uint256 payment) {
        // uint256 startGas = gasleft();
        // solhint-disable-next-line avoid-tx-origin
        address payable receiver = refundReceiver == address(0) ? payable(tx.origin) : refundReceiver;
        if (gasToken == address(0)) {
            // For ETH we will only adjust the gas price to not be higher than the actual used gas price
            payment = (gasUsed + baseGas) * (gasPrice < tx.gasprice ? gasPrice : tx.gasprice);
            (bool success,) = receiver.call{value: payment}("");
            require(success, "BSA011");
        } else {
            payment = (gasUsed + baseGas) * (gasPrice) / (tokenGasPriceFactor);
            require(transferToken(gasToken, receiver, payment), "BSA012");
        }
        // uint256 requiredGas = startGas - gasleft();
        //console.log("hp %s", requiredGas);
    }

That’s called at the end of execTransaction():

            if (refundInfo.gasPrice > 0) {
                //console.log("sent %s", startGas - gasleft());
                // extraGas = gasleft();
                payment = handlePayment(startGas - gasleft(), refundInfo.baseGas, refundInfo.gasPrice, refundInfo.tokenGasPriceFactor, refundInfo.gasToken, refundInfo.refundReceiver);
                emit WalletHandlePayment(txHash, payment);
            }

As an example, given that:

  • gasUsed = 1,000,000
  • baseGas = 100,000
  • gasPrice = 10,000,000,000 (10 gwei)
  • tokenGasPriceFactor = 18

You get $(1,000,000 + 100,000) * 10,000,000,000 / 18 = 6.1111111e14$. If the submitter executes the transaction with tokenGasPriceFactor = 1 they get $1.1e16$ instead, i.e. 18 times more.

Recommended Mitigation Steps

tokenGasPriceFactor should be included in the encoded transaction data and thus verified by the user’s signature.

livingrockrises (Biconomy) confirmed


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

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 tokenGasPriceFactor field was omitted from the data hashed and signed by the wallet owner, so the contract did not verify it.

Impact

The submitter could increase the ERC20 gas repayment and steal the wallet owner's funds; the report gives an example where the refund is 18 times larger.

What the attacker needed

Wallet uses ERC20 gas refunds; attacker submits the transaction; owner signs the transaction

The fix

tokenGasPriceFactor should be added to the encoded transaction data so the owner's signature covers it.

How it was found

  1. The submitter prepares a transaction with a chosen tokenGasPriceFactor and gets the wallet owner to sign it.
  2. The submitter submits the same transaction but with a different, smaller tokenGasPriceFactor.
  3. The contract calculates the ERC20 refund using the altered factor, paying the submitter more than intended.

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.

A smart contract wallet lets someone else submit a transaction and pays them back for the gas it costs. The wallet owner signs the details to approve the amount, but one number used in the refund calculation was left out of what they signed. The submitter could change that number to make the refund much bigger and take the extra from the wallet.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by Ruhum

View all

55 more disclosed reports, best first.

More from Biconomy - Smart Contract Wallet contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.