Skip to content
Smart contracts Audit finding Intermediate

[Tomo-M3] Use safeMint instead of mint for ERC721

The staking contract uses `_mint` instead of `_safeMint` when minting a proof-of-staking NFT to `msg.sender`. If `msg.sender` is a contract that cannot handle ERC721 tokens, the NFT could become stuck in that contract.

Tomo Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2022-11-frankendao-judging/issues/65

Found by

Tomo

Summary

Use safeMint instead of mint for ERC721

Vulnerability Detail

The msg.sender will be minted as a proof of staking NFT when _stakeToken() is called.

However, if msg.sender is a contract address that does not support ERC721, the NFT can be frozen in the contract.

As per the documentation of EIP-721:

A wallet/broker/auction application MUST implement the wallet interface if it will accept safe transfers.

Ref: https://eips.ethereum.org/EIPS/eip-721

As per the documentation of ERC721.sol by Openzeppelin

Ref: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/ERC721.sol#L274-L285

Solidity
/**
 * @dev Mints `tokenId` and transfers it to `to`.
 *
 * WARNING: Usage of this method is discouraged, use {_safeMint} whenever possible
 *
 * Requirements:
 *
 * - `tokenId` must not exist.
 * - `to` cannot be the zero address.
 *
 * Emits a {Transfer} event.
 */
function _mint(address to, uint256 tokenId) internal virtual {

Impact

Users possibly lose their NFTs

Code Snippet

https://github.com/sherlock-audit/2022-11-frankendao/blob/main/src/Staking.sol#L411

Solidity
  _mint(msg.sender, _tokenId);

Tool used

Manual Review

Recommendation

Use safeMint instead of mint to check received address support for ERC721 implementation.

https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/ERC721.sol#L262

Discussion

zobront

I might consider this a duplicate of #55 but not sure how this is usually judged. We will be changing this function based on other issues to not allow "approved" spenders, so msg.sender will be the owner of the FrankenPunk, which ensures they are able to hold NFTs.

zobront

Fixed: https://github.com/Solidity-Guild/FrankenDAO/pull/14

I didn't need to add safeMint, as I made a change for another issue that removed the ability to non holder to unstake, which means they have the ability to hold NFTs.

jack-the-pug

Fix confirmed

The original report as published on Sherlock, by Tomo. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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 used `_mint` instead of `_safeMint`, so it did not verify that the receiving address could handle ERC721 tokens.

Impact

Users could possibly lose their NFTs if the receiving contract cannot handle ERC721 tokens.

What the attacker needed

Msg.sender is a contract; contract does not support ERC721

The fix

The issue was fixed by changing the unstaking logic so that only the NFT owner can unstake, ensuring the receiver can hold NFTs, rather than by adding `safeMint`.

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 contract creates a new NFT, it can use a basic mint function that does not check whether the receiver can handle NFTs. If the receiver is a smart contract that lacks the required interface, the NFT may be locked forever. Using a safe mint function asks the receiver if it can accept the NFT before completing the transfer.

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 Tomo

View all

1 more disclosed report, best first.

More from FrankenDAO

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.