Possible vulnerability allows an attacker to exploit the function by recursively calling it before the initial transaction is completed, potentially draining the contract of funds beyond the intended amount.
The function performs an external call to stakeController.unstake(_amount) before updating the contract's state or transferring tokens. This order of operations creates a window for reentrancy attacks, where an attacker can recursively call the withdraw function before the first call completes. Specifically, the stakeController.unstake(_amount) call creates the reentrancy opportunity in the following ways:
The unstake function is an external call to another contract. During this call, control is temporarily passed to the stakeController contract, which may have malicious code.
At the point of the unstake call, the Vault contract's state has not been updated to reflect the withdrawal. This means that if the stakeController contract calls back into the Vault, it will see the pre-withdrawal state.
If the stakeController contract is malicious or compromised, it could include logic in its unstake function to call back into the Vault's withdraw function before completing the unstaking process.
Due to the state not being updated, each recursive call to withdraw would process as if it were the first withdrawal, potentially allowing multiple withdrawals of the same funds.
This vulnerability is particularly dangerous because the Vault contract trusts the stakeController to behave correctly. If an attacker can control or manipulate the stakeController, they can exploit this trust to drain funds from the Vault.
Manual code review
To address the reentrancy vulnerability and improve overall security, consider these modifications:
Reorder operations in the `withdraw` function to update the contract's state before making external calls.
Implement OpenZeppelin's `ReentrancyGuard` to prevent recursive calls to sensitive functions.
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.