Voting tokens may be lost when given to non-EOA accounts
The VotingEscrow contract's minting and transfer functions do not correctly check the return value of onERC721Received, so a non-fungible token representing a locked voting position could be sent to a contract that cannot handle it and become permanently locked. An attacker could not directly exploit this, but a user could lose their voting power and rewards if they transfer to an incompatible contract.
Submitted by IllIllI, also found by rotcivegaf and Ruhum
veNFTs may be sent to contracts that cannot handle them, and therefore all rewards and voting power, as well as the underlying are locked forever
Proof of Concept
The original code had the following warning:
* @dev 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.
The minting code, which creates the locks, does not do this check:
File: contracts/contracts/VotingEscrow.sol #1
462 function _mint(address _to, uint _tokenId) internal returns (bool) {
463 // Throws if `_to` is zero address
464 assert(_to != address(0));
465 // TODO add delegates
466 // checkpoint for gov
467 _moveTokenDelegates(address(0), delegates(_to), _tokenId);
468 // Add NFT. Throws if `_tokenId` is owned by someone
469 _addTokenTo(_to, _tokenId);
470 emit Transfer(address(0), _to, _tokenId);
471 return true;
472 }
Once a lock is already minted, if it’s transfered to a contract, the code does attempt to check for the issue:
File: contracts/contracts/VotingEscrow.sol #2
378 /// If `_to` is a smart contract, it calls `onERC721Received` on `_to` and throws if
379 /// the return value is not `bytes4(keccak256("onERC721Received(address,address,uint,bytes)"))`.
380 /// @param _from The current owner of the NFT.
381 /// @param _to The new owner.
382 /// @param _tokenId The NFT to transfer.
383 /// @param _data Additional data with no specified format, sent in call to `_to`.
384 function safeTransferFrom(
385 address _from,
386 address _to,
387 uint _tokenId,
388 bytes memory _data
389 ) public {
390 _transferFrom(_from, _to, _tokenId, msg.sender);
391
392 if (_isContract(_to)) {
393 // Throws if transfer destination is a contract which does not implement 'onERC721Received'
394 try IERC721Receiver(_to).onERC721Received(msg.sender, _from, _tokenId, _data) returns (bytes4) {} catch (
395 bytes memory reason
396 ) {
397 if (reason.length == 0) {
398 revert('ERC721: transfer to non ERC721Receiver implementer');
399 } else {
400 assembly {
401 revert(add(32, reason), mload(reason))
402 }
403 }
404 }
405 }
406 }
While the transfer function does in fact execute onERC721Received, it doesn’t actually do a check of the bytes4 variable - it only checks for a non-zero length. The ERC721 standard says that for the function call to be valid, it must return the bytes4 function selector, otherwise it’s invalid. If a user of the escrow system uses a contract that is attempting to explicitly reject NFTs by returning zero in its onERC721Received() function, the VotingEscrow will interpret that response as success and will transfer the NFT, potentially locking it forever. If the lock is minted to a contract, no checks are done, and the NFT can be locked forever.
Recommended Mitigation Steps
Call onERC721Received() in _mint() and ensure that the return value equals IERC721Receiver.onERC721Received.selector in both _mint() and safeTransferFrom().
pooltypes (Velodrome) disputed
Alex the Entreprenerd (judge) commented:
While I would be surprised by any realistic scenario of a smart contract that would implement
onERC721Receivedand not return the specified selector, I have to acknowledge that technically we can get multiple false positives, especially when dealing with contracts that have afallbackfunction .That said, to me that was not sufficient to constitute a valid finding.
However, I went and checked the EIP-721 and the standard definition of
safeTransferFrom, which can be verified here: https://eips.ethereum.org/EIPS/eip-721According to the standard the function must check for the return value, and for that reason I believe the finding to be valid.
While I would be surprised if this causes any issue in the real world, because the function is non-conformant to the standard and technically a loss can happen because of it, I believe Medium Severity to be appropriate.
To confirm, I doubt any meaningful loss will ever happen.
However this finding is basically: “Incorrect implementation of
onERC721Received” and it’s a valid finding at that.
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 fails to verify that the onERC721Received call returns the required function selector, and it also skips this check entirely during minting.
Impact
A user could lose their voting power and rewards if they transfer a locked NFT to a contract that does not properly implement the receiver interface, though the judge doubts any meaningful loss will occur in practice.
What the attacker needed
A contract that returns zero from onERC721Received; user transfers a veNFT to that contract
The fix
The minting function should call onERC721Received and both minting and safeTransferFrom should check that the return value equals the expected selector.
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. When you send a special token (an NFT) to a contract, the contract is supposed to say 'yes, I can handle it' by returning a specific code. Here, the code only checks that some response came back, not that it was the right one. So a contract that says 'no' might still receive the token, and then the token could be stuck forever.
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.