Skip to content
Business logic Audit finding Intermediate

Improper tokenGasPrice design can overcharge user for the gas cost by a huge margin

The gas fee calculation for token transfers uses an integer tokenGasPrice, which for tokens with few decimals (like WBTC or USDC) can result in a gas fee that is orders of magnitude higher than the actual gas cost. An attacker could exploit this to overcharge users by a huge margin.

WatchPug Follow

Originally disclosed on Code4rena

Submitted by WatchPug, also found by cmichel and hyh

LiquidityPool.sol#L330-L337

uint256 totalGasUsed = initialGas - gasleft();
totalGasUsed = totalGasUsed + tokenManager.getTokensInfo(tokenAddress).transferOverhead;
totalGasUsed = totalGasUsed + baseGas;

uint256 gasFee = totalGasUsed * tokenGasPrice;
gasFeeAccumulatedByToken[tokenAddress] = gasFeeAccumulatedByToken[tokenAddress] + gasFee;
gasFeeAccumulated[tokenAddress][_msgSender()] = gasFeeAccumulated[tokenAddress][_msgSender()] + gasFee;
amountToTransfer = amount - (transferFeeAmount + gasFee);

When the Executor calls sendFundsToUser(), the tokenGasPrice will be used to calculate the gas fee for this transaction and it will be deducted from the transfer amount.

However, since tokenGasPrice is uint256, the smallest chargeable amount is 1 wei Token for 1 gas. But there are tokens like WBTC (decimals = 8) or USDC (decimals = 6), for these tokens, even 1 wei of the token can be worth a lot of gas, if the tokenGasPrice is set to 1, gasFee will far more than the actual cost; if it’s set to 0, gasFee can only be 0.

Proof of Concept

Given:

  • baseGas = 21000
  • tokenGasPrice for WBTC = 1 wei
  • transferFeeAmount = 0
  • 1 WBTC = 20,000 MATIC
  • Alice send 0.1 WBTC to Bob’s address on Polygon
  • Executor calls sendFundsToUser() with tokenGasPrice = 1 on Polygon, totalGasUsed = 42000 and the gas price is 30G wei, Executor paid 0.00126 MATIC for gas.
uint256 gasFee = 42000 * 1;
...
amountToTransfer = 10000000 - (0 + 42000);
  1. Bob received 0.09958 WBTC, and paid 0.00042 WBTC for the gas, the gas fee was overcharged by 6666 times.

Recommended Mitigation Steps

Consider changing tokenGasPrice to a value with decimals of 18 and it should be used like this:

uint256 gasFee = totalGasUsed * tokenGasPrice / 1e18;

ankurdubey521 (Biconomy) acknowledged and commented:

I’m not sure I agree with the recommendation since If a token atom’s value exceeds the gas paid for the transaction, it would still be truncated if we send a tokenGasPrice multiplied by 10e18 and divide it in the contract.

But this is a great catch, I think the bigger issue here is that for certain tokens it is not feasible to charge gas fee on a per transaction basis, we’ll have to think about how to mitigate this.

pauliax (judge) decreased severity to Medium and commented:

The sponsor better knows the design and intentions of the system, and they claim to be dealing with the token atoms on a contract level. However, I would still like to emphasize possible risks with different tokens and decimals. It is a common issue, so I would like to group similar issues together and assign them a severity of Medium.


The original report as published on Code4rena, by WatchPug. 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

Likely the tokenGasPrice is an integer with no decimal precision, so for tokens with few decimals the smallest unit is too large relative to gas costs.

Impact

Users could be overcharged for gas fees by a huge margin when transferring tokens with few decimals, potentially losing a significant portion of the transferred amount.

What the attacker needed

Token with low decimals (e.g., WBTC, USDC); tokenGasPrice set to 1 wei; user initiates a transfer

The fix

The tokenGasPrice should be changed to a value with 18 decimals and the gas fee calculated as totalGasUsed * tokenGasPrice / 1e18.

How it was found

  1. Alice sends 0.1 WBTC to Bob on Polygon.
  2. Executor calls sendFundsToUser with tokenGasPrice = 1, totalGasUsed = 42000, gas price 30 Gwei.
  3. Gas fee is calculated as 42000 * 1 = 42000 wei WBTC.
  4. Bob receives 0.09958 WBTC and pays 0.00042 WBTC for gas, overcharged by 6666 times.

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 you send tokens, a small fee is taken to cover the cost of processing the transaction. This report shows that for tokens like WBTC, which have few decimal places, the fee calculation can be wrong and charge you thousands of times more than the real cost. The problem is that the fee is calculated using whole numbers, so you can't set a small enough value to match the actual cost.

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 WatchPug

View all

197 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.

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.