_handleReturn
Function in PerpetualVault ContractThe _handleReturn
function interacts with external contracts via the _transferToken
call and potentially through the IGmxProxy(gmxProxy).refundExecutionFee
call. If these external calls are not properly secured against reentrancy attacks, an attacker could exploit this by recursively calling back into _handleReturn
or other functions within the same contract, leading to unintended behavior or financial loss.
An attacker might be able to drain funds from the contract or manipulate its internal state by exploiting a reentrancy vulnerability. This can result in significant financial losses for users and undermine the integrity of the smart contract system.
Consider the following scenario where _transferToken
is used to send tokens to an untrusted recipient:
An attacker deploys a malicious contract that implements a fallback function which calls back into _handleReturn
.
The attacker deposits collateral into the contract, ensuring their depositId triggers the vulnerable _transferToken
call.
During execution of _handleReturn
, when _transferToken
is invoked, it transfers tokens to the attacker's malicious contract.
The malicious contract’s fallback function executes and calls back into _handleReturn
, allowing the attacker to withdraw additional funds before the global state is updated (delete flowData; delete flow;
).
This recursive call allows the attacker to bypass the intended business logic, withdrawing more than they should or causing undesired side effects such as double-spending.
To prevent reentrancy attacks, implement the Checks-Effects-Interactions pattern. Update all mutable states before making any external calls. In your case, ensure that swapProgressData
, flowData
, and flow
are deleted prior to invoking _transferToken
or refundExecutionFee
.
Here’s how you can modify the _handleReturn
function:
Additionally, consider using OpenZeppelin's ReentrancyGuard
library, which provides a simple and effective mechanism to protect against reentrancy vulnerabilities. You can import the library and use the nonReentrant
modifier to prevent reentrancy attacks in your smart contract.
Please read the CodeHawks documentation to know which submissions are valid. If you disagree, provide a coded PoC and explain the real likelihood and the detailed impact on the mainnet without any supposition (if, it could, etc) to prove your point.
There is no real proof, concrete root cause, specific impact, or enough details in those submissions. Examples include: "It could happen" without specifying when, "If this impossible case happens," "Unexpected behavior," etc. Make a Proof of Concept (PoC) using external functions and realistic parameters. Do not test only the internal function where you think you found something.
Please read the CodeHawks documentation to know which submissions are valid. If you disagree, provide a coded PoC and explain the real likelihood and the detailed impact on the mainnet without any supposition (if, it could, etc) to prove your point.
There is no real proof, concrete root cause, specific impact, or enough details in those submissions. Examples include: "It could happen" without specifying when, "If this impossible case happens," "Unexpected behavior," etc. Make a Proof of Concept (PoC) using external functions and realistic parameters. Do not test only the internal function where you think you found something.
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.