Skip to content
We manage your security to weather the AI storm →

Security review · October 2024

Buyback Updates, Part 2 for GMX

Guardian's review of Buyback Updates, Part 2 for GMX, published October 2024. The report records 15 findings across 2 review rounds, including 3 critical and 1 high.

See how Guardian runs a perp DEX smart contract audit.

Published
Review window
October 1 to 22, 2024
Rounds
Main Review, Remediation Review
Language
Solidity
Chains
Arbitrum, Avalanche
  • 3 Critical
  • 1 High
  • 1 Medium
  • 10 Low
  • 0 Informational

6 resolved · 9 acknowledged

Scope

Findings 15

Main Review

13 findings · October 1 to 7, 2024
  1. C-01 Critical Expected Fee Calculation Skews Output Logical Error Resolved
    Location
    FeeHandler.sol: 187
    Round
    Main Review

    Description

    The buyback mechanism aims to calculate how many fee tokens must be provided to the user for the batch amount of buyback tokens based on current oracle prices: uint256 expectedFeeTokenAmount = Precision.mulDiv(batchSize, feeTokenPrice, buybackTokenPrice);

    The issue is that batchSize represents a buyback token amount, and should be multiplied with the buybackTokenPrice to get the USD value of the batch, and then divide by the feeTokenPrice to calculate the worth in fee tokens. However, the current logic multiplies by feeTokenPrice and divides by buybackTokenPrice, leading to drastically different outputs of the swap.

    Consider the following scenario: Batch size: 1 GMX feeToken WBTC: $100,000 buybackToken GMX: $10

    The current logic would return 1 GMX * $100,000 / $10 = 10,000 BTC to provide to the user for 1 GMX, draining the contract’s available fees. This scenario also does not account for the varying decimal precision in the prices for each token, which also drastically modifies the expected output.

    Recommendation

    Modify the calculation to uint256 expectedFeeTokenAmount = Precision.mulDiv(batchSize, buybackTokenPrice, feeTokenPrice);

  2. C-02 Critical WNT Fees Cannot Be Used For GMX Buyback Validation Resolved
    Location
    FeeHandler.sol
    Round
    Main Review

    Description

    Function claimFees validates that the fee token batch size is zero, which prevents GMX and WNT from being the provided feeToken. This will prevent accumulated WNT fees from being used to buyback GMX tokens in the FeeHandler, since the available fee amounts will not be incremented for either buyback token.** **

    Recommendation

    Clearly document this behavior.

  3. C-03 Critical BUYBACK_MAX_PRICE_AGE Cannot Be Set DoS Resolved
    Location
    Config.sol
    Round
    Main Review

    Description

    BUYBACK_MAX_PRICE_AGE is not included in the allowedBaseKeys of the Config contract, which prevents it from being set, even by the keeper, as the _validateKey function will revert. As a result, maxPriceAge will be 0 in the _getMaxFeeTokenAmount function, causing all buybacks to revert unless they are executed at the exact same timestamp as the oracle update.

    Recommendation

    Set allowedBaseKeys[Keys.BUYBACK_MAX_PRICE_AGE] to true in the Config file.

  4. H-01 High V1 Fees Cannot Be Claimed DoS Resolved
    Location
    FeeHandler.sol: 71
    Round
    Main Review

    Description

    When withdrawing fees from V1, FeeHandler calls the withdrawFees function of the vaultGov, which is a timelock. However, this will always revert since the vaultGov.withdrawFees has an onlyAdmin modifier, and the FeeHandler is not admin of the timelock. Since there can be only one admin, other onlyAdmin functions like setLiquidator, addExcludedToken, setInPrivateTransferMode etc. cannot be called if the FeeHandler is set as admin.

    Recommendation

    Consider removing the V1 fee logic from the claimFees function. Instead, V1 fees should be regularly withdrawn and transferred to the FeeHandler by the V1 admin. Additionally, consider adding a separate admin function that updates the available fee amounts after each V1 fee transfer.

  5. M-01 Medium Lack of Interval Caps Validation Acknowledged
    Location
    FeeHandler.sol
    Round
    Main Review

    Description

    Each buyback token has a particular batch amount to limit how much can be sold by the user at a particular time. However, nothing prevents a user from calling the buyback function multiples times and swapping multiples batches within a small timeframe. This can be used to take advantage of any price discrepancies between the Chainlink oracles and market prices.

    Recommendation

    Consider limiting how much the protocol can buy back within a specific time interval.

  6. L-01 Low No Access Control on Claim Fees Access Control Acknowledged
    Location
    FeeHandler.sol: 63
    Round
    Main Review

    Description

    Currently any user can call function claimFees to withdraw fees from the GMX vault or claim accumulated GMXV2 fees. This can lead to a user continuously claiming fees while buybacks are active to prevent the protocol from claiming fees for itself when necessary.

    Recommendation

    Consider added onlyFeeKeeper access control.

  7. L-02 Low Output Amount Can Be Inaccurate Logical Error Resolved
    Location
    FeeHandler.sol: 105
    Round
    Main Review

    Description

    The current getOutputAmount function does not validate that markets are unique. Hence, if duplicates of a market were passed into the view function, the availableFeeAmount would be larger due to double-counting of available fees. Ultimately, the function would return an inaccurate output amount and this can affect the behavior of the UI.

    Recommendation

    Consider checking for duplicates in the markets parameter in the frontend and document this behavior.

  8. L-03 Low SafeTransferFrom Should Be First Best Practices Acknowledged
    Location
    FeeHandler.sol
    Round
    Main Review

    Description

    In function buyback, the safeTransferFrom occurs after the fee token is transferred. However it is a best practice to transfer funds in before performing other state updates.

    Recommendation

    Consider performing safeTransferFrom right after the initial validations and before the fee token is transferred to the user.

  9. L-04 Low Discounted GMX Buys Not Supported Warning Acknowledged
    Location
    FeeHandler.sol
    Round
    Main Review

    Description

    The existing buyback mechanism aims to apply a premium on GMX purchases: Precision.applyFactor(expectedFeeTokenAmount, maxPriceImpactFactor + Precision.FLOAT_PRECISION); This will increase how many fee tokens the user may receive per buyback token. For example, if the price of GMX is $100 and batch size is 10, max price impact is 0.3%, and fees for AAVE accumulate, the max worth of AAVE that would be swapped would be $1003.

    However, it may be prudent to also have a mechanism to discount GMX purchases so that users cannot extract value when the oracle price has yet to be updated to reflect latest market prices, or even to future-proof the contract as buyback incentive mechanisms may change with future proposals.

    Recommendation

    Clearly document the premium behavior to users and consider adding the ability to discount the GMX buyback price such as Precision.applyFactor(expectedFeeTokenAmount, Precision.FLOAT_PRECISION - maxDiscountFactor);

  10. L-05 Low buyback Uses Wrong Oracle Modifier Informational Acknowledged
    Location
    FeeHandler::L91
    Round
    Main Review

    Description

    buyback() allows any user to call it and does not use the typical keeper system seen throughout the GMX protocol. This allows a user to create an atomic swap. Although an atomic action is taking place, the modifier withOraclePrices() is used. This allows users to pass in price data that has a larger deadline than if the modifier withOraclePricesForAtomicAction() was used. Although maxPriceAge is validated, if it is set to too large of a value, then this can open up users to perform swaps with prices that deviate from the current market price. Additionally, if it is intended to still use Chainlink Price Feed, then using withOraclePrices() may cause unexpected reverts for a provider that is expected to be a Chainlink Data Stream.

    Recommendation

    If using withOraclePrices() is intended, verify that BUYBACK_MAX_PRICE_AGE is not set to too large of a value. Otherwise, use withOraclePricesForAtomicAction() instead.

  11. L-06 Low Consider Adding Withdraw Function For Fee Tokens Informational Acknowledged
    Location
    FeeHandler.sol
    Round
    Main Review

    Description

    Fee tokens are transferred to the FeeHandler contract, and users can purchase these fee tokens by providing buyback tokens. The keeper has the ability to withdraw buyback tokens but does not have the ability to withdraw fee tokens. If users do not utilize the buyback functionality, claimed fee tokens may have to remain in the FeeHandler contract for a long period of time and cannot be used for other purposes.

    Recommendation

    Consider adding a privileged withdraw function for fee tokens.

  12. L-07 Low Memory Variable Declared In For Loop Gas Optimization Resolved
    Location
    FeeHandler::L140
    Round
    Main Review

    Description

    getOutputAmount() declares feeAmounts, which is a memory variable, inside of a for loop. In Solidity, declaring a memory variable inside of a for loop stores a new variable to memory on each iteration. The first 22 words of memory are priced linearly, but the pricing is quadratic after that. This can lead to users having to pay substantially more in gas costs.

    Recommendation

    Declare feeAmounts outside of the for loop, and reassign in each iteration.

  13. L-08 Low Save WNT As Immutable Gas Optimization Acknowledged
    Location
    FeeHandler::L193
    Round
    Main Review

    Description

    _incrementAvailableFeeAmounts() gets the wrapped native token from storage. Since the wrapped native token will remain the same for each chain, it is safe to store it as an immutable variable. This will cut down on gas costs, since it will avoid reading from storage.

    Recommendation

    Store WNT as an immutable variable in the constructor similar to gmx.

