The PerpetualVault.afterOrderExecution function has a potential underflow issue when calculating the increased amount for share minting. This can cause transaction reverts, failed share minting, and locked funds.
When calculating the increased amount for share minting, the function subtracts 1 after deducting fees and price impact. If the total deductions exceed the deposit amount, this results in an underflow, causing the transaction to revert.
Root Cause:
The calculation of the increased amount in the PerpetualVault.afterOrderExecution function can cause an underflow if the total deductions exceed the deposit amount.
Proof of Concept:
Code Analysis:
Example Scenario:
Deposit:
User deposits 1,000 tokens.
amount = 1,000.
Fee and Price Impact Calculation:
Assume feeAmount = 500 and priceImpact = 500.
Total deductions: feeAmount + priceImpact + 1 = 500 + 500 + 1 = 1,001.
Underflow:
increased = amount - feeAmount - uint256(priceImpact) - 1 = 1,000 - 500 - 500 - 1 = -1.
This causes an underflow, and the transaction reverts.
Unit Test: copy and paste this code into PerpetualVault.t.sol add these:
import { Order } from "../contracts/libraries/Order.sol";
import { IGmxProxy } from "../contracts/interfaces/IGmxProxy.sol";
Failed share allocation leaves user deposits locked in the contract without corresponding shares, leading to loss of funds.
Manual
To prevent the underflow, ensure that the total deductions do not exceed the deposit amount. One approach is to add a check before performing the subtraction:
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.