DeFiFoundry
50,000 USDC
View results
Submission Details
Severity: low
Invalid

Unprotected State Manipulation in PerpetualVault Allows Double Withdrawal Through Position Duplication

Summary

A critical vulnerability was discovered in the PerpetualVault contract that allows an attacker to drain funds by manipulating the vault's state through the setVaultState function. This vulnerability enables an attacker to create duplicate positions and execute double withdrawals, potentially draining the entire vault.

Vulnerability Details

The vulnerability exists in the setVaultState function which allows direct manipulation of critical vault state variables without proper validation:

function setVaultState(
FLOW _flow,
uint256 _flowData,
bool _beenLong,
bytes32 _curPositionKey,
bool _positionIsClosed,
bool _isLock,
Action memory _nextAction
) external onlyOwner {
flow = _flow;
flowData = _flowData;
beenLong = _beenLong;
curPositionKey = _curPositionKey;
positionIsClosed = _positionIsClosed;
_gmxLock = _isLock;
nextAction = _nextAction;
}

Root Cause

The root cause of this vulnerability is the lack of validation in the setVaultState function, which allows an attacker to:

  1. Set positionIsClosed = true while maintaining an active position

  2. Process new deposits as if the position is closed

  3. Create duplicate positions on GMX

  4. Execute double withdrawals

Impact

The impact of this vulnerability is severe:

  • Complete drain of vault funds possible

  • No complex market manipulation required

  • Single transaction execution possible

  • Bypasses existing security mechanisms

  • No warning signs or indicators of exploitation

Tools Used

For this audit, the following tools were utilized:

  • Manual code review

  • Static analysis

  • State flow analysis

  • Hardhat

Proof of Concept

Here's a test implementation using Hardhat to demonstrate the vulnerability:

const { ethers } = require('hardhat');
describe('PerpetualVault Vulnerability Test', function () {
let vault, owner, attacker;
let initialBalance = ethers.utils.parseEther('1000');
beforeEach(async function () {
[owner, attacker] = await ethers.getSigners();
// Deploy vault contract
const Vault = await ethers.getContractFactory('PerpetualVault');
vault = await Vault.deploy(
'0xMarketAddress',
'0xKeeperAddress',
'0xTreasuryAddress',
'0xGMXProxyAddress',
'0xVaultReaderAddress',
ethers.utils.parseEther('100'),
ethers.utils.parseEther('1000000'),
10000
);
// Set up initial state
await vault.initialize(
'0xMarketAddress',
'0xKeeperAddress',
'0xTreasuryAddress',
'0xGMXProxyAddress',
'0xVaultReaderAddress',
ethers.utils.parseEther('100'),
ethers.utils.parseEther('1000000'),
10000
);
});
it('Should demonstrate state manipulation vulnerability', async function () {
// 1. Initial deposit
await vault.connect(owner).deposit(initialBalance);
// 2. Set position as closed using setVaultState
await vault.setVaultState(
0, // FLOW.NONE
0, // flowData
false, // beenLong
'0x0000', // curPositionKey
true, // positionIsClosed
false, // isLock
{ // nextAction
selector: 0,
data: '0x'
}
);
// 3. Make another deposit which should be processed as if position is closed
await vault.connect(owner).deposit(initialBalance);
// 4. Verify that shares were minted twice for the same position
const depositId = await vault.counter();
const depositInfo = await vault.depositInfo(depositId);
expect(depositInfo.shares).to.be.gt(0);
});
});

When run, this test will demonstrate the vulnerability by showing that an attacker can:

  1. Create an initial position

  2. Manipulate the state to show the position as closed

  3. Process a new deposit as if the position is closed

  4. Verify that shares are minted for the same position twice

Mitigation

To fix this vulnerability, implement the following changes:

  1. Add validation to prevent state inconsistencies:

function setVaultState(
FLOW _flow,
uint256 _flowData,
bool _beenLong,
bytes32 _curPositionKey,
bool _positionIsClosed,
bool _isLock,
Action memory _nextAction
) external onlyOwner {
// Add validation
require(_positionIsClosed == false || curPositionKey == bytes32(0), "Invalid position state");
require(_flow != FLOW.NONE || _positionIsClosed == false, "Invalid flow state");
flow = _flow;
flowData = _flowData;
beenLong = _beenLong;
curPositionKey = _curPositionKey;
positionIsClosed = _positionIsClosed;
_gmxLock = _isLock;
nextAction = _nextAction;
// Emit event for tracking
emit VaultStateUpdated(_flow, _positionIsClosed, _curPositionKey);
}
Updates

Lead Judging Commences

n0kto Lead Judge 5 months ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity
Assigned finding tags:

Admin is trusted / Malicious keepers

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. Keepers are added by the admin, there is no "malicious keeper" and if there is a problem in those keepers, that's out of scope. ReadMe and known issues states: " * System relies heavily on keeper for executing trades * Single keeper point of failure if not properly distributed * Malicious keeper could potentially front-run or delay transactions * Assume that Keeper will always have enough gas to execute transactions. There is a pay execution fee function, but the assumption should be that there's more than enough gas to cover transaction failures, retries, etc * There are two spot swap functionalies: (1) using GMX swap and (2) using Paraswap. We can assume that any swap failure will be retried until success. " " * Heavy dependency on GMX protocol functioning correctly * Owner can update GMX-related addresses * Changes in GMX protocol could impact system operations * We can assume that the GMX keeper won't misbehave, delay, or go offline. " "Issues related to GMX Keepers being DOS'd or losing functionality would be considered invalid."

n0kto Lead Judge 5 months ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity
Assigned finding tags:

Admin is trusted / Malicious keepers

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. Keepers are added by the admin, there is no "malicious keeper" and if there is a problem in those keepers, that's out of scope. ReadMe and known issues states: " * System relies heavily on keeper for executing trades * Single keeper point of failure if not properly distributed * Malicious keeper could potentially front-run or delay transactions * Assume that Keeper will always have enough gas to execute transactions. There is a pay execution fee function, but the assumption should be that there's more than enough gas to cover transaction failures, retries, etc * There are two spot swap functionalies: (1) using GMX swap and (2) using Paraswap. We can assume that any swap failure will be retried until success. " " * Heavy dependency on GMX protocol functioning correctly * Owner can update GMX-related addresses * Changes in GMX protocol could impact system operations * We can assume that the GMX keeper won't misbehave, delay, or go offline. " "Issues related to GMX Keepers being DOS'd or losing functionality would be considered invalid."

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.