Skip to content
Access control Audit finding Intermediate

broken logic in configureGmxState() of PirexGmx contract because it doesn’t properly call safeApprove() for stakedGmx address

The configureGmxState function approves the new stakedGmx address for unlimited GMX spending but never resets the allowance of the previous stakedGmx address, unlike setContract. This leaves a stale unlimited approval that a compromised old stakedGmx contract could use to drain the contract's GMX balance, and repeated calls can revert or leave the contract stuck.

unforgiven Follow with eierina, Jeiwan and 1 other

Originally disclosed on Code4rena

Submitted by unforgiven, also found by eierina, Jeiwan, and imare

https://github.com/code-423n4/2022-11-redactedcartel/blob/03b71a8d395c02324cb9fdaf92401357da5b19d1/src/PirexGmx.sol#L269-L293

https://github.com/code-423n4/2022-11-redactedcartel/blob/03b71a8d395c02324cb9fdaf92401357da5b19d1/src/PirexGmx.sol#L346-L355

Impact

Function configureGmxState() of PirexGmx is for configuring GMX contract state but logic is using safeApprove() improperly, it won’t reset approval amount for old stakedGmx address This would cause 4 problem:

  1. Different behavior in setContract() and configureGmxState() for handling stakeGmx address changes. in setContract() the logic reset approval for old address to zero but in configureGmxState() the logic don’t reset old address GMX spending approval.
  2. The call to this function would revert if stakeGmx address didn’t changed but other addresses has been changed so owner can’t use this to configure contract.
  3. Contract won’t reset approval for old stakedGmx address which is a threat because contract in that address can steal all the GMX balance any time in the future if that old address had been compromised.
  4. Contract won’t reset approval for old stakedGmx address, if owner use configureGmxState() to change the stakeGmx value then it won’t be possible to set stakedGmx value to previous ones by using either configureGmxState() or setContract() and contract would be in broken state.

Proof of Concept

This is configureGmxState() code in PirexGmx:

    /**
        @notice Configure GMX contract state
     */
    function configureGmxState() external onlyOwner whenPaused {
        // Variables which can be assigned by reading previously-set GMX contracts
        rewardTrackerGmx = RewardTracker(gmxRewardRouterV2.feeGmxTracker());
        rewardTrackerGlp = RewardTracker(gmxRewardRouterV2.feeGlpTracker());
        feeStakedGlp = RewardTracker(gmxRewardRouterV2.stakedGlpTracker());
        stakedGmx = RewardTracker(gmxRewardRouterV2.stakedGmxTracker());
        glpManager = gmxRewardRouterV2.glpManager();
        gmxVault = IVault(IGlpManager(glpManager).vault());

        emit ConfigureGmxState(
            msg.sender,
            rewardTrackerGmx,
            rewardTrackerGlp,
            feeStakedGlp,
            stakedGmx,
            glpManager,
            gmxVault
        );

        // Approve GMX to enable staking
        gmx.safeApprove(address(stakedGmx), type(uint256).max);
    }

As you can see it just sets the approval for new stakeGmx address and doesn’t do anything about spending approval of old stakeGmx address.

This is part of the setContract() code that handels stakeGmx:

        if (c == Contracts.StakedGmx) {
            // Set the current stakedGmx (pending change) approval amount to 0
            gmx.safeApprove(address(stakedGmx), 0);

            stakedGmx = RewardTracker(contractAddress);

            // Approve the new stakedGmx contract address allowance to the max
            gmx.safeApprove(contractAddress, type(uint256).max);
            return;
        }

As you can see it resets the spending approval of old stakedGmx address to zero and then give unlimited spending approval for new address.

So the impact #1 is obvious that the code for same logic in two different functions don’t behave similarly.

Function configureGmxState() is used for configuring GMX contract state but if owner uses this function one time then it won’t be possible to call this function the second time if stakedGmx wasn’t changed. For example in this scenario:

  1. Owner calls configureGmxState() to set values for GMX contract addresses.
  2. Then address of one of contract changes in GMX (stakedGmx stayed the same) and owner wants to call configureGmxState() to reset the values of variables to correct ones.
  3. Owner call to configureGmxState() would fail because stakedGmx address didn’t change and in the line gmx.safeApprove(address(stakedGmx), type(uint256).max); contract tries to set non zero approval for stakedGmx but it already has non zero spending allowance. (safeApprove() would revert if the current spending allowance is not zero and the new allowance is not zero either).

