Skip to content
Business logic Audit finding Advanced

[WP-H0] Fake balances can be created for not-yet-existing ERC20 tokens, which allows attackers to set traps to steal funds from future users

The vault creation function used a token transfer library that does not verify the token address contains code, so a vault could be created for a non-existent token with a fake balance. An attacker could set traps for future tokens deployed at the same address on the same network, later withdrawing real tokens deposited by other users.

WatchPug Follow

Originally disclosed on Code4rena

Submitted by WatchPug, also found by 0xsanson, BondiPestControl, and IllIllI

https://github.com/code-423n4/2022-05-cally/blob/1849f9ee12434038aa80753266ce6a2f2b082c59/contracts/src/Cally.sol#L158-L201

function createVault(
    uint256 tokenIdOrAmount,
    address token,
    ...
) external returns (uint256 vaultId) {
    ...
    Vault memory vault = Vault({
        ...
    });

    // vault index should always be odd
    vaultIndex += 2;
    vaultId = vaultIndex;
    _vaults[vaultId] = vault;

    // give msg.sender vault token
    _mint(msg.sender, vaultId);

    emit NewVault(vaultId, msg.sender, token);

    // transfer the NFTs or ERC20s to the contract
    vault.tokenType == TokenType.ERC721
        ? ERC721(vault.token).transferFrom(msg.sender, address(this), vault.tokenIdOrAmount)
        : ERC20(vault.token).safeTransferFrom(msg.sender, address(this), vault.tokenIdOrAmount);
}

https://github.com/code-423n4/2022-05-cally/blob/1849f9ee12434038aa80753266ce6a2f2b082c59/contracts/src/Cally.sol#L23-L34

import "solmate/utils/SafeTransferLib.sol";

...

contract Cally is CallyNft, ReentrancyGuard, Ownable {
    using SafeTransferLib for ERC20;
    ...

When creating a new vault, solmate’s SafeTransferLib is used for pulling vault.token from the caller’s account, this issue won’t exist if OpenZeppelin’s SafeERC20 is used instead.

That’s because there is a subtle difference between the implementation of solmate’s SafeTransferLib and OZ’s SafeERC20:

OZ’s SafeERC20 checks if the token is a contract or not, solmate’s SafeTransferLib does not.

See: https://github.com/Rari-Capital/solmate/blob/main/src/utils/SafeTransferLib.sol#L9

Note that none of the functions in this library check that a token has code at all! That responsibility is delegated to the caller.

As a result, when the token’s address has no code, the transaction will just succeed with no error.

This attack vector was made well-known by the qBridge hack back in Jan 2022.

For our project, this alone still won’t be a problem, a vault created and wrongfully accounted for a certain amount of balance for a non-existing token won’t be much of a problem, there will be no fund loss as long as the token stays that way (being non-existing).

However, it’s becoming popular for protocols to deploy their token across multiple networks and when they do so, a common practice is to deploy the token contract from the same deployer address and with the same nonce so that the token address can be the same for all the networks.

For example: $1INCH is using the same token address for both Ethereum and BSC; Gelato's$GEL token is using the same token address for Ethereum, Fantom and Polygon.

A sophisticated attacker can exploit it by taking advantage of that and setting traps on multiple potential tokens to steal from the future users that deposits with such tokens.

Proof of Concept

Given:

  • ProjectA has TokenA on another network;
  • ProjectB has TokenB on another network;
  • ProjectC has TokenC on another network;
  • The attacker createVault() for TokenA, TokenB, and TokenC with 10000e18 as tokenIdOrAmount each;
  • A few months later, ProjectB lunched TokenB on the local network at the same address;
  • Alice created a vault with 11000e18 TokenB;
  • The attacker called initiateWithdraw() and then withdraw() to receive 10000e18 TokenB.

In summary, one of the traps set by the attacker was activated by the deployment of TokenB and Alice was the victim. As a result, 10000e18 TokenB was stolen by the attacker.

Recommendation

Consider using OZ’s SafeERC20 instead.

outdoteth (Cally) confirmed and resolved:

this issue has been fixed here: https://github.com/outdoteth/cally/pull/5

HardlyDifficult (judge) commented:

Great catch and the potential attack is very clearly explained. Although the window for an attack like this would not be common, it’s an easy trap to setup and likely would occur as some point if Cally is planning to support multiple networks.


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 use of solmate's SafeTransferLib, which does not check that the token address has code, instead of OpenZeppelin's SafeERC20, which does.

Impact

An attacker could steal tokens from future users who deposit a token that is later deployed at a trapped address; the judge noted the window for such an attack would not be common.

What the attacker needed

Attacker can create vaults; future token deployed at same address; victim deposits that token

The fix

The issue was fixed by switching to OpenZeppelin's SafeERC20, which checks that the token address contains code.

How it was found

  1. The attacker calls createVault for several token addresses that do not yet exist on the local network, specifying a large amount.
  2. Later, a project deploys a real token at one of those addresses.
  3. A victim creates a vault depositing that real token.
  4. The attacker calls initiateWithdraw and then withdraw to receive the victim's tokens.

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 can create a vault for any token address, even one where no token exists yet. Because the transfer function does not check that the address actually contains a token contract, it silently succeeds and records a balance. If a real token is later deployed at that same address, the attacker can withdraw tokens that other people deposited.

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