Skip to content
Race condition Audit finding Advanced

Malicious user can frontrun withdrawals from Insurance Fund to significantly decrease value of shares

The insurance fund calculates the vUSD returned on withdrawal from the current vUSD balance, so a call to settle bad debt just before a withdrawal reduces that balance and the payout for the same shares. An attacker could front-run a withdrawal with a bad-debt settlement to make the victim receive significantly less vUSD.

0xDjango Follow with Delvir0, carrotsmuggler and 3 others

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-04-hubble-exchange-judging/issues/72

Found by

0xDjango, Delvir0, carrotsmuggler, dirk_y, lil.eth, rogue-lion-0619

Summary

When a user withdraws from the insurance fund, the value of their shares is calculated based on the balance of vUSD in the fund. Another user could deliberately frontrun (or frontrun by chance) the withdrawal with a call to settleBadDebt to significantly reduce the vUSD returned from the withdrawal with the same number of shares.

Vulnerability Detail

When a user wants to withdraw from the insurance pool they have to go through a 2 step withdrawal process. First they need to unbond their shares, and then they have to wait for the pre-determined unbonding period before they can withdraw the vUSD their shares are worth by calling withdraw.

When a user calls withdraw the amount of vUSD to redeem is calculated as:

Solidity
amount = balance() * shares / totalSupply();

where balance() is the balance of vUSD in the contract and totalSupply() is the total supply of share tokens. Therefore, if the balance of vUSD in the contract were to decrease, then the amount of vUSD redeemed from the same number of shares would decrease as a result.

This occurs when a trader's bad debt is settled when calling settleBadDebt in MarginAccount.sol as this calls insuranceFund.seizeBadDebt under the hood, which in turn calls settlePendingObligation which transfers vUSD out of the insurance fund to the margin account:

Solidity
vusd.safeTransfer(marginAccount, toTransfer);

The result is now that the balance of vUSD in the insurance fund is lower and thus the shares are worth less vUSD as a consequence.

Impact

A user withdrawing from the insurance fund could receive significantly less (potentially 0) vUSD when finalising their withdrawal.

Code Snippet

https://github.com/sherlock-audit/2023-04-hubble-exchange/blob/main/hubble-protocol/contracts/InsuranceFund.sol#L215 https://github.com/sherlock-audit/2023-04-hubble-exchange/blob/main/hubble-protocol/contracts/InsuranceFund.sol#L259-L261 https://github.com/sherlock-audit/2023-04-hubble-exchange/blob/main/hubble-protocol/contracts/InsuranceFund.sol#L153-L161

Tool used

Manual Review

Recommendation

One option would be to include a slippage parameter on the withdraw and withdrawFor methods so that the user redeeming shares can specify the minimum amount of vUSD they would accept for their shares.

When depositing into the insurance fund, the number of shares to mint is actually calculated based on the total value of the pool (value of vUSD and all other collateral assets). Therefore, the withdraw logic could also use _totalPoolValue instead of balance() to get a "true" value per share, however this could lead to withdrawals failing while assets are up for auction. Assuming all the assets are expected to be sold within the short 2 hour auction duration, this is probably the better solution given the pricing is more accurate, but it depends if users would accept failed withdrawals for short periods of time.

Discussion

asquare08

Withdrawals done after settleBadDebt and before the end of the auction of the seized collateral, will receive less USDC as compared to withdrawals done after the auction has ended. However, a user deliberately cannot front run all withdrawals with settleBadDebt as it will required generating bad debt first. Hence, the severity can be changed to low

ctf-sec

Emm worth checking the duplicate as well, the duplicate highlights the total supply can be inflated to make user mint less share as well

root cause is the totalSupply() can be inflated by transferring asset directly

for example duplicate #144, #224

recommend maintaining high severity

ctf-sec

Comment from senior Watson

Total supply can be inflated by donation but that would distribute the funds to other users in the pool making this a bigger loss than any kind of gain.

Assuming they owned the entire pool, which the only way to make donating profitable, the victim wouldn't lose any value because the donated assets would be auctioned.

When burning, donation of other assets doesn't affect the amount of vUSD received.

Best case I see for this is medium since there is potential for withdraws to be frontrun by calls to settle bad debt. Since there is no way to create bad debt arbitrarily it would be dependent on bad debt already existing but being unclaimed:

asquare08

agree with the above commnet

asquare08

As this case arises only in case of multi-collateral, we will fix this in later releases

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

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 withdrawal payout is based on the fund's current vUSD balance rather than a stable total pool value, so any transfer out of the fund changes the share price at withdrawal time.

Impact

A user withdrawing from the insurance fund could receive significantly less, potentially zero, vUSD when finalising their withdrawal; the maintainer notes this requires bad debt to already exist and be unclaimed, and the fix is deferred to later releases.

What the attacker needed

Existing unclaimed bad debt; attacker can call settleBadDebt; victim has unbonded shares and calls withdraw

The fix

The report recommends adding a slippage parameter to withdraw and withdrawFor so users can set a minimum vUSD amount, or using the total pool value instead of the vUSD balance; the maintainer says it will be fixed in later releases.

How it was found

  1. A user unbonds their insurance fund shares and waits for the unbonding period.
  2. The user calls withdraw to redeem vUSD for their shares.
  3. Another user front-runs the withdrawal with a call to settleBadDebt.
  4. settleBadDebt seizes bad debt from the insurance fund, transferring vUSD out.
  5. The withdrawal then calculates a lower vUSD amount for the same 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.

The insurance fund holds shares that are worth a slice of the fund's vUSD balance. If someone else causes vUSD to leave the fund just before you withdraw, your shares are suddenly worth less. The report says a trader's bad debt being settled can trigger that transfer, and an attacker could time it to hurt a withdrawal.

What is Race condition?

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.

More by 0xDjango

View all

23 more disclosed reports, best first.

More from Hubble Exchange

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Race conditions

The lesson behind this report.

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.