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
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
- User calls the
safeTransferFromfunction (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);
}
- 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);
}
- Now lets see
_transferfunction
function _transfer(
address from,
address to,
uint256 tokenId
) internal virtual {
MintableERC721Logic.executeTransfer(
_ERC721Data,
POOL,
ATOMIC_PRICING,
from,
to,
tokenId
);
}
- This is calling
MintableERC721Logic.executeTransferwhich simply transfers the asset - In this full flow there is no check to see whether
toaddress can support ERC721 which fails the purpose ofsafeTransferFromfunction - Also notice the comment mentions that
dataparameter passed in safeTransferFrom is sent to recipient in call but there is no such transfer ofdata
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.
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
- A user calls safeTransferFrom on the NToken contract.
- The call goes through _safeTransferFrom and then _safeTransfer.
- _safeTransfer calls _transfer, which calls MintableERC721Logic.executeTransfer.
- 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.