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