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

Funds might be lost when the treasury address is changed.

Summary

In the PerpetualVault::setTreasury , when the treasury address is changed we do not track the previous addresses or transfer the funds already in the previous treasury to the new treasury address.

Vulnerability Details

When the owner changes the PerpetualVault::treasury, all funds held in the previous treasury address remains there. As we do not keep track of previous treasury addresses, the funds will be lost(locked) in the previous treasury addresses.

Impact

The protocol will end up losing all the funds that has been sent to the previous treasury address.

Proof of Code:

Please note that this a fork test. We need to use a fork url for arbitrum. You can get a fork url from [alchemy](https://www.alchemy.com/).

The test won't run if you do not use a fork url. To run the test, open a new terminal and run,

forge test --fork-url $YOUR_ARBITRUM_FORK_URL test/PerpetualVault.t.sol --mt test_FundsLostWhenTreasuryChanges -vvv

Add the following lines of code to the test/PerpetualVault.t.sol file. This fuzz test will revert because one or more of these conditions won't be met after a few runs.

function test_FundsLostWhenTreasuryChanges() external {
address alice = makeAddr("alice");
address ochuko = makeAddr("ochuko");
depositFixture(alice, 1e12);
vm.startPrank(alice);
uint256[] memory depositIds = PerpetualVault(vault).getUserDeposits(alice);
uint256 executionFee = PerpetualVault(vault).getExecutionGasLimit(false);
vm.expectRevert(Error.Locked.selector);
PerpetualVault(vault).withdraw{value: executionFee * tx.gasprice}(alice, depositIds[0]);
// simulate profit just by sending some tokens to the vault.
uint256 profit = 1e7;
IERC20 collateralToken = PerpetualVault(vault).collateralToken();
deal(address(collateralToken), alice, profit);
collateralToken.transfer(vault, profit);
uint256 lockTime = PerpetualVault(vault).lockTime();
vm.warp(block.timestamp + lockTime + 1);
PerpetualVault(vault).withdraw{value: executionFee * tx.gasprice}(alice, depositIds[0]);
uint256 feePercent = PerpetualVault(vault).governanceFee();
address treasury = PerpetualVault(vault).treasury();
uint256 collectedFee = collateralToken.balanceOf(treasury);
address owner = PerpetualVault(vault).owner();
vm.stopPrank();
vm.prank(owner);
PerpetualVault(vault).setTreasury(ochuko);
address newTreasury = PerpetualVault(vault).treasury();
uint256 collectedFeeLater = collateralToken.balanceOf(newTreasury);
assert(collectedFee != collectedFeeLater);
assertEq(collectedFeeLater, 0);
}

Recommendations

The issue stems from [PerpetualVault::setTreasury](https://github.com/CodeHawks-Contests/2025-02-gamma/blob/84b9da452fc84762378481fa39b4087b10bab5e0/contracts/PerpetualVault.sol#L712)

There are 2 possible solutions. The first solution is to track all previous treasury address in an array, preferable an enumerable set to prevent duplicate addresses. However, storing previous treasury addresses will take more slots in storage, especially if the treasury addresses change frequently.

The 2nd solution and best solution is to transfer all the funds in the previous address to the new address when we call PerpetualVault::setTreasury.

// add this event to it
+ event TreasuryChanged(address newTreasury);
function setTreasury(address _treasury) external onlyOwner {
if (_treasury == address(0)) revert Error.ZeroValue();
+ address previousTreasury = treasury;
+ uint256 treasuryBalance = collateralToken.balanceOf(treasury);
+ if (treasuryBalance > 0) {
+ collateralToken.safeTransferFrom(treasury, _treasury, treasuryBalance);
+ }
treasury = _treasury;
+ emit TreasuryChanged(_treasury);
}


Please note that for the safeTransferFromfunction to work, the previous treasury needs to approve the contract as spender. This is a test showing how to enable the contract as spender of the previous treasury funds.

function test_FundsLostWhenTreasuryChanges() external {
address alice = makeAddr("alice");
address ochuko = makeAddr("ochuko");
depositFixture(alice, 1e12);
vm.startPrank(alice);
uint256[] memory depositIds = PerpetualVault(vault).getUserDeposits(alice);
uint256 executionFee = PerpetualVault(vault).getExecutionGasLimit(false);
vm.expectRevert(Error.Locked.selector);
PerpetualVault(vault).withdraw{value: executionFee * tx.gasprice}(alice, depositIds[0]);
// simulate profit just by sending some tokens to the vault.
uint256 profit = 1e7;
IERC20 collateralToken = PerpetualVault(vault).collateralToken();
deal(address(collateralToken), alice, profit);
collateralToken.transfer(vault, profit);
uint256 lockTime = PerpetualVault(vault).lockTime();
vm.warp(block.timestamp + lockTime + 1);
PerpetualVault(vault).withdraw{value: executionFee * tx.gasprice}(alice, depositIds[0]);
uint256 feePercent = PerpetualVault(vault).governanceFee();
address treasury = PerpetualVault(vault).treasury();
uint256 collectedFee = collateralToken.balanceOf(treasury);
console.log("Collected fees in treasury:", collectedFee);
//console outputs the amount in the treasury
address owner = PerpetualVault(vault).owner();
vm.stopPrank();
vm.prank(treasury); //prank treasurer to approve the contract as a spender
collateralToken.approve(address(PerpetualVault(vault)), collectedFee);
console.log("Vault allowance", collateralToken.allowance(treasury, address(PerpetualVault(vault))));
//console output of the allowance of the treasury the contract can spend on behalf of the treasury
vm.prank(owner); //prank owner to change treasury
PerpetualVault(vault).setTreasury(ochuko);
address newTreasury = PerpetualVault(vault).treasury();
uint256 collectedFeeLater = collateralToken.balanceOf(newTreasury);
uint256 collectedFeeLaterOld = collateralToken.balanceOf(treasury);
console.log("Collected fees in new treasury:", collectedFeeLater);
//console the amount that has been transfered to the new treasury
console.log("fees in old treasury:", collectedFeeLaterOld);
//console return 0, meaning all funds held previously by the treasury has been transfered to the new treasury
assert(collectedFee == collectedFeeLater);
assertEq(collectedFeeLaterOld, 0);
}
Updates

Lead Judging Commences

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

Informational or Gas

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.

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 9 months ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity
Assigned finding tags:

Informational or Gas

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.

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.

Give us feedback!