Skip to content
Business logic Audit finding Intermediate

amount requires to be updated to contract balance increase (1)

The contract assumes that ERC20 transferFrom or transfer moves exactly the requested amount, but some tokens (e.g. fee-on-transfer) deliver less. An attacker could exploit this to cause accounting mismatches, potentially leading to value leakage or re-entrancy.

MaratCerby Follow

Originally disclosed on Code4rena

Submitted by MaratCerby, also found by 0x1337, 0x52, 0xYamiDancho, AuditsAreUS, berndartmueller, cccz, CertoraInc, csanuragjain, defsec, Dravee, GimelSec, hickuphh3, horsefacts, hyh, IllIllI, jayjonah8, kenzo, leastwood, mtz, p4st13r4, PPrieditis, reassor, Ruhum, throttle, TrungOre, VAD37, wuwe1, and ych18

Every time transferFrom or transfer function in ERC20 standard is called there is a possibility that underlying smart contract did not transfer the exact amount entered.

It is required to find out contract balance increase/decrease after the transfer. This pattern also prevents from re-entrancy attack vector.

Recommended Mitigation Steps

Recommended code:

function fundPool(uint poolId) internal {
    Pool storage pool = pools[poolId];
    bool success = true;
    uint amount;
    for (uint i = 0; i < pool.rewardFunding.length; i++) {
        amount = getMaximumRewards(poolId, i);
        // transfer the tokens from pool-creator to this contract

        uint256 balanceBefore = IERC20(pool.rewardTokens[i]).balanceOf(address(this)); // remembering asset balance before the transfer
        IERC20(pool.rewardTokens[i]).safeTransferFrom(msg.sender, address(this), amount);
        uint256 newAmount = IERC20(pool.rewardTokens[i]).balanceOf(address(this)) - balanceBefore; // updating actual amount to the contract balance increase
        success = success && newAmount == amount; // making sure amounts match

        // bookkeeping to make sure pools don't share tokens
        pool.rewardFunding[i] += amount;
    }
    require(success, 'Token deposits failed');
}

illuzen (FactoryDAO) confirmed, disagreed with severity and commented:

Re-entrance here would involve sending our contract tokens multiple times and creating multiple pools, not withdraw any funds. Malicious tokens could lie about balance as well, so the mitigation doesn’t completely fix the issue.

And malicious tokens are explicitly considered in the comments as acceptable. What is unacceptable is malicious pools harming other pools.

illuzen (FactoryDAO) resolved:

https://github.com/code-423n4/2022-05-factorydao/pull/2

Justin Goro (judge) decreased severity to Medium and commented:

Just a note on the reason for checking token balances before and after: not all tokens that report a difference between the balance and the amount are acting maliciously. In particular fee-on-transfer tokens.

Reducing severity as this is a value leakage situation and because the sponsor has taken pains to emphasize the isolation of pools and the desire to not have to support all tokens.


The original report as published on Code4rena, by MaratCerby. 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 contract trusts the transfer amount without verifying the actual balance change, which fails for tokens that do not transfer the exact amount.

Impact

This could lead to value leakage or accounting errors, and the judge noted it is a value leakage situation, though the sponsor argued malicious tokens are acceptable and the mitigation does not fully fix the issue.

What the attacker needed

Fee-on-transfer or non-standard token; contract uses transferFrom without balance check

The fix

The contract should measure the balance before and after the transfer and use the actual increase, as shown in the recommended code, and this was resolved via a pull request.

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 sends tokens, it usually assumes the recipient gets exactly the amount sent. But some tokens take a fee or behave oddly, so the actual amount received can be less. This report says the contract should check the balance before and after to see what really arrived.

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 MaratCerby

View all

2 more disclosed reports, best first.

More from FactoryDAO 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.