So the impact #2 would happen and calls to this function in some scenarios would fail and it won’t be functional.

Because function configureGmxState() doesn’t reset the old stakeGmx addresses GMX token spending approval to 0x0 so it would be possible to lose all GMX balance of PirexGmx contract if the old stakeGmx addresses are compromised. For example in this scenario:

  1. GMX protocol get hacked (either by a bug or leaking some private keys) and stakeGmx contract control would be in hacker’s hand.
  2. GMX deploy new contracts and stakeGmx address changes in GMX.()
  3. owner of PirexGmx calls configureGmxState() to reset the values of GMX contracts addresses in PirexGmx.
  4. Function configureGmxState() logic would change the GMX contract addresses but it won’t set GMX token spending approval for old stakeGmx address to zero.
  5. Hacker who control the old stakeGmx address would use his access to that address contract to withdraw GMX balance of PirexGmx.

BPirexGmx won’t set approval for old stakeGmx contract so it would be possible for that old stakeGmx address to transfer GMX balance of PirexGmx anytime in future. The bug in old stakeGmx contract or leakage of private keys of stakeGmx address (private key who can become the owner or admin of that contract) can happen after migrating GMX and Pirex contracts too. This is impact #3 scenario.

In scenario #4, contract would be stuck in unrecoverable state. The problem is that if configureGmxState() gets used more than once and stakeGmx variable’s value has been changes more than once then it won’t be possible to set stakeGmx value to old values either with configureGmxState() or setContract() and the contract won’t be useful. The scenario is this: (safeApprove() won’t allow to set non-zero approval for address that has already non-zero approval amount. See the OZ implementation)

  1. GMX protocol changes its stakeGmx contract address from old address to new (for any reason, they got hacked or they are migrating or …)
  2. owner of PirexGmx calls configureGmxState() to update GMX protocol’s contract address in PirexGmx contract and the logic would update the values of variables. (The GMX spending approval for old and new stakeGmx address would be max value).
  3. GMX protocol changes stakeGmx contract address from new value to old value (for any reason, they decided to roll back to old address).
  4. Owner tries to call configureGmxState() to reupdate GMX protocol’s address in PirexGmx but the call would revert because the code tries to call safeApprove() for address that has already non-zero allowance.
  5. Owner can’t call setContract() to update value of stakeGmx variable because this function tries to call safeApprove() to set non-zero approval value for address that already has non-zero allowance.

So in this state owner can’t recover PirexGmx contract and because contract has wrong value for stakeGmx it won’t be functional and it would stay in broken state.

Tools Used

VIM

Recommended Mitigation Steps

Like setContract(), function configureGmxState() should set approval for old PirexGmx to zero first.

drahrealm (Redacted Cartel) confirmed, but disagreed with severity and commented:

The method was meant to be called only once (ie. right before unpausing the contract to make it live). To make it clearer, we will change the method name to initializeGmxState instead.

Picodes (judge) commented:

Maybe you could add a flag to prevent the method from being called multiple times as well.


The original report as published on Code4rena, by unforgiven. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the function calls safeApprove to set a maximum allowance on the new stakedGmx address without first zeroing the allowance of the previous stakedGmx address, and safeApprove reverts when changing a non-zero allowance to another non-zero value.

Impact

A compromised or buggy old stakedGmx contract could transfer the contract's entire GMX balance at any future time, and repeated address changes could leave the contract in an unrecoverable state where neither configureGmxState nor setContract can restore it.

What the attacker needed

Owner calls configureGmxState; old stakedGmx address compromised or buggy

The fix

The function should reset the allowance of the old stakedGmx address to zero before approving the new one, as setContract does; the team said it was intended to run only once and planned to rename it to initializeGmxState.

How it was found

  1. The owner calls configureGmxState to set the GMX contract addresses.
  2. One of the GMX contract addresses changes while stakedGmx stays the same, and the owner calls configureGmxState again.
  3. The call reverts because safeApprove tries to set a non-zero allowance on an address that already has one.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

A smart contract can let another contract spend its tokens by giving it an allowance, like a standing permission. This function gives the new staking contract unlimited permission but forgets to cancel the old one, so the old contract keeps the power to move all the tokens. If that old contract is ever taken over, the attacker can use the leftover permission to take the funds.

What is Access control?

A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.

More by unforgiven

View all

50 more disclosed reports, best first.

More from Redacted Cartel contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Broken access control

The lesson behind this report.

A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.