Skip to content
Business logic Audit finding Intermediate

Unchecked ERC20 transfers can cause lock up

The contract's topupMarketBalance function does not check the return value of an ERC20 transferFrom call, so if the token returns false instead of reverting on failure, the internal marketBalance can be inflated without actual tokens being transferred. This can cause other functions to fail with 'Books are unbalanced', locking up deposits and withdrawals.

axic Follow with gpersoon and pauliax

Originally disclosed on Code4rena

*Submitted by axic, also found by gpersoon, pauliax, Jmukesh, a_delamo, s1m0, cmichel, and shw_

Some major tokens went live before ERC20 was finalized, resulting in a discrepancy whether the transfer functions should (A) return a boolean or (B) revert/fail on error. The current best practice is that they should revert, but return “true” on success. However, not every token claiming ERC20-compatibility is doing this — some only return true/false; some revert, but do not return anything on success. This is a well known issue, heavily discussed since mid-2018.

Today many tools, including OpenZeppelin, offer a wrapper for “safe ERC20 transfer”:

RealityCards is not using such a wrapper, but instead tries to ensure successful transfers via the balancedBooks modifier:

modifier balancedBooks {
    _;
    // using >= not == in case anyone sends tokens direct to contract
    require(
        erc20.balanceOf(address(this)) >=
            totalDeposits + marketBalance + totalMarketPots,
        "Books are unbalanced!"
    );
}

This modifier is present on most functions, but is missing on topupMarketBalance:

function topupMarketBalance(uint256 _amount) external override {
    erc20.transferFrom(msgSender(), address(this), _amount);
    if (_amount > marketBalanceDiscrepancy) {
        marketBalanceDiscrepancy = 0;
    } else {
        marketBalanceDiscrepancy -= _amount;
    }
    marketBalance += _amount;
}

In the case where an ERC20 token which is not reverting on failures is used, a malicious actor could call topupMarketBalance with a failing transfer, but also move the value of marketBalance above the actual holdings. After this, deposit, withdrawDeposit, payRent, payout, sponsor, etc. could be locked up and always failing with “Books are unbalanced”.

Anyone can call topupMarketBalance with some unrealistically large number, so that marketBalance does not overflow, but is above the actually helping balances. This is only possible if the underlying ERC20 used is not reverting on failures, but is returning “false” instead.

Recommended Steps:

  1. Use something like OpenZeppelin’s SafeERC20
  2. Set up an allow list for tokens, which are knowingly safe
  3. Consider a different approach to the balancedBooks modifier

Splidge (Reality Cards) confirmed:

The particular ERC20 contracts we are using don’t have this issue. However for futureproofing in the event we change ERC20 tokens we will implement the recommended mitigation 1 and start using OpenZeppelin’s SafeERC20.

Splidge (Reality Cards) resolved:

Fix implemented here

The original report as published on Code4rena, by axic. 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 does not use a safe transfer wrapper and fails to check the boolean return value of the ERC20 transferFrom call in topupMarketBalance, allowing the internal balance to be inflated without actual token transfer.

Impact

If the underlying ERC20 token returns false on failure instead of reverting, an attacker can cause other functions to be permanently locked up, denying service to users.

What the attacker needed

The ERC20 token used returns false on failure; Attacker can call topupMarketBalance

The fix

The project implemented the recommended mitigation by starting to use OpenZeppelin's SafeERC20.

How it was found

  1. Identify that the ERC20 token used returns false on failed transfers instead of reverting.
  2. Call topupMarketBalance with a very large amount that causes transferFrom to fail (return false).
  3. The function ignores the false return and increases marketBalance by the amount.
  4. Subsequent calls to deposit, withdrawDeposit, payRent, payout, sponsor, etc. fail with 'Books are unbalanced'.

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 older cryptocurrency tokens don't signal failure by stopping the transaction; instead they return a value of false. The contract ignores that return value, so it thinks tokens were added when they weren't. This makes the contract's internal accounting wrong and can block other users from withdrawing or depositing.

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 from Reality Cards (round 2)

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.