BuyerAgent.purchase() function. Reentrancy occurs when a contract calls another contract, and that contract calls back into the original contract, potentially allowing an attacker to drain funds or manipulate state.BuyerAgent.purchase() function makes an external call to swan.purchase(asset) at line 251, and then writes to state variables inventory[round].push(asset) at line 248, isOracleRequestProcessed[taskId] = true at line 255, and spendings[round] += price at line 242.Creating a malicious contract that inherits from BuyerAgent.
Overriding the purchase() function to call back into the original BuyerAgent contract's purchase() function.
Manipulating the state variables to drain funds or gain unauthorized access.
BuyerAgent.purchase() function:Deployment and Execution:
Deploy the BuyerAgent contract and the Swan contract.
Deploy the ReentrancyAttack contract, passing the addresses of the BuyerAgent and Swan contracts as constructor arguments.
Call the attack function on the ReentrancyAttack contract.
Expected Behavior:
The attack function should call the purchase function on the BuyerAgent contract, which should then call the purchase function on the Swan contract. However, due to the reentrancy vulnerability, the purchase function on the BuyerAgent contract will be called again, allowing the attacker to drain funds or manipulate state.
Actual Behavior:
The attack function will successfully drain funds from the BuyerAgent contract by repeatedly calling the purchase function and reentering the contract.
Check the conditions and requirements for the purchase.
Update the state variables (effects).
Make the external call to swan.purchase(asset).
Here's an example of how the purchase() function could be modified:
By following the checks-effects-interactions pattern, you can prevent reentrancy attacks and ensure the security of your contract.
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.