Skip to content
Business logic Audit finding Intermediate

Unused ERC20 tokens are not refunded

The swap contract only refunds leftover ETH to the caller and never returns unused ERC20 sell tokens, so any tokens left after a swap stay stuck in the contract. An attacker or ordinary user could leave value stranded in the contract rather than receiving it back.

WatchPug Follow

Originally disclosed on Code4rena

Submitted by WatchPug

Based on the context and comments in the code, we assume that it’s possible that there will be some leftover sell tokens, not only when users are selling unwrapped ETH but also for ERC20 tokens.

However, in the current implementation, only refunded ETH is returned (L158).

Because of this, the leftover tkoens may be left in the contract unintentionally.

Swap.sol L153-L181

if (boughtERC20Amount > 0) {
    // take the swap fee from the ERC20 proceeds and return the rest
    uint256 toTransfer = SWAP_FEE_DIVISOR.sub(swapFee).mul(boughtERC20Amount).div(SWAP_FEE_DIVISOR);
    IERC20(zrxBuyTokenAddress).safeTransfer(msg.sender, toTransfer);
    // return any refunded ETH
    payable(msg.sender).transfer(boughtETHAmount);

    emit SwappedTokens(
        zrxSellTokenAddress,
        zrxBuyTokenAddress,
        amountToSell,
        boughtERC20Amount,
        boughtERC20Amount.sub(toTransfer)
    );
} else {

    // take the swap fee from the ETH proceeds and return the rest. Note
    // that if any 0x protocol fee is refunded in ETH, it also suffers
    // the swap fee tax
    uint256 toTransfer = SWAP_FEE_DIVISOR.sub(swapFee).mul(boughtETHAmount).div(SWAP_FEE_DIVISOR);
    payable(msg.sender).transfer(toTransfer);
    emit SwappedTokens(
        zrxSellTokenAddress,
        zrxBuyTokenAddress,
        amountToSell,
        boughtETHAmount,
        boughtETHAmount.sub(toTransfer)
    );
}

Shadowfiend (Tally) acknowledged:

I believe the 0x API does in fact guarantee that we won’t have any sell tokens left over, particularly since we intend to use RFQ-T for these quotes, but if not we will fix this… And we may make the change regardless to future-proof.

0xean (judge) commented:

Downgrading to sev 2

2 — Med: Assets not at direct risk, but the function of the protocol or its availability could be impacted, or leak value with a hypothetical attack path with stated assumptions, but external requirements.

As I believe this to be a “leak value” scenario.

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 ERC20 branch of the swap function transfers the bought tokens and refunded ETH but has no code path to return unused sell tokens to the caller.

Impact

Leftover ERC20 sell tokens may be stranded in the contract, leaking value, though the team believes the 0x API normally prevents leftovers and the judge rated it medium severity.

What the attacker needed

A swap that leaves unused sell tokens

The fix

The contract should be changed to refund any unused ERC20 sell tokens to the caller, which the team said it may do to future-proof.

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 is a program on a blockchain that holds funds. When someone swaps tokens, the contract is supposed to send back anything that was not used. Here it sends back leftover ETH but forgets leftover tokens, so those tokens stay locked in the contract.

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