Reentrancy bug in Basket’s withdraw multiple tokens function which gives attacker ability to transfer basket ownership and spend it but withdraw all the tokens out of basket
The Basket contract checks ownership only at the start of a batch withdrawal, so an attacker can transfer the basket NFT to a vault mid-withdrawal and still drain all tokens. This could let a third-party contract that buys the vault lose funds.
Originally disclosed on Code4rena
Submitted by unforgiven
Impact
Basket is used for keep multiple tokens in contract and mint one NFT token to represent their ownership. Basket only allows for owner of NFT(id=0) to withdraw tokens from Basket address. users can deposit multiple tokens in one Basket and then create a NibbVault based on that Basket NFT. but due to reentrancy vulnerability in Basket it’s possible to call the multiple-token-withdraw functions (withdrawMultipleERC721(), withdrawMultipleERC1155(), withdrawMultipleERC721() and withdrawMultipleERC20()) and in the middle their external calls, spend Basket NFT (transfer ownership of id=0 to other contract, for example createVault()) and receive some fund from other, then in the rest of the multiple-token-withdraw function withdraw all the basket tokens. Basket shouldn’t allow transferring ownership of id=0 in the middle of multiple token withdraws.
Proof of Concept
This is withdrawMultipleERC721() code:
function withdrawMultipleERC721(address[] memory _tokens, uint256[] memory _tokenId, address _to) external override {
require(_isApprovedOrOwner(msg.sender, 0), "withdraw:not allowed");
for (uint256 i = 0; i < _tokens.length; i++) {
IERC721(_tokens[i]).safeTransferFrom(address(this), _to, _tokenId[i]);
emit WithdrawERC721(_tokens[i], _tokenId[i], _to);
}
}
As you can see, contract only checks the ownership of id=0 in the beginning of the function to see that user allowed to perform this action or not. then it iterates through user specified addresses and call safeTransferFrom() function in those address by user specified values. the bug is that in the middle of the external calls attacker can spend Basket NFT id=0 (give ownership of that basket to other contracts and receive fund from them, for example attacker can call createVault in NibblVaultFactory and create a vault and call other contracts to invest in that vault) then in the rest of the iterations in withdrawMultipleERC721() attacker can withdraw Basket tokens. so even so the ownership of the Basket has been transferred and attacker received funds for it, attacker withdraw Basket tokens too.
This is the steps attacker would perform:
- Create a
Basketwith well knownNFTtoken list. let’s assume theBasketname isBasket_M - Give approve permission to
NibblVaultFactoryforBasket_M id=0token. - Call
Basket_M.withdrawMultipleERC721(address[] memory _tokens, uint256[] memory _tokenId, address _to)with list of all the tokens in basket to withdraw all of them, but the first address in the_tokenslist is the address that attacker controls. Basket_Mwould check that attacker is owner of the basket (owner of theid = 0) and in first iteration of theforit would call attacker controlled address which is a contract that attacker wrote its code.- Attacker contract would call
NibblVaultFactory.createVault()withBasket_Maddress andid=0to create a vault which then transfer the ownership ofBasket_M id=0to the vault address. let’s assume it’sVault_M. - Attacker contract would buy some fraction of
Vault_Mby callingbuy()function. - Let’s assume there are other contracts(call it
Invest_Contract) that would want to buy fraction of the well knownNFTs in the basket andInvest_Contractinvest some fund in vault having thoseNFTin vault’s address or vault’s basket just by callingInvest_Contract. attacker contract would callInvest_Contractto invest inVault_MandInvest_Contractwould check that well knownNFTis inBasket_M id=0which belongs toVault_Mto it would invest money on it by callinginitiateBuyout() - Attacker contract then withdraw his money from
Vault_M. - The rest of
Basket_M.withdrawMultipleERC721()for iterations performs and all theNFTtokens of theBasket_Mwould be send to attacker andBasket_Mwould have nothing.
Steps 5 to 8 can be other things, the point is in those steps attacker would spent Basket_M and receive some fund from other contract while those other contracts checks that they are owner of the Basket_M which has well known NFT tokens, but in fact attacker withdraw those well known NFT tokens from Basket_M after spending it in the rest of the withdrawMultipleERC721() iterations. (those above step 5-8 is just a sample case)
So Basket shouldn’t allow ownership transfer in the middle of the Basket_M.withdrawMultipleERC721() and similar multiple-token-withdraw functions or it should check the ownership in every iteration.
Tools Used
VIM
Recommended Mitigation Steps
Check ownership of id=0 in every iteration or don’t allow ownership transfer in the multiple-token-transfer functions.
mundhrakeshav (Nibbl) disputed
Alex the Entreprenerd (warden) commented:
Contract checks if you own it as owner of Basket has bought it, and as such is entitled to underlying tokens.
KenzoAgada (warden) commented:
The attack is contingent on a regular user, creating a smart contract, which allows anybody to call it, which checks that a parameter-supplied Nibbl vault contains a Nibbl basket which contains a specific NFT, and then proceeds to buyout/buy shares of that vault.
Honestly it seems like the vector of attack is possible but quite far fetched.
HardlyDifficult (judge) decreased severity to Medium and commented:
Creative thinking, this is what I’m here for!
If I’m following the flow correctly.. you kick off a bulk withdraw from the basket, the first NFT in the list is a malicious contract which then creates a vault for that basket (so the contract needs to be the basket owner, which is okay). Now the vault is fully created for that basket which still has valuable NFTs in it but you’re mid-tx. Your malicious contract pings other contracts which can be prompted to ape in via on-chain logic — their logic confirms all looks well and buys. But then control returns to the original batch withdrawal and the basket is drained.
Honestly it seems like the vector of attack is possible but quite far fetched.
I think I’d agree. To put it in terms of risk, this is not High: “valid attack path that does not have hand-wavy hypotheticals” — this sounds a bit hand-wavy. Namely because it assumes
Invest_Contractallows any address to trigger a purchase using other users funds which seems risky. And the victim here isInvest_Contract, not regular users of the protocol. Lowering to Medium. Great stuff though.
Alex the Entreprenerd (warden) commented:
The finding in essence is claiming that you can setup an empty
Basketand sell it to external contracts, and those contracts would lose funds.If that were the case the vulnerability would be in the “sniping / buying” contracts and not in the Basket nor the Vault.
The only thing the warden has shown is that they can create a Basket with a malicious token and through that they can call the Factory to create a Vault which after the tx will be empty.
This is logically equivalent to selling an empty vault, or selling a vault of
BryptoPunks(typo on purpose, it’s a scam token).
HardlyDifficult (judge) commented:
@Alex the Entreprenerd - I agree in terms of normal usage. The key here is a 3rd party contract uses on-chain logic in order to authorize a purchase. If that were the case, while in the middle of the attack as described all checks that contract may perform would confirm assets were included and terms look good — it would not be able to determine that the basket was in the middle of a batch withdraw request. Let me know if I’m overlooking something.
The basket itself does not need to hold a malicious token — the withdraw request takes an array of addresses, so the malicious contract only need to appear there.
Alex the Entreprenerd (warden) commented:
If that were the case, while in the middle of the attack as described all checks that contract may perform would confirm assets were included and terms look good — it would not be able to determine that the basket was in the middle of a batch withdraw request. Let me know if I’m overlooking something.
The 3rd party contract would need to check that the Basket is properly set via
ownerOf(Basket) == Vault(where Vault is an address contained in the list ofnibbledVaultsfrom factory).
That would allow to determine if the contract is properly setup.
The basket itself does not need to hold a malicious token,
That is correct as you can setup any contract to accept the
safeTransferFromcall.My statement is that an automated 3rd party contract can get rekt, but that’s not the contract under audit.
HardlyDifficult (judge) commented:
Agree that it’s the 3rd party contract that suffers a loss.
An
ownerOf(Basket) == Vault && ownerOf(NFT) == Basketcheck is insufficient here because if it’s in the middle of this scenario then the owner checks will appear legit but by the end of the tx they won’t be. That’s the part that I’m still hung up on. Part of the Medium definition is “the function of the protocol or its availability could be impacted” — although not an explicit goal, is it not implicit that protocols can be built upon with other contracts. The concern here seems to limit that ability, one could not build a contract that decided to participate based on on-chain state alone w/ or w/o an allow list of NFTs — it would require a trusted actor to allow list specific vaults or to perform the action itself.This is certainly grey though. Very hypothetical, e.g. it’s not clear that a 3rd party contract would ever be interested in a capability like this. This is an interesting discussion! I’ll sleep on it, but please chime in if you have more to add - I appreciate the feedback.
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 ownership check is performed only once before the loop, and the external calls inside the loop allow the ownership to change before the remaining transfers.
Impact
A third-party contract that buys the vault based on on-chain state could lose funds, but the attack is considered far-fetched and depends on such a contract existing.
What the attacker needed
Attacker can create a Basket; attacker can deploy a malicious contract; a third-party contract buys based on on-chain state
The fix
The ownership of id=0 should be checked in every iteration, or ownership transfer should be disallowed during multiple-token withdrawals.
How it was found
- Create a Basket with a known NFT token list.
- Approve NibblVaultFactory for the Basket NFT.
- Call withdrawMultipleERC721 with the attacker's contract as the first token address.
- The attacker's contract creates a vault for the Basket, transferring ownership.
- The attacker's contract buys a fraction of the vault and triggers an external investment contract to buy in.
- The attacker withdraws their money from the vault, then the remaining withdrawals drain the Basket.
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.
A smart contract can call another contract, and that call can run code that changes things. Here, the basket checks you own it only once at the start, then sends tokens one by one. In between, you can sell the basket, and the basket still sends you all its tokens.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.