Collateral NFT deposited to a wrong address, when transferred directly to PaprController
The PaprController contract's onERC721Received hook incorrectly uses the first argument (the caller) as the token owner instead of the second argument (the actual owner). When an approved address or operator transfers an NFT to the contract, the NFT is credited to the wrong vault, causing the real owner to lose their collateral.
Submitted by Jeiwan, also found by Koolex, Ruhum, and rotcivegaf
Users will lose collateral NFTs when they are transferred to PaprController by an approved address or an operator.
Proof of Concept
The PaprController allows users to deposit NFTs as collateral to borrow Papr tokens. One way of depositing is by transferring an NFT to the contract directly via a call to safeTransferFrom: the contract implements the onERC721Received hook that will handle accounting of the transferred NFT (PaprController.sol#L159). However, the hook implementation uses a wrong argument to identify token owner: the first argument, which is used by the contract to identify token owner, is the address of the safeTransferFrom function caller, which may be an approved address or an operator. The actual owner address is the second argument (ERC721.sol#L436):
try IERC721Receiver(to).onERC721Received(_msgSender(), from, tokenId, data) returns (bytes4 retval) {
Thus, when an NFT is sent by an approved address or an operator, it’ll be deposited to the vault of the approved address or operator:
// test/paprController/OnERC721ReceivedTest.sol
function testSafeTransferByOperator_AUDIT() public {
address operator = address(0x12345);
vm.prank(borrower);
nft.setApprovalForAll(operator, true);
vm.prank(operator);
nft.safeTransferFrom(borrower, address(controller), collateralId, abi.encode(safeTransferReceivedArgs));
// NFT was deposited to the operator's vault.
IPaprController.VaultInfo memory vaultInfo = controller.vaultInfo(operator, collateral.addr);
assertEq(vaultInfo.count, 1);
// Borrower has 0 tokens in collateral.
vaultInfo = controller.vaultInfo(borrower, collateral.addr);
assertEq(vaultInfo.count, 0);
}
function testSafeTransferByApproved_AUDIT() public {
address approved = address(0x12345);
vm.prank(borrower);
nft.approve(approved, collateralId);
vm.prank(approved);
nft.safeTransferFrom(borrower, address(controller), collateralId, abi.encode(safeTransferReceivedArgs));
// NFT was deposited to the approved address's vault.
IPaprController.VaultInfo memory vaultInfo = controller.vaultInfo(approved, collateral.addr);
assertEq(vaultInfo.count, 1);
// Borrower has 0 tokens in collateral.
vaultInfo = controller.vaultInfo(borrower, collateral.addr);
assertEq(vaultInfo.count, 0);
}
Recommended Mitigation Steps
Consider this change:
--- a/src/PaprController.sol
+++ b/src/PaprController.sol
@@ -156,7 +156,7 @@ contract PaprController is
/// @param _id the id of the NFT
/// @param data encoded IPaprController.OnERC721ReceivedArgs
/// @return selector indicating succesful receiving of the NFT
- function onERC721Received(address from, address, uint256 _id, bytes calldata data)
+ function onERC721Received(address, address from, uint256 _id, bytes calldata data)
external
override
returns (bytes4)
wilsoncusack (Backed) confirmed
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
Impact
The original owner loses their collateral NFT, which is credited to the operator or approved address instead.
What the attacker needed
The victim must have approved an operator or approved…; The operator or approved address must transfer the NFT to…
The fix
The parameters in the onERC721Received function should be swapped so that the second argument (from) is used as the owner.
How it was found
- The borrower approves an operator or approved address to transfer their NFT.
- The operator or approved address calls safeTransferFrom to send the NFT to the PaprController contract.
- The contract's onERC721Received hook records the NFT as belonging to the caller (operator or approved address) instead of the original owner.
- The NFT is deposited into the wrong vault, and the original owner's vault shows zero collateral.
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 runs a function to decide what to do with it. This contract mistakenly thought the person who sent the NFT was the owner, rather than the person who actually owned it. So if someone else (like an approved operator) sent your NFT, the contract gave it to them instead of you.
What is Access control?
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.