👛The Dangers of Not Using SafeERC20 for Token Transfers

In Ethereum and other blockchain networks, secure token transfer handling is crucial to avoid vulnerabilities and potential exploits. One common oversight in smart contract development is not using OpenZeppelin's SafeERC20 library for token transfers. This can lead to serious vulnerabilities, particularly in scenarios involving bridges, where tokens are transferred across multiple chains. In this tutorial, we will explore the core vulnerability that arises when contracts fail to use SafeERC20 and how attackers can exploit this gap. We'll also use the QBridge Hack as a real-world example to highlight the dangers of not implementing SafeERC20.


Why SafeERC20 Is Critical for Secure Token Transfers

The ERC-20 token standard is widely used in DeFi applications for token transfers. However, the standard ERC-20 functions do not perform critical safety checks that could prevent unexpected behaviors or reentrancy attacks. Specifically, token transfers using functions like transfer() or transferFrom() can silently fail or interact with contracts in unintended ways if proper checks are not implemented.

OpenZeppelin's SafeERC20 is a library that adds additional safeguards when interacting with ERC-20 tokens. Here’s why using SafeERC20 is important:

  1. Function Call Verification: SafeERC20 uses OpenZeppelin's functionCall() method to verify that the target address contains valid contract code before attempting a transfer.

  2. Return Value Check: Standard ERC-20 functions may not return a boolean value indicating whether the transfer was successful. SafeERC20 checks the return value and ensures that the transfer succeeded.

  3. Prevents Reentrancy Attacks: SafeERC20 helps protect contracts from certain types of reentrancy attacks by performing proper validation before and after the token transfer.

Without SafeERC20:

When a contract directly uses transfer() or transferFrom() without these extra checks, there’s a risk of:

  • Tokens being transferred to invalid addresses (e.g., the zero address).

  • Silent failures, where the contract believes the transfer was successful, but no tokens were actually moved.

  • Opening the door to reentrancy vulnerabilities in more complex contracts, such as bridges.


Vulnerability: Not Using SafeERC20 in Token Bridges

Token bridges are a common feature in decentralized finance (DeFi). They allow users to transfer tokens between different blockchain networks (e.g., Ethereum to Binance Smart Chain). The basic functionality involves users locking tokens in a contract on one chain and receiving corresponding tokens on another chain.

However, if a bridge contract does not use SafeERC20 for handling token transfers, it introduces critical vulnerabilities. Let’s break down how this vulnerability works in a general bridge contract:

1. Basic Bridge Workflow:

  • A user initiates a token transfer by calling a deposit function on the bridge contract.

  • The contract should transfer the tokens from the user to itself using transferFrom(), then emit an event indicating that the tokens have been locked and are ready to be bridged.

  • On the destination chain, an equivalent amount of tokens is minted or released to the user.

2. Vulnerability in Transfer:

If the bridge contract directly uses the transferFrom() function (without SafeERC20), the following risks arise:

  • Invalid Target Address: The function may attempt to transfer tokens to an invalid address (e.g., the zero address) without reverting. This allows the bridge contract to proceed with emitting the event, believing the transfer was successful, even though no tokens were actually moved.

  • Reentrancy Attacks: Without SafeERC20’s checks, an attacker can use an ERC-777 token’s callback mechanism to execute reentrancy attacks, calling the deposit function multiple times and potentially minting or receiving more tokens than they deposited.

3. Impact:

This flaw can be exploited by attackers to mint or withdraw more tokens than they are entitled to, resulting in a loss of user funds or inflation of bridged tokens. Since bridges handle large amounts of value, such an exploit can cause significant financial damage.


Example: The QBridge Hack (January 2022)

The QBridge Hack is a prime example of how failing to use SafeERC20 in a bridge contract can be exploited. Let’s walk through the steps of the QBridge hack, using the previously described vulnerability as the core issue.

1. The QBridge Deposit Function:

  • The QBridge allowed users to deposit tokens on Ethereum and mint corresponding tokens (qXETH) on Binance Smart Chain.

  • However, the contract used a modified version of safeTransferFrom() that relied on the call() function without the safety checks provided by SafeERC20.

  • This function did not verify that the target address contained valid contract code, meaning that even if the target address was invalid (such as the zero address), the transfer would succeed without actually transferring tokens.

2. How the Exploit Worked:

  • The attacker called the deposit function on the QBridge with no actual ETH. The contract attempted to transfer tokens using its flawed safeTransferFrom() function.

  • Since the call was made to an invalid address, no tokens were transferred, but the transaction did not revert.

  • The bridge contract believed the deposit was successful and emitted an event indicating that the tokens had been deposited.

  • The attacker received qXETH tokens on Binance Smart Chain, even though no real ETH had been deposited.

3. Repeating the Attack:

  • The attacker repeated this process multiple times, each time increasing their balance of qXETH tokens without depositing any real ETH.

  • Finally, the attacker exchanged the qXETH for BNB, draining $80 million from the Qubit protocol.

4. How SafeERC20 Could Have Prevented This:

  • If the QBridge contract had used OpenZeppelin’s SafeERC20 for the token transfers, it would have verified that the target address contained contract code using functionCall().

  • The transfer would have reverted upon attempting to transfer to the zero address, stopping the attack before any fake tokens were minted on Binance Smart Chain.

Last updated