Skip to content
Business logic Audit finding Intermediate

Reward tokens mismanagement can cause users losing rewards

The PirexRewards contract keeps its own owner-editable list of reward tokens, while PirexGmx hard codes a different list used to actually transfer rewards. If the owner removes a hard-coded token, adds an unsupported one, or empties the list, the claim function still zeroes the user's reward state and emits a Claim event without sending anything, so users silently lose rewards.

Jeiwan Follow with pashov, cryptonue and 6 others

Originally disclosed on Code4rena

Submitted by Jeiwan, also found by pashov, cryptonue, 0xbepresent, 141345, unforgiven, cryptoDave, Koolex, and datapunk

A user (which can also be one of the autocompounding contracts, AutoPxGlp or AutoPxGmx) can lose a reward as a result of reward tokens mismanagement by the owner.

Proof of Concept

The protocol defines a short list of reward tokens that are hard coded in the claimRewards function of the PirexGmx contract (PirexGmx.sol#L756-L759):

rewardTokens[0] = gmxBaseReward;
rewardTokens[1] = gmxBaseReward;
rewardTokens[2] = ERC20(pxGmx); // esGMX rewards distributed as pxGMX
rewardTokens[3] = ERC20(pxGmx);

The fact that these addresses are hard coded means that no other reward tokens will be supported by the protocol. However, the PirexRewards contract maintains a different list of reward tokens, one per producer token (PirexRewards.sol#L19-L31):

struct ProducerToken {
    ERC20[] rewardTokens;
    GlobalState globalState;
    mapping(address => UserState) userStates;
    mapping(ERC20 => uint256) rewardStates;
    mapping(address => mapping(ERC20 => address)) rewardRecipients;
}

// Producer tokens mapped to their data
mapping(ERC20 => ProducerToken) public producerTokens;

These reward tokens can be added (PirexRewards.sol#L151) or removed (PirexRewards.sol#L179) by the owner, which creates the possibility of mismanagement:

  1. The owner can mistakenly remove one of the reward tokens hard coded in the PirexGmx contract;
  2. The owner can add reward tokens that are not supported by the PirexGmx contract.

Such mismanagement can cause users to lose rewards for two reasons:

  1. Reward state of a user is updated before their rewards are claimed;
  2. It’s the reward token addresses set by the owner of the PirexRewards contract that are used to transfer rewards.

In the claim function:

  1. harvest is called to pull rewards from GMX (PirexRewards.sol#L377):

    harvest();
    
  2. claimReward is called on PirexGmx to pull rewards from GMX and get the hard coded lists of producer tokens, reward tokens, and amounts (PirexRewards.sol#L346-L347):

    (_producerTokens, rewardTokens, rewardAmounts) = producer
    .claimRewards();
    
  3. Rewards are recorded for each of the hard coded reward token (PirexRewards.sol#L361):

    if (r != 0) {
        producerState.rewardStates[rewardTokens[i]] += r;
    }
    
  4. Later in the claim function, owner-set reward tokens are read (PirexRewards.sol#L386-L387):

    ERC20[] memory rewardTokens = p.rewardTokens;
    uint256 rLen = rewardTokens.length;
    
  5. User reward state is set to 0, which means they’ve claimed their entire share of rewards (PirexRewards.sol#L391), however this is done before a reward is actually claimed:

    p.userStates[user].rewards = 0;
    
  6. The owner-set reward tokens are iterated and the previously recorded rewards are distributed (PirexRewards.sol#L396-L415):

    for (uint256 i; i < rLen; ++i) {
        ERC20 rewardToken = rewardTokens[i];
        address rewardRecipient = p.rewardRecipients[user][rewardToken];
        address recipient = rewardRecipient != address(0)
            ? rewardRecipient
            : user;
        uint256 rewardState = p.rewardStates[rewardToken];
        uint256 amount = (rewardState * userRewards) / globalRewards;
    
        if (amount != 0) {
            // Update reward state (i.e. amount) to reflect reward tokens transferred out
            p.rewardStates[rewardToken] = rewardState - amount;
    
            producer.claimUserReward(
                address(rewardToken),
                amount,
                recipient
            );
        }
    }
    

In the above loop, there can be multiple reasons for rewards to not be sent:

  1. One of the hard coded reward tokens is missing in the owner-set reward tokens list;
  2. The owner-set reward token list contains a token that’s not supported by PirexGmx (i.e. it’s not in the hard coded reward tokens list);
  3. The rewardTokens array of a producer token turns out to be empty due to mismanagement by the owner.

In all of the above situations, rewards won’t be sent, however user’s reward state will still be set to 0.

Also, notice that calling claim won’t revert if reward tokens are misconfigured, and the Claim event will be emitted successfully, which makes reward tokens mismanagement hard to detect.

The amount of lost rewards can be different depending on how much GMX a user has staked and how often they claim rewards. Of course, if a mistake isn’t detected quickly, multiple users can suffer from this issue. The autocompounding contracts (AutoPxGlp and AutoPxGmx) are also users of the protocol, and since they’re intended to hold big amounts of real users’ deposits (they’ll probably be the biggest stakers), lost rewards can be big.

Recommended Mitigation Steps

Consider having one source of reward tokens. Since they’re already hard coded in the PirexGmx contract, consider exposing them so that PirexRewards could read them in the claim function. This change will also mean that the addRewardToken and removeRewardToken functions won’t be needed, which makes contract management simpler.

Also, in the claim function, consider updating global and user reward states only after ensuring that at least one reward token was distributed.

drahrealm (Redacted Cartel) disagreed with severity and commented:

To make sure this won’t be an issue, we will add the whenNotPaused modifier to claimUserReward method in PirexGmx. Also, as migrateRewards is going to be updated to also set the pirexRewards address to 0, it will defer any call to claim the rewards

Picodes (judge) commented:

Seems to be the mitigation for #249.


The original report as published on Code4rena, by Jeiwan. 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 reward token list used for accounting is maintained separately from the hard-coded list used for transfers, and user reward state is reset before any transfer is confirmed.

Impact

Users, including the autocompounding contracts holding large deposits, could permanently lose unclaimed rewards, and the loss is hard to detect because the claim appears to succeed.

What the attacker needed

Owner misconfigures reward token list; user has staked and claims rewards

The fix

The team said it would add a pause check to claimUserReward and zero the PirexRewards address during migration; the report recommends a single source of reward tokens and updating reward state only after a successful distribution.

How it was found

  1. The owner removes a hard-coded reward token, adds an unsupported one, or leaves the producer token list empty.
  2. A user calls claim, which harvests rewards and records amounts against the hard-coded tokens.
  3. The user's reward state is set to zero before any transfer happens.
  4. The loop over the owner-set tokens finds no matching token or amount, so nothing is transferred.
  5. The call does not revert and the Claim event is emitted, hiding the loss.

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 staking contract tracks how much reward each person has earned. When someone claims, it first marks their balance as zero and then pays out from a list of tokens the owner controls. If that list does not match the tokens the payout contract actually supports, the payment step quietly does nothing, but the balance is already wiped, so the reward is gone for good.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by Jeiwan

View all

69 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.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.