Remediation Review

2 findings · October 22, 2024
  1. L-01 Low Fee Claims Can Continue When Buybacks Disabled Warning Acknowledged
    Location
    FeeHandler.sol
    Round
    Remediation Review

    Description

    Function claimFees can still be called by arbitrary users even if the batch sizes have been set to zero, effectively disabling buybacks. In such a scenario, fees could accrue in the contract but not be withdrawn.

    Recommendation

    Consider if some pause functionality may be necessary for fee claims.

  2. L-02 Low Events Emitted With Zero Transfers Warning Acknowledged
    Location
    FeeHandler.sol
    Round
    Remediation Review

    Description

    If zero tokens are claimed through FeeUtils.claimFees, a "FeesClaimed" event is still emitted. This can be done by calling claimFees before fees have accumulated, or passing an invalid fee token address.

    Recommendation

    Be aware and take this into consideration for offchain services.

More from GMX

All 60 reports
  1. Morpho Liquidation Module

    3 findings 3 findings: 3 informational
  2. RewardRouterV3 & RewardRouterUtils

    20 findings1 high 20 findings: 1 high, 8 low, 11 informational
  3. V2.2.1 Review

    102 findings8 high 102 findings: 8 high, 37 medium, 49 low, 8 informational
  4. Incentives Contracts

    17 findings 17 findings: 3 medium, 3 low, 11 informational

Put your code through the same review.

This review started with a conversation about scope. Tell us what you are building and we will plan yours with you.

Get a quote