PayGasFeeInZetaAndUpdateCctx() is prone to slippage, causing sender overpays the revert gas and lose returned funds
The gas payment function swaps tokens without slippage protection, so a price manipulation can inflate the gas cost. This can cause the sender to overpay or lose their entire refund if the transaction is aborted.
Submitted by oakcobalt, also found by MevSec, deliriusz, QiuhaoLi, and p0wd3r
https://github.com/code-423n4/2023-11-zetachain/blob/b237708ed5e86f12c4bddabddfd42f001e81941a/repos/node/x/crosschain/keeper/gas_payment.go#L321 https://github.com/code-423n4/2023-11-zetachain/blob/b237708ed5e86f12c4bddabddfd42f001e81941a/repos/node/x/crosschain/keeper/gas_payment.go#L292
UniswapV2 swap on zetaChain is used in multiple flows where the protocol will swap either zeta or ERC20 token to gas erc20 token to pay for outbound tx gas. However, there is no slippage protection for the swapping transaction which makes the process of charging gas fees slippage-prone, easily causing the sender to overpay the revert gas and lose returned funds.
Proof of Concept
When an outbound tx is initiated whether as a revert tx or diverted outbound tx, PayGasAndUpdateCctx() on gas_payment.go will be called. Based on CointType, PayGasAndUpdateCctx() divert the flow into different process that swaps either zeta token or ERC20 token into gas token of the external chain to pay for revert or outbound gas.
For example, CointType is zeta coin. PayGasInZetaAndUpdateCctx() will be called, which will first calculate gas token amount needed based on the external chainId. Then it first calls QueryUniswapV2RouterGetZetaAmountsIn() which query wzeta/gasZRC20 uniswapv2 pool for zeta token amount (outTxGasFeeInZeta) needed to swap out outTxGasFee.BigInt() amount of gasZRC20 token. Then it will call CallUniswapV2RouterSwapExactETHForToken() with exact input outTxGasFeeInZeta amount.
The above is problematic because after QueryUniswapV2RouterGetZetaAmountsIn(), there is no check to ensure the quoted input token amount outTxGasFeeInZeta is acceptable within a certain slippage parameter. When there is significant slippage in the uniswapV2 pool before protocol’s PayGasInZetaAndUpdateCctx() call, gasZRC20 token price in zeta amount will be inflated,causing gas overcharged.
When gas is overcharged, (1) this either cause feeInZeta.GT(zetaBurnt) == true which means sender doesn’t have enough zeta token to burn, which directly return an error, which might cause a revert cctx to be aborted and sender will lose all refunds. (2) Or if feeInZeta.GT(zetaBurnt) == false, sender’s zeta balance will be substracted by inflated gas payment, and the sender is overcharged.
//x/crosschain/keeper/gas_payment.go
func (k Keeper) PayGasInZetaAndUpdateCctx(
ctx sdk.Context,
chainID int64,
cctx *types.CrossChainTx,
zetaBurnt math.Uint,
noEthereumTxEvent bool,
) error {
...
// get the gas fee in Zeta using system uniswapv2 pool wzeta/gasZRC20 and adding the protocol fee
//@audit this quote the price of gasZRC20 in zeta denomination. There is no slippage check to ensure `outTxGasFeeInZeta` is not inflated.
|> outTxGasFeeInZeta, err := k.fungibleKeeper.QueryUniswapV2RouterGetZetaAmountsIn(ctx, outTxGasFee.BigInt(), gasZRC20)
...
feeInZeta := types.GetProtocolFee().Add(math.NewUintFromBigInt(outTxGasFeeInZeta))
//@audit-info when feeInZeta.GT(zetaBurnt)==true, sender doesn't have enough zeta to pay for gas, this return error, if the cctx is revert tx, this will abort and sender will lose all refunds
if feeInZeta.GT(zetaBurnt) {
return cosmoserrors.Wrap(types.ErrNotEnoughZetaBurnt, fmt.Sprintf("feeInZeta(%s) more than zetaBurnt (%s) | Identifiers : %s ",
feeInZeta,
zetaBurnt,
cctx.LogIdentifierForCCTX()),
)
}
...
//@audit-info when feeInZeta.GT(zetaBurnt)==false, this substract user zeta balance. Sender might be overcharged for gas due to slippage.
newAmount := zetaBurnt.Sub(feeInZeta)
...
//@audit This is directly execute the swap based on previous quote amount, slippage will be realized here.
|> amounts, err := k.fungibleKeeper.CallUniswapV2RouterSwapExactETHForToken(
ctx,
types.ModuleAddressEVM,
types.ModuleAddressEVM,
outTxGasFeeInZeta,
gasZRC20,
noEthereumTxEvent,
)
...
As seen above, any price of gasZRC20 will be accepted during gas payment, will directly result in either user losing the entire refund or user overpaying for gas. I consider this medium severity.
Recommended Mitigation Steps
Check and handle slippage case. For example, when QueryUniswapV2RouterGetZetaAmountsIn() returns an overinflated value due to slippage, return gas payment flow with a slippage error, and handle slippage error in the corresponding vote inbound/outbound tx flow to allow for pending cctx instead of direct abort.
Can see this qualifying as a leak of value and therefore warranting M severity.
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 payment function queries a UniswapV2 pool for a quote and then executes the swap without checking the quoted amount against a slippage tolerance.
Impact
If slippage inflates the gas cost, the sender may either overpay or, if the fee exceeds their burnt zeta, the transaction aborts and they lose all refunds.
What the attacker needed
Significant slippage in the UniswapV2 pool; outbound or revert transaction
The fix
The gas payment flow should check for slippage and handle it by returning an error that allows the transaction to be pending instead of aborting.
How it was found
- An outbound transaction is initiated, triggering PayGasAndUpdateCctx().
- The function calls [redacted]() to get the required zeta amount.
- The function calls [redacted]() with the quoted amount, without any slippage check.
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 cryptocurrency across chains, a fee is charged by swapping your tokens for the destination chain's gas token. This report says the swap doesn't check if the price is fair, so if someone manipulates the market, you might pay too much or lose your refund.
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.