Skip to content
Smart contracts Audit finding Intermediate

safeTransfer is not implemented correctly

The safeTransfer function in the ERC721 implementation does not call the onERC721Received hook on the recipient, so tokens can be transferred to contracts that cannot handle them, potentially locking them forever. The data parameter is also ignored.

Anurag Jain Follow with joestakey, eierina and 2 others

Originally disclosed on Code4rena

Submitted by csanuragjain, also found by joestakey, eierina, unforgiven, and Lambda

https://github.com/code-423n4/2022-11-paraspace/blob/main/paraspace-core/contracts/protocol/tokenization/base/MintableIncentivizedERC721.sol#L320

The safeTransfer function Safely transfers tokenId token from from to to, checking first that contract recipients are aware of the ERC721 protocol to prevent tokens from being forever locked. But seems like this safety check got missed in the _safeTransfer function leading to non secure ERC721 transfers

Proof of Concept

  1. User calls the safeTransferFrom function (Using NToken contract which implements MintableIncentivizedERC721 contract)
function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId,
        bytes memory _data
    ) external virtual override nonReentrant {
        _safeTransferFrom(from, to, tokenId, _data);
    }
  1. This makes an internal call to _safeTransferFrom -> _safeTransfer -> _transfer
function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId,
        bytes memory _data
    ) external virtual override nonReentrant {
        _safeTransferFrom(from, to, tokenId, _data);
    }

    function _safeTransferFrom(
        address from,
        address to,
        uint256 tokenId,
        bytes memory _data
    ) internal {
        require(
            _isApprovedOrOwner(_msgSender(), tokenId),
            "ERC721: transfer caller is not owner nor approved"
        );
        _safeTransfer(from, to, tokenId, _data);
    }

function _safeTransfer(
        address from,
        address to,
        uint256 tokenId,
        bytes memory
    ) internal virtual {
        _transfer(from, to, tokenId);
    }
  1. Now lets see _transfer function
function _transfer(
        address from,
        address to,
        uint256 tokenId
    ) internal virtual {
        MintableERC721Logic.executeTransfer(
            _ERC721Data,
            POOL,
            ATOMIC_PRICING,
            from,
            to,
            tokenId
        );
    }
  1. This is calling MintableERC721Logic.executeTransfer which simply transfers the asset
  2. In this full flow there is no check to see whether to address can support ERC721 which fails the purpose of safeTransferFrom function
  3. Also notice the comment mentions that data parameter passed in safeTransferFrom is sent to recipient in call but there is no such transfer of data

Recommended Mitigation Steps

Add a call to onERC721Received for recipient and see if the recipient actually supports ERC721.

WalidOfNow (Paraspace) commented via duplicate issue #51:

This is by design. We want to avoid re-entrancy to our contracts and so we removed calling the hook.


The original report as published on Code4rena, by csanuragjain. 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 _safeTransfer function omits the call to onERC721Received on the recipient, so it behaves like a plain transfer.

Impact

Tokens could be sent to contracts that cannot handle them, potentially locking them forever; the team states this is by design to avoid re-entrancy.

What the attacker needed

Recipient is a contract without ERC721 support

The fix

The team says this is intentional to avoid re-entrancy, so no fix was applied.

How it was found

  1. A user calls safeTransferFrom on the NToken contract.
  2. The call goes through _safeTransferFrom and then _safeTransfer.
  3. _safeTransfer calls _transfer, which calls MintableERC721Logic.executeTransfer.
  4. No check is made that the recipient contract implements onERC721Received.

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 you send an NFT to a smart contract, the contract must be able to receive it, otherwise the NFT can get stuck. The safe transfer function is supposed to check this by calling a special function on the receiving contract. Here, that check is missing, so NFTs can be sent to contracts that cannot handle them.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by Anurag Jain

View all

63 more disclosed reports, best first.

More from ParaSpace contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.