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
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:
- Different behavior in
setContract()andconfigureGmxState()for handlingstakeGmxaddress changes. insetContract()the logic reset approval for old address to zero but inconfigureGmxState()the logic don’t reset old address GMX spending approval. - The call to this function would revert if
stakeGmxaddress didn’t changed but other addresses has been changed soownercan’t use this to configure contract. - Contract won’t reset approval for old
stakedGmxaddress 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. - Contract won’t reset approval for old
stakedGmxaddress, ifowneruseconfigureGmxState()to change thestakeGmxvalue then it won’t be possible to setstakedGmxvalue to previous ones by using eitherconfigureGmxState()orsetContract()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:
- Owner calls
configureGmxState()to set values for GMX contract addresses. - Then address of one of contract changes in GMX (
stakedGmxstayed the same) and owner wants to callconfigureGmxState()to reset the values of variables to correct ones. - Owner call to
configureGmxState()would fail becausestakedGmxaddress didn’t change and in the linegmx.safeApprove(address(stakedGmx), type(uint256).max);contract tries to set non zero approval forstakedGmxbut 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:
- GMX protocol get hacked (either by a bug or leaking some private keys) and
stakeGmxcontract control would be in hacker’s hand. - GMX deploy new contracts and
stakeGmxaddress changes inGMX.() - owner of
PirexGmxcallsconfigureGmxState()to reset the values of GMX contracts addresses inPirexGmx. - Function
configureGmxState()logic would change the GMX contract addresses but it won’t set GMX token spending approval for oldstakeGmxaddress to zero. - Hacker who control the old
stakeGmxaddress would use his access to that address contract to withdraw GMX balance ofPirexGmx.
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)
- GMX protocol changes its
stakeGmxcontract address from old address to new (for any reason, they got hacked or they are migrating or …) ownerofPirexGmxcallsconfigureGmxState()to update GMX protocol’s contract address inPirexGmxcontract and the logic would update the values of variables. (The GMX spending approval for old and newstakeGmxaddress would be max value).- GMX protocol changes
stakeGmxcontract address from new value to old value (for any reason, they decided to roll back to old address). - Owner tries to call
configureGmxState()to reupdate GMX protocol’s address inPirexGmxbut the call would revert because the code tries to callsafeApprove()for address that has already non-zero allowance. - Owner can’t call
setContract()to update value ofstakeGmxvariable because this function tries to callsafeApprove()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
initializeGmxStateinstead.
Maybe you could add a flag to prevent the method from being called multiple times as well.
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
- The owner calls configureGmxState to set the GMX contract addresses.
- One of the GMX contract addresses changes while stakedGmx stays the same, and the owner calls configureGmxState again.
- 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.