Skip to content
Smart contracts Audit finding Intermediate

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

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 by 1 shares : 1000e18 assets
  • Bob deposits 100e18 assets, the shares calculation goes 100e18 * 1 / 1000e18 and 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:

  1. The votingEscrow contract allows anyone to deposit assets for any position through its public deposit_for(uint _tokenId, uint _value) external nonreentrant function.
  2. The receivePenaltyReward function in GovernanceHYBR contract lacks access controll, which allows an attacker to donate to increase totalAssets.
  3. 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.

Hybra Finance mitigated:

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.


The original report as published on Code4rena, by asui. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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

  1. Alice deposits a tiny amount (2 wei) and receives 2 shares.
  2. Alice transfers 1000e18 HYBR to the gHYBR contract and calls receivePenaltyReward to donate, inflating totalAssets.
  3. Bob deposits 100e18 HYBR, but the share calculation rounds down to zero, so he receives no shares.
  4. 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.

More by asui

View all

2 more disclosed reports, best first.

More from Hybra Finance

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.