Skip to content
Business logic Audit finding Intermediate

Assets deposited before calculating shares amount to mint will cause users to mint less shares

The deposit function adds the user's tokens to the voting escrow before calculating the shares to mint, so the new total assets (including the deposit) is used in the share calculation. This causes users to receive fewer shares than they should, effectively losing part of their deposit.

asui Follow with 0xauditagent, 0xBugSlayer and 31 others

Originally disclosed on Code4rena

Submitted by asui, also found by 0xauditagent, 0xBugSlayer, 0xJason, 0xnija, 0xRaz, Albert, axelot, ayden, Boy2000, classic-k, dee24, EtherEngineer, harry, InvarianteX, itsravin0x, kjc, KKKKK, kmkm, KuwaTakushi, LhoussainePh, luncy, mbuba666, oct0pwn, Olami978355, OnyxAudits, piki, silver_eth, Sourav_DEV, testnate, the_haritz, Vagner, ZanyBonzy, and zcai

GovernanceHYBR.sol #L137-L144

Summary

        } else {
            // Add to existing veNFT
            IERC20(HYBR).approve(votingEscrow, amount);
            IVotingEscrow(votingEscrow).deposit_for(veTokenId, amount);

            // Extend lock to maximum duration
            _extendLockToMax();
        }

        // Calculate shares to mint based on current totalAssets
        uint256 shares = calculateShares(amount);

        // Mint gHYBR shares
        _mint(recipient, shares);

As we can see, the GovernanceHYBR::deposit function first deposits the HYBR into the votingEscrow before calculating and minting shares.

This will deposit the tokens first increasing the totalAssets() and the new totalAssets() will be used in shares = calculateShares(amount)

This results in incorrect calculation of shares for the users beause their deposits are treated as rewards and they are minted shares with the new rate and will suffer slippage from their own tokens.

Example:

  • Initially Bob has a deposit of 100 gHYBR : 100 HYBR, ie.. 1:1 shares to asset ratio
  • Alice also enter with 100 assets(HYBR),
  • In an ideal condition, Alice is expected to recieve 100 shares because the ratio is 1:1 at the time of deposit
  • but because deposit is done first before calculating shares,
  • Alice will get, shares = 100 * 100 / (100 +100) i.e. only 50 shares

Impact

Loss of assets for users by minting less shares.

Recommended mitigation steps

Make sure that the ratio at the time of deposit must be used to calculate the shares to mint:

        IERC20(HYBR).transferFrom(msg.sender, address(this), amount);
+       uint256 shares = calculateShares(amount);

        // Initialize veNFT on first deposit
        if (veTokenId == 0) {
            _initializeVeNFT(amount);
        } else {
            // Add to existing veNFT
            IERC20(HYBR).approve(votingEscrow, amount);
            IVotingEscrow(votingEscrow).deposit_for(veTokenId, amount);

            // Extend lock to maximum duration
            _extendLockToMax();
        }

        // Calculate shares to mint based on current totalAssets
-       uint256 shares = calculateShares(amount);

Proof of concept

forge test --mt test_test_submissionValidity -vvv

    function test_submissionValidity() external {
        address bob = makeAddr("bob");
        address alice = makeAddr("alice");

        vm.startPrank(address(minter));
        hybr.mint(bob, 100e18);
        hybr.mint(alice, 100e18);
        vm.stopPrank();

        // bob mints gHYBR shares at 1:1 ratio
        vm.startPrank(bob);
        hybr.approve(address(gHybr), 100e18);
        gHybr.deposit(100e18, bob);
        uint bobShares = gHybr.balanceOf(bob);
        console.log("Bob shares: ", bobShares);
        uint shareToAssetRatio = gHybr.calculateAssets(1e18);
        vm.stopPrank();

        // Alice mints gHYBR shares at 1:1 ratio but gets less shares due to the incorrect shares calculation
        vm.startPrank(alice);
        hybr.approve(address(gHybr), 100e18);
        gHybr.deposit(100e18, alice);
        uint aliceShares = gHybr.balanceOf(alice);
        console.log("Alice shares: ", aliceShares);
        uint aliceShareToAssetRatio = gHybr.calculateAssets(1e18);
        vm.stopPrank();

        console.log("ratio after Bob deposits                                              : ", shareToAssetRatio);
        console.log("ratio after Alice deposits even when there was no rewards distribution: ", aliceShareToAssetRatio);
    }

We can see from the test console that Alice recieves less shares than bob with the same assset amount deposits.

Hybra Finance mitigated:

S-321 calculating shares use the pool ratio at the time of deposit S-352 check share is zero S- 101 too many locks check

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

The contract updates the total assets by depositing the user's tokens before calculating the shares, so the share calculation uses the post-deposit ratio instead of the pre-deposit ratio.

Impact

Users mint fewer shares than they should, resulting in a loss of assets.

What the attacker needed

A user makes a deposit

The fix

The share calculation should be moved before the deposit so that the ratio at the time of deposit is used.

How it was found

  1. Bob deposits 100 HYBR and receives 100 shares at a 1:1 ratio.
  2. Alice deposits 100 HYBR, but because the deposit is added to total assets first, she receives only 50 shares.

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 pool, you get shares representing your portion. The contract mistakenly counts your deposit as part of the pool before working out how many shares to give you, so you get fewer shares than you deserve. This means you lose some of your deposit.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

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.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.