Preventing any user from calling the functions withdraw , redeem , or depositGmx in contract AutoPxGmx
The contract AutoPxGmx calls a compound function during withdraw, redeem, and depositGmx, which swaps a reward token for GMX with a minimum output of 1. An attacker can send a tiny amount of the reward token directly to the contract, causing the swap to revert because the output is less than 1, thereby blocking all users from withdrawing or depositing.
Submitted by HE1M
It is possible that an attacker can prevent any user from calling the functions withdraw, redeem, or depositGmx in contract AutoPxGmx by just manipulating the balance of token gmxBaseReward, so that during the function compound the swap will be reverted.
Proof of Concept
Whenever a user calls the functions withdraw, redeem, or depositGmx in contract AutoPxGmx, the function compound is called:
The function compound claims token gmxBaseReward from rewardModule: https://github.com/code-423n4/2022-11-redactedcartel/blob/03b71a8d395c02324cb9fdaf92401357da5b19d1/src/vaults/AutoPxGmx.sol#L262
Then, if the balance of the token gmxBaseReward in custodian of the contract AutoPxGmx is not zero, the token gmxBaseReward will be swapped to token GMX thrrough uniswap V3 by calling the function exactInputSingle. Then the total amount of token GMX in custodian of the contract AutoPxGmx will be deposited in the contract PirexGmx to receive token pxGMX:
if (gmxBaseRewardAmountIn != 0) {
gmxAmountOut = SWAP_ROUTER.exactInputSingle(
IV3SwapRouter.ExactInputSingleParams({
tokenIn: address(gmxBaseReward),
tokenOut: address(gmx),
fee: fee,
recipient: address(this),
amountIn: gmxBaseRewardAmountIn,
amountOutMinimum: amountOutMinimum,
sqrtPriceLimitX96: sqrtPriceLimitX96
})
);
// Deposit entire GMX balance for pxGMX, increasing the asset/share amount
(, pxGmxMintAmount, ) = PirexGmx(platform).depositGmx(
gmx.balanceOf(address(this)),
address(this)
);
}
Whenever the function compound is called inside the mentioned functions, the parameters are: compound(poolFee, 1, 0, true);
fee = poolFeeamountOutMinimum = 1sqrtPriceLimitX96 = 0optOutIncentive = true
The vulnerability is the parameter amountOutMinimum which is equal to 1. This provides an attack surface so that if the balance of token gmxBaseReward in AutoPxGmx is nonzero and small enough that does not worth 1 token GMX, the swap procedure will be reverted.
For example, if the balance of gmxBaseReward is equal to 1, then since the value of gmxBaseReward is lower than token GMX, the output amount of GMX after swapping gmxBaseReward will be zero, and as parameter amountOutMinimum is equal to 1, the swap will be reverted, and as a result, the compound function will be reverted.
Attack Scenario:
Suppose, recently the function compound was called, so the balance of token gmxBaseReward in contract AutoPxGmx is equal to zero. Later, Alice (honest user) would like to withdraw. So, she calls the function withdraw(...).
function withdraw(
uint256 assets,
address receiver,
address owner
) public override returns (uint256 shares) {
// Compound rewards and ensure they are properly accounted for prior to withdrawal calculation
compound(poolFee, 1, 0, true);
shares = previewWithdraw(assets); // No need to check for rounding error, previewWithdraw rounds up.
if (msg.sender != owner) {
uint256 allowed = allowance[owner][msg.sender]; // Saves gas for limited approvals.
if (allowed != type(uint256).max)
allowance[owner][msg.sender] = allowed - shares;
}
_burn(owner, shares);
emit Withdraw(msg.sender, receiver, owner, assets, shares);
asset.safeTransfer(receiver, assets);
}
In a normal situation, the function compound will be called, and since the balance of gmxBaseReward is zero, no swap will be executed in uniswap V3, and the rest of the code logic will be executed.
But in this scenario, before Alice’s transaction, Bob transfers 1 token gmxBaseReward directly to contract AutoPxGmx . So, when Alice’s transaction is going to be executed, inside the function compound the swap function will be called (because the balance gmxBaseReward is equal to 1 now). In the function exactInputSingle in uniswap V3, there is a check: require(amountOut >= params.amountOutMinimum, 'Too little received'); https://etherscan.io/address/0xe592427a0aece92de3edee1f18e0157c05861564#code#F1#L128
This check will be reverted, because 1 token of gmxBaseReward is worth less than 1 token of GMX, so the amount out will be zero which is smaller than amountOutMinimum.
In summary, an attacker before user’s deposit, checks the balance of token gmxBaseReward in AutoPxGmx. If this balance is equal to zero, the attacker transfers 1 token gmxBaseReward to contract AutoPxGmx, so the user’s transaction will be reverted, and user should wait until this balance reaches to the amount that worth more than or equal to 1 token GMX.
Recommended Mitigation Steps
The parameter amountOutMinimum should be equal to zero when the function compound is called. compound(poolFee, 0, 0, true);
Picodes (judge) decreased severity to Medium and commented:
The attack is unlikely: there is no real reason to do it as the attacker would have to pay gas, you need conditions on the price, and conditions on the reward amounts, as if rewards are accruing you won’t be able to do it, so downgrading to Medium severity.
However it’s true that it may be good to set a minimum amount for the swaps that depends on the
amountIn, or remove entirely theminAmountOutrequirement.
kphed (Redacted Cartel) commented:
Hi @Picodes, this is going to be resolved as a side effect of issue #321 being fixed (i.e.
compoundwill be updated to consider token balances w/o dependence on external factors to prevent DoS’ing of users). Wanted to flag it for your attention but ultimately up to you re: awarding. Thanks for your help ser.
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
The compound function uses a hardcoded minimum output amount of 1 for the swap, which can be triggered to fail by sending a small amount of the input token to the contract.
Impact
An attacker could block all users from withdrawing, redeeming, or depositing by making a tiny token transfer, though the judge noted the attack is unlikely due to gas costs and specific conditions.
What the attacker needed
Attacker can send tokens to the contract; reward token balance is zero or small; victim attempts withdraw, redeem, or deposit
The fix
The parameter amountOutMinimum should be set to zero when compound is called, or a minimum amount that depends on the input should be used.
How it was found
- Attacker checks the balance of gmxBaseReward in AutoPxGmx.
- If the balance is zero, the attacker transfers 1 token gmxBaseReward directly to AutoPxGmx.
- When a user later calls withdraw, redeem, or depositGmx, the compound function attempts to swap the 1 token.
- The swap reverts because the output amount is less than the minimum of 1, causing the user's transaction to fail.
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.
This is a denial-of-service bug. The contract tries to swap a small reward token for GMX, but it insists on getting at least 1 GMX back. If someone sends a tiny amount of the reward token to the contract, the swap fails because the amount is too small to get 1 GMX, and the whole withdrawal or deposit transaction reverts. So an attacker can stop everyone from using those functions by making a tiny transfer.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.