Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect ERC721 receiver check causes safe transfers to always fail #135

Closed
howlbot-integration bot opened this issue Sep 16, 2024 · 2 comments
Closed
Labels
2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working downgraded by judge Judge downgraded the risk level of this issue duplicate-148 🤖_09_group AI based duplicate group recommendation satisfactory satisfies C4 submission criteria; eligible for awards sufficient quality report This report is of sufficient quality

Comments

@howlbot-integration
Copy link

Lines of code

https://github.com/code-423n4/2024-08-superposition/blob/4528c9d2dbe1550d2660dac903a8246076044905/pkg/sol/OwnershipNFTs.sol#L73-L96

Vulnerability details

Impact

The _onTransferReceived function incorrectly checks the return value from the onERC721Received function call. This causes all safe transfers to contract addresses to fail, even when the receiving contract correctly implements the ERC721 receiver interface.

Proof of Concept

Take a look at the _onTransferReceived function: https://github.com/code-423n4/2024-08-superposition/blob/4528c9d2dbe1550d2660dac903a8246076044905/pkg/sol/OwnershipNFTs.sol#L73-L96

function _onTransferReceived(
    address _sender,
    address _from,
    address _to,
    uint256 _tokenId
) internal {
    // only call the callback if the receiver is a contract
    if (_to.code.length == 0) return;

    bytes4 data = IERC721TokenReceiver(_to).onERC721Received(
        _sender,
        _from,
        _tokenId,
        ""
    );

    require(
        data != IERC721TokenReceiver.onERC721Received.selector,
        "bad nft transfer received data"
    );
}

The require statement checks if the returned data is NOT equal to the onERC721Received selector. This is the opposite of what it should be doing. According to the ERC721 standard, the onERC721Received function should return its function selector to indicate successful receipt of the token. but the way it's currently implemented will revert the transaction when the receiver correctly implements the onERC721Received function.

This bug goes on to affect both safeTransferFrom functions in the contract, as they both call _onTransferReceived:

function safeTransferFrom(
    address _from,
    address _to,
    uint256 _tokenId
) external payable {
    _transfer(_from, _to, _tokenId);  <--------
    _onTransferReceived(msg.sender, _from, _to, _tokenId);
}

function safeTransferFrom(
    address _from,
    address _to,
    uint256 _tokenId,
    bytes calldata /* _data */
) external payable {
    _transfer(_from, _to, _tokenId);
    _onTransferReceived(msg.sender, _from, _to, _tokenId);  <--------
}

Tools used

Manual review

Recommended Mitigation Steps

Change the require statement in the _onTransferReceived function to check for equality instead of inequality:

function _onTransferReceived(
    address _sender,
    address _from,
    address _to,
    uint256 _tokenId
) internal {
    // only call the callback if the receiver is a contract
    if (_to.code.length == 0) return;

    bytes4 data = IERC721TokenReceiver(_to).onERC721Received(
        _sender,
        _from,
        _tokenId,
        ""
    );

    require(
-       data != IERC721TokenReceiver.onERC721Received.selector,
-       "bad nft transfer received data"
+       data == IERC721TokenReceiver.onERC721Received.selector,
+       "ERC721: transfer to non ERC721Receiver implementer"
    );
}

Assessed type

Error

@howlbot-integration howlbot-integration bot added 3 (High Risk) Assets can be stolen/lost/compromised directly 🤖_09_group AI based duplicate group recommendation bug Something isn't working duplicate-55 sufficient quality report This report is of sufficient quality labels Sep 16, 2024
howlbot-integration bot added a commit that referenced this issue Sep 16, 2024
@c4-judge c4-judge added duplicate-148 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value downgraded by judge Judge downgraded the risk level of this issue and removed duplicate-55 3 (High Risk) Assets can be stolen/lost/compromised directly labels Sep 23, 2024
@c4-judge
Copy link
Contributor

alex-ppg changed the severity to 2 (Med Risk)

@c4-judge c4-judge added the satisfactory satisfies C4 submission criteria; eligible for awards label Sep 23, 2024
@c4-judge
Copy link
Contributor

alex-ppg marked the issue as satisfactory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working downgraded by judge Judge downgraded the risk level of this issue duplicate-148 🤖_09_group AI based duplicate group recommendation satisfactory satisfies C4 submission criteria; eligible for awards sufficient quality report This report is of sufficient quality
Projects
None yet
Development

No branches or pull requests

1 participant