CryptoPunks NFTs may be stolen via deposit frontrunning
The addCollateral function in the ERC721 pool allows anyone to deposit a CryptoPunk NFT that is offered for sale to the pool, even if the caller is not the owner. An attacker can front-run the owner's deposit transaction and have the NFT credited to their own account, then withdraw it.
Source: https://github.com/sherlock-audit/2023-01-ajna-judging/issues/140
Found by
Jeiwan
Summary
Depositing of CryptoPunks NFTs may be front run, a malicious actor may deposit someone else's CryptoPunks NFT.
Vulnerability Detail
Due to the CryptoPunks NFT collection not implementing the ERC721 standard, depositing of CryptoPunks NFTs is implemented via a direct sale:
- token owner needs to call offerPunkForSaleToAddress and set the
toAddressvalue to the address of the pool the token will be deposited to; - token owner then calls the addCollateral function of the ERC721 pool;
- the pool buys the token from its owner.
However, addCollateral can be called by anyone: the pool will buy the token and will deposit it on the caller's account even if the caller is not the owner of the token.
Impact
CryptoPunks NFTs owner may lose their NFTs when trying to deposit them to an ERC721 pool. A malicious actor may front run the depositing and deposit the NFTs to their account. The malicious actor may then withdraw the NFTs.
Code Snippet
ERC721Pool.sol#L577 CryptoPunksMarket:
function offerPunkForSaleToAddress(uint punkIndex, uint minSalePriceInWei, address toAddress) {
if (!allPunksAssigned) throw;
if (punkIndexToAddress[punkIndex] != msg.sender) throw;
if (punkIndex >= 10000) throw;
punksOfferedForSale[punkIndex] = Offer(true, punkIndex, msg.sender, minSalePriceInWei, toAddress);
PunkOffered(punkIndex, minSalePriceInWei, toAddress);
}
function buyPunk(uint punkIndex) payable {
if (!allPunksAssigned) throw;
Offer offer = punksOfferedForSale[punkIndex];
if (punkIndex >= 10000) throw;
if (!offer.isForSale) throw; // punk not actually for sale
if (offer.onlySellTo != 0x0 && offer.onlySellTo != msg.sender) throw; // punk not supposed to be sold to this user
if (msg.value < offer.minValue) throw; // Didn't send enough ETH
if (offer.seller != punkIndexToAddress[punkIndex]) throw; // Seller no longer owner of punk
address seller = offer.seller;
punkIndexToAddress[punkIndex] = msg.sender;
balanceOf[seller]--;
balanceOf[msg.sender]++;
Transfer(seller, msg.sender, 1);
punkNoLongerForSale(punkIndex);
pendingWithdrawals[seller] += msg.value;
PunkBought(punkIndex, msg.value, seller, msg.sender);
// Check for the case where there is a bid from the new owner and refund it.
// Any other bid can stay in place.
Bid bid = punkBids[punkIndex];
if (bid.bidder == msg.sender) {
// Kill bid and refund value
pendingWithdrawals[msg.sender] += bid.value;
punkBids[punkIndex] = Bid(false, punkIndex, 0x0, 0);
}
}
Tool used
Manual Review
Recommendation
Before buying a CryptoPunks NFT, consider checking that msg.sender is the owner of the token. For example:
diff --git a/contracts/src/ERC721Pool.sol b/contracts/src/ERC721Pool.sol
index b1bf36b..a512a9d 100644
--- a/contracts/src/ERC721Pool.sol
+++ b/contracts/src/ERC721Pool.sol
@@ -574,6 +574,7 @@ contract ERC721Pool is FlashloanablePool, IERC721Pool {
ICryptoKitties(_getArgAddress(COLLATERAL_ADDRESS)).transferFrom(msg.sender ,address(this), tokenId);
}
else{
+ require(ICryptoPunks(_getArgAddress(COLLATERAL_ADDRESS)).punkIndexToAddress(tokenId) == msg.sender);
ICryptoPunks(_getArgAddress(COLLATERAL_ADDRESS)).buyPunk(tokenId);
}
Discussion
grandizzy
will fix with the fix for https://github.com/sherlock-audit/2023-01-ajna-judging/issues/163
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
CryptoPunks NFT owners may lose their NFTs when trying to deposit them, as a malicious actor could front-run the deposit and withdraw the NFT.
What the attacker needed
Victim lists punk for sale to pool; attacker front-runs deposit transaction
The fix
The fix is to check that the caller is the owner of the CryptoPunk before buying it, as shown in the recommendation.
How it was found
- The token owner calls offerPunkForSaleToAddress, setting the toAddress to the pool's address.
- The token owner then calls addCollateral on the ERC721 pool.
- The pool buys the token from its owner.
- An attacker front-runs the owner's addCollateral call and calls addCollateral themselves.
- The pool buys the token and deposits it to the attacker's account.
- The attacker withdraws the NFT.
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.
CryptoPunks are NFTs that do not follow the usual ERC721 standard, so the pool uses a workaround: the owner lists the punk for sale to the pool's address, then calls addCollateral which buys it. But addCollateral does not check who is calling, so anyone can call it after the listing and the punk gets deposited to that caller instead of the real owner.
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.