Skip to content
Business logic Audit finding Intermediate

MerkleDropFactory.depositTokens() does not require the tree to exist

The depositTokens function does not check that the provided treeIndex exists, so it can operate on a non-existent tree with a zero token address. If the contract later uses safeTransferFrom, an attacker could deposit tokens into a non-existent tree, increasing its tokenBalance.

AuditsAreUS Follow

Originally disclosed on Code4rena

Submitted by AuditsAreUS

The function depositTokens() does not first check to ensure that the treeIndex exists.

The impact is that we will attempt to transfer from the zero address to this address. If the transfer succeeds (which it currently does not since we use IERC20.transferFrom()) then the tokenBalance of this index will be increased.

This will be an issue if the contract is updated to use OpenZeppelin’s safeTransferFrom() function. This update may be necessary to support non-standard ERC20 tokens such as USDT.

If the update is made then merkleTree.tokenAddress.safeTransferFrom(msg.sender, address(this), value), "ERC20 transfer failed"); will succeed if merkleTree.tokenAddress = address(0) since safeTransferFrom() succeeds against the zero address.

Proof of Concept

There are no checks the treeIndex is valid.

    function depositTokens(uint treeIndex, uint value) public {
        // storage since we are editing
        MerkleTree storage merkleTree = merkleTrees[treeIndex];

        // bookkeeping to make sure trees don't share tokens
        merkleTree.tokenBalance += value;

        // transfer tokens, if this is a malicious token, then this whole tree is malicious
        // but it does not effect the other trees
        require(IERC20(merkleTree.tokenAddress).transferFrom(msg.sender, address(this), value), "ERC20 transfer failed");
        emit TokensDeposited(treeIndex, merkleTree.tokenAddress, value);
    }

Recommended Mitigation Steps

Consider adding the check to ensure 0 < treeIndex <= numTrees in depositTokens().

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

Technically valid, but this harms no one but the caller, and incorrectly entering arguments is not in scope.

File this under code style.

illuzen (FactoryDAO) resolved:

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

Justin Goro (judge) commented:

Maintaining severity as validating treeIndex isn’t out of bounds seems within the appropriate expectations of input validation.

HickupHH3 (warden) commented:

FYI, the call will not succeed because OZ’s safeTransferFrom() will revert if target isn’t an EOA. Solmate on the other hand will not. https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/utils/SafeERC20.sol#L110 https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/Address.sol#L135 https://github.com/Rari-Capital/solmate/blob/main/src/utils/SafeTransferLib.sol#L9

Justin Goro (judge) commented:

@HickupHH3 - ‘target’ in openzeppelin’s address library refers to the contract making the call. In other words, merkleTree.tokenAddress. So the call will succeed.


The original report as published on Code4rena, by AuditsAreUS. 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 function lacks a check that the treeIndex is within the valid range of existing trees.

Impact

If the contract is updated to use safeTransferFrom, an attacker could deposit tokens into a non-existent tree, increasing its tokenBalance, though currently the transfer fails.

What the attacker needed

Contract uses safeTransferFrom; attacker can call depositTokens

The fix

The function should be fixed by adding a check to ensure the treeIndex is valid (0 < treeIndex <= numTrees).

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.

This is a smart contract bug where a function that deposits tokens doesn't verify that the tree you're depositing into actually exists. If you pass a fake tree number, the contract might try to transfer tokens from a zero address, which could succeed under certain conditions and mess up the contract's records.

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 AuditsAreUS

View all

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