First depositor attack possible through multiple attack paths because the deposit function does not check 0 shares received
The gHYBR vault's deposit function does not check that the depositor receives at least one share, so an attacker can inflate the share price and cause later depositors to receive zero shares, capturing their assets. The attacker can donate assets via the public deposit_for function, the unprotected receivePenaltyReward function, or a multiSplit withdrawal.
asui Follow with 0xBugSlayer, 0xDemon and 27 others
Originally disclosed on Code4rena
Submitted by asui, also found by 0xBugSlayer, 0xDemon, 0xPSB, Almanax, ayden, blokfrank, classic-k, CoheeYang, EtherEngineer, fullstop, Huntoor, ibrahimatix0x01, IzuMan, kestyvickky, khaye26, MoZi, odeili, osok, piki, queen, reidnerFM, rzizah, Sancybars, Sejin, shieldrey, silver_eth, the_haritz, zoox, and zubyoz
GovernanceHYBR.sol#L144GovernanceHYBR.sol#L492-L509GovernanceHYBR.sol#L238
Summary
The gHYBR contract is just another veNFT position holder from the perspective of votingEscrow contract, while the gHYBR contract acts as a vault.
And the deposit does not ensure that we mint at least one gHYBR share. This can lead to a contdition where the first depositor attacks another user.
Example:
- Alice deposits dust shares, 1 share : 1 asset
- Alice donates 1000e18 assets before Bob deposits, through
deposit_for, and he increased the ratio by1 shares : 1000e18 assets - Bob deposits 100e18 assets, the shares calculation goes
100e18 * 1 / 1000e18and rounds down to 0 - Receives 0 shares
- All bob’s deposit is captured by Alice’s shares
- Bob deposits 100e18 assets and receives 0 shares
- Alice has 1 share worth
1000e18 + 100e18(bob's) assets
The entry points the attacker can use to perform this attack are:
- The votingEscrow contract allows anyone to deposit assets for any position through its public
deposit_for(uint _tokenId, uint _value) external nonreentrantfunction. - The
receivePenaltyRewardfunction inGovernanceHYBRcontract lacks access controll, which allows an attacker to donate to increase totalAssets. - Attacker can utilize multiSplit through withdraw, by first depositing 1000:1000 and withdrawing so that the leftover is 1:1 ratio dust.
Recommended mitigation steps
My best suggestion is to require share > 0 in the deposit function in the GovernanceHYBR contract, and also add access control for the receivePenaltyReward function.
Proof of concept
function test_submissionValidity() external {
// first depositor attack
address alice = makeAddr("alice");
address bob = makeAddr("bob");
vm.startPrank(address(minter));
hybr.mint(alice, 2000e18);
hybr.mint(bob, 100e18);
vm.stopPrank();
vm.startPrank(alice);
hybr.approve(address(gHybr), type(uint).max);
gHybr.deposit(2, alice);
hybr.transfer(address(gHybr), 1000e18); // alice transfers 1000 HYBR to gHYBR
gHybr.receivePenaltyReward(1000e18); // alice donates 1000 through the receivePenaltyReward function
vm.stopPrank();
// bob deposits
vm.startPrank(bob);
hybr.approve(address(gHybr),100e18);
gHybr.deposit(100e18, bob);
vm.stopPrank();
console.log("alice's shares: ",gHybr.balanceOf(alice));
console.log("bob's shares: ",gHybr.balanceOf(bob));
console.log("gHYBR total assets: ",gHybr.totalAssets());
}
Run this test and see the result :
- Alice’s shares: 2
- Bob’s shares: 0
- gHYBR total assets: 1100000000000000000002
Alice captures all of Bob’s deposits.
Here, Alice used the receivePenaltyReward to donate and increase totalAssets. I want to show why access control is needed on receivePenaltyReward, Alice can also use other means to increase the totalAssets, like the ones mentioned above.
Note that the share calcuation is wrong, which makes the atack possible without even donation: i.e., first depositor only needs to deposit 1 dust asset for 1 share and he will capture all the send depositor’s tokens. But this is another issue and different from this one; this one will work even after the other one is fixed.
Added setMaxFee() function to make the fee cap configurable by the owner (up to 50%). This replaces the hardcoded limit and allows governance to adjust the maximum dynamic fee when needed.
Status: Mitigation confirmed. Full details in the mitigation review reports from niffylord, rayss, and ZanyBonzy.
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 deposit function does not require the calculated share amount to be greater than zero, and the receivePenaltyReward function lacks access control, allowing anyone to inflate totalAssets.
Impact
A later depositor could lose their entire deposit to the attacker, as the attacker's shares capture the deposited assets.
What the attacker needed
Attacker deposits first; attacker can donate via receivePenaltyReward or deposit_for; victim deposits after inflation
How it was found
- Alice deposits a tiny amount (2 wei) and receives 2 shares.
- Alice transfers 1000e18 HYBR to the gHYBR contract and calls receivePenaltyReward to donate, inflating totalAssets.
- Bob deposits 100e18 HYBR, but the share calculation rounds down to zero, so he receives no shares.
- Alice's shares now represent all of Bob's deposit.
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 'first depositor' attack on a vault that issues shares. The attacker makes one share very expensive, so when the next person deposits, the maths rounds their shares down to zero and the attacker keeps their money. The vault should refuse any deposit that would give the depositor zero shares.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.