safeTransfer function does not check for existence of ERC20 token contract
The safeTransfer function in TransferHelper.sol uses a low-level call to transfer tokens without first checking that the token contract exists. An attacker could create a pair with a token that can be self-destructed, and after destruction, deposits into the pool would be lost because the transfer call would still succeed.
Originally disclosed on Code4rena
Submitted by 0xSmartContract, also found by 0xDecorativePineapple, berndartmueller, brgltd, Jeiwan, kaden, and rbserver
The safeTransfer function does not check for the existence of the ERC20 token contract , TransferHelper.sol performs a transfer with a low-level call without confirming the contract’s existence
function safeTransfer(
address token,
address to,
uint256 value
) internal {
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(IERC20Minimal.transfer.selector, to, value));
require(success && (data.length == 0 || abi.decode(data, (bool))), 'TF');
}
The low-level functions call, delegatecall and staticcall return true as their first return value if the account called is non-existent, as part of the design of the EVM. Account existence must be checked prior to calling if needed.
It allows malicious people to pair with a qualified token like ETH with [dubious] tokens that they can destroy later, and most importantly, to run the safeTransfer function even if the token contract is later destroyed.
Proof of Concept
1 Alice creates a pair of A and B Tokens (For exampleETH - TestToken Pair). The creator and only owner of TestToken is Alice.
2 Next, Alice destroys the TestToken with a Selfdestruct based on onlyowner privilege.
3 Bob, unaware of this, deposits ETH into the pool to trade from the ETH-TestToken pair, but cannot receive any tokens because safeTransfer does not check for the existence of the contract.
Recommended Mitigation Steps
Have the SafeTransfer function check the existence of the contract before every transaction.
0xean (judge) decreased severity to Medium and commented:
This is technically correct but requires some pretty explicit external requirements that cannot be easily fixed by a permission-less protocol.
Mainly that if someone creates a pool with an ERC20 that can be self destructed, that pool’s paired asset is going to be locked in some form as well.
I am going to downgrade to M due to the external factors required for this to occur. The recommended fix could ensure more assets don’t become locked as a result of the self destruct.
sameepsi (QuickSwap & StellaSwap) disputed and commented:
Tokens that are destructible are non-standard tokens. We cannot add support for all types of non-standard tokens in the protocol. This protocol is intended to work fine with standard tokens.
Going to leave as Medium. I understand the sponsor’s point here, but also believe there is a fix that would at least reduce some of the risk here. I do agree it is certainly an edge case.
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 safeTransfer function uses a low-level call without verifying that the token address contains a contract, so calls to non-existent addresses return success.
Impact
If a pool is created with a token that can be self-destructed, the paired asset (e.g., ETH) could become locked after the token is destroyed, as the transfer call would still succeed without moving any tokens.
What the attacker needed
A token that can be self-destructed; a pool created with that token; victim deposits into the pool
The fix
The safeTransfer function should check that the token contract exists before performing the transfer.
How it was found
- Alice creates a pair of ETH and TestToken, and is the only owner of TestToken.
- Alice destroys TestToken using selfdestruct with onlyowner privilege.
- Bob deposits ETH into the pool to trade from the ETH-TestToken pair but cannot receive any tokens because safeTransfer does not check for the contract's existence.
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 a smart contract calls another contract that doesn't exist, the call still returns success by default in Ethereum. This code uses such a call to transfer tokens but never checks if the token contract is actually there. So if someone makes a token that can be deleted, people could lose funds when trading with it.
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.