The mintRapper function in the OneShot file has a vulnerability where the ERC-721 token is distributed before updating the rapperStats
. This sequence allows a potential reentrancy attack, as a malicious contract can mint the NFT, triggered the onERC721Received
function in their contract, and then exploit the NFT to initiate a rapper battle using the goOnStageOrBattle
function in the rapperBattle
file. This results in the NFT having a rapperStats
value of 65 instead of the expected initialized value of 50, potentially impacting the outcome of battles and allowing users to gain an extra 15 points without proper staking.
The vulnerability arises from the order of execution in the mintRapper
function, allowing a reentrancy attack. A malicious contract can mint an NFT, receive it through the onERC721Received
function, and then immediately trigger a rapper battle, taking advantage of the uninitialized rapperStats
value. The impact of this vulnerability is verified through the MockERC721Receiver
contract and the testReentrancyIssue
test in RapBattleTest
, displaying a final skill value of 65 instead of the expected initialized value of 50.
Adding MockERC721Receiver
in OneShotTest.t.sol
:
Adding the test in RapBattleTest
test contract
In the console, it display the final skill as the following:
The result is 65 instead of 50.
This vulnerability enables users to gain an additional 15 points without proper staking, influencing the results of battles. It deviates from the original protocol design, potentially compromising the fairness and integrity of the system.
Manual Review
To mitigate the reentrancy attack and ensure proper initialization, the following recommendations are suggested:
Reentrancy Guard: Implement a reentrancy guard in relevant functions to introduce a mutex lock, protecting shared state variables from reentrancy attacks.
CEI Pattern (Check-Effect-Interaction): Reorder the statements in the mintRapper function to follow the CEI pattern. Ensure that the rapperStats are initialized before any battles are initiated, preventing the exploitation of uninitialized values in reentrancy attacks.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.