Open Chidubemkingsley opened 3 months ago
The provided code snippet is incorrect (it does not reflect the actual code of withdraw
) and hence the issue invalid.
The provided code snippet is incorrect (it does not reflect the actual code of
withdraw
) and hence the issue invalid.
function withdraw(uint256 tokenId) external {
require(ownerOf(tokenId) == msg.sender, "Only the owner can withdraw");
// Burn the NFT to complete the withdrawal process.
_burn(tokenId);
deposits[msg.sender] -= depositRequired;
(bool success, ) = msg.sender.call{value: depositRequired}(" ");
require(success, "Transfer failed");
}
}
This is the actual code and the one i provided was just a way to show how reentrancy was in the code. i was trying to show with comment how reentrancy was in the code as i didnt write it in the order as it was in the real code. I dont understand how this minor issue can get me invalidated. Thanks for understanding.
The vulnerability is not based on the actual code, making the issue based of it invalid. You should refer to the actual code, where it you can see that the low-level call is executed at the end and reentrancy is not possible since the token is already burned
Severity : High
Vulnerability Details:
The BuggyNFTVault contract has a reentrancy vulnerability in the withdraw function. The function transfers ETH to the caller before updating the user's deposit balance in the deposits mapping. This sequence of operations opens up the possibility for an attacker to re-enter the withdraw function multiple times within the same transaction, allowing them to withdraw more funds than they initially deposited.
Proof of Code:
Here is the problematic section of the withdraw function:
Impact:
The impact of this vulnerability is severe. An attacker can exploit this flaw to withdraw more funds than they originally deposited by repeatedly calling the withdraw function before their deposit balance is updated. This can lead to the depletion of the contract's ETH balance, resulting in significant financial losses for both the contract owner and legitimate users.
Recommendation:
To fix this vulnerability, update the deposits mapping before transferring ETH to the caller. This ensures that the user's balance is reduced before any external calls are made, mitigating the risk of reentrancy attacks.
Here's the corrected withdraw function:
Consider using OpenZeppelin's ReentrancyGuard to further protect against reentrancy attacks across the entire contract:
Then, modify the withdraw function to include the nonReentrant modifier:
Implementing these changes will effectively mitigate the reentrancy vulnerability and protect user funds.