Skip to content
Business logic Audit finding Intermediate

Emergency withdrawal fails to update the guage when the contract is not killed

The emergency_withdraw function skips the gauge checkpoint that normal deposit and withdraw functions perform, allowing an attacker to manipulate reward distributions. An attacker could borrow funds, inflate apparent activity, and withdraw without updating the gauge, potentially stealing or unfairly claiming rewards.

0x37 Follow with Bigsam, Colin and 3 others

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2025-08-yield-basis-judging/issues/207

Found by

0x37, Bigsam, Colin, KupiaSec, blockace, lanrebayode77

Summary

Without _checkpoint_gauge() in emergency_withdraw when not killed, an attacker could exploit this by calling emergency_withdraw to manipulate reward distributions. Since the function handles non-killed states (updating liquidity values via _calculate_values) but skips the checkpoint—intended to prevent flashloan attacks on rewards — it opens a vulnerability window.

Root Cause

An attacker could borrow funds, interact with the contract to inflate apparent activity, and withdraw via emergency_withdraw without updating the gauge, potentially stealing or unfairly claiming rewards from the gauge controller. This is especially severe if the staker is an active gauge with significant rewards, the security measure present in regular deposit and withdraw functions is skipped, leading to fund loss or reward dilution for legitimate users.

VYPER

@external
@nonreentrant
def deposit(assets: uint256, debt: uint256, min_shares: uint256, receiver: address = msg.sender) -> uint256:
    """
    @notice Method to deposit assets (e.g. like BTC) to receive shares (e.g. like yield-bearing BTC)
    @param assets Amount of assets to deposit
    @param debt Amount of debt for AMM to take (approximately BTC * btc_price)
    @param min_shares Minimal amount of shares to receive (important to calculate to exclude sandwich attacks)
    @param receiver Receiver of the shares who is optional. If not specified - receiver is the sender
    """
    self._mint(receiver, shares)

@audit>>     self._checkpoint_gauge()
   
   log Deposit(sender=msg.sender, owner=receiver, assets=assets, shares=shares)
    self._distribute_borrower_fees(FEE_CLAIM_DISCOUNT)
    return shares

VYPER
@external
@nonreentrant
def withdraw(shares: uint256, min_assets: uint256, receiver: address = msg.sender) -> uint256:
    """
    @notice Method to withdraw assets (e.g. like BTC) by spending shares (e.g. like yield-bearing BTC)
    @param shares Shares to withdraw
    @param min_assets Minimal amount of assets to receive (important to calculate to exclude sandwich attacks)
    @param receiver Receiver of the assets who is optional. If not specified - receiver is the sender
    """

@audit>>    self._checkpoint_gauge()
   
    log Withdraw(sender=msg.sender, receiver=receiver, owner=msg.sender, assets=crypto_received, shares=shares)
    self._distribute_borrower_fees(FEE_CLAIM_DISCOUNT)
    return crypto_received

Emergency withdrawal can be called instead of the normal withdrawal, even when the the system is not killed

VYPER

@external
@nonreentrant
def emergency_withdraw(shares: uint256, receiver: address = msg.sender) -> (uint256, int256):
    """
    @notice Method to repay the debt from the wallet and withdraw what is in the AMM. Does not use heavy math but
            does not necessarily work as single asset withdrawal. Minimal output is not specified: convexity of
            bonding curves ensures that attackers can only lose value, not gain
    @param shares Shares to withdraw
    @param receiver Receiver of the assets who is optional. If not specified - receiver is the sender
    @return (unsigned asset, signed stables). If stables < 0 - we need to bring them
    """
    staker: address = self.staker
    assert staker not in [msg.sender, receiver], "Withdraw to/from staker"

    supply: uint256 = 0
    lv: LiquidityValuesOut = empty(LiquidityValuesOut)
    amm: LevAMM = self.amm
    killed: bool = staticcall amm.is_killed()

    if killed:
        supply = self.totalSupply
    else:
        lv = self._calculate_values(self._price_oracle_w())
        supply = lv.supply_tokens
        self.liquidity.admin = lv.admin
        self.liquidity.total = lv.total
        self.liquidity.staked = lv.staked
        self.totalSupply = supply
        if staker != empty(address):
            self.balanceOf[staker] = lv.staked_tokens
            self._log_token_reduction(staker, lv.token_reduction)

    assert supply >= MIN_SHARE_REMAINDER + shares or supply == shares, "Remainder too small"  ## bug attacker creates problem if permissionless

    frac: uint256 = 10**18 * shares // supply
    frac_clean: int256 = convert(frac, int256)
    if lv.admin > 0 and lv.total != 0:
        frac = frac * lv.total // (convert(lv.admin, uint256) + lv.total)

    withdrawn_levamm: Pair = extcall amm._withdraw(frac)
    assert extcall CRYPTOPOOL.transferFrom(amm.address, self, withdrawn_levamm.collateral, default_return_value=True)
    withdrawn_cswap: uint256[2] = extcall CRYPTOPOOL.remove_liquidity(withdrawn_levamm.collateral, [0, 0])
    stables_to_return: int256 = convert(withdrawn_cswap[0], int256) - convert(withdrawn_levamm.debt, int256)

    self._burn(msg.sender, shares)

    self.liquidity.total = self.liquidity.total * (supply - shares) // supply
    if self.liquidity.admin < 0 or killed:
        self.liquidity.admin = self.liquidity.admin * (10**18 - frac_clean) // 10**18             ## we reduce admin's liquidity by fraction. 

    if stables_to_return > 0:
        assert extcall STABLECOIN.transfer(receiver, convert(stables_to_return, uint256), default_return_value=True)
    elif stables_to_return < 0:
        assert extcall STABLECOIN.transferFrom(msg.sender, self, convert(-stables_to_return, uint256), default_return_value=True)
    assert extcall STABLECOIN.transfer(amm.address, withdrawn_levamm.debt, default_return_value=True)
    assert extcall ASSET_TOKEN.transfer(receiver, withdrawn_cswap[1], default_return_value=True)

    return (withdrawn_cswap[1], stables_to_return)
VYPER
@internal
def _checkpoint_gauge():
    """

@audit>>>    @notice Checkpoint a gauge if any to prevent flash loan attacks on reward distribution, reverts are ignored
    """
    gauge: address = self.staker
    if gauge != empty(address):
        success: bool = False
        res: Bytes[32] = empty(Bytes[32])
        success, res = raw_call(
            self.admin,
            method_id("gauge_controller()"),
            max_outsize=32,
            is_static_call=True,
            revert_on_failure=False)
        if success:
            gc: address = convert(res, address)
            if gc != empty(address):
                success = raw_call(
                    gc,
                    abi_encode(gauge, method_id=method_id("checkpoint(address)")),
                    is_static_call=False,
                    revert_on_failure=False)

Internal Pre-conditions

https://github.com/sherlock-audit/2025-08-yield-basis/blob/main/yb-core/contracts/LT.vy#L225-L248

External Pre-conditions

....

Attack Path

No checkpoint

Impact

Removed protection as Checkpointing a gauge, if any, prevents flash loan attacks on reward distribution

PoC

No response

Mitigation

Add the checkpoint call to the emergency withdrawal path, especially when the system is not killed.

Discussion

sherlock-admin2

The protocol team fixed this issue in the following PRs/commits: https://github.com/yield-basis/yb-core/commit/4b15e6293d1b3c9469c533c18a1049975d512736

The original report as published on Sherlock, by 0x37. 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

The emergency_withdraw function omits the _checkpoint_gauge() call that is present in the normal deposit and withdraw functions, so the gauge is not updated before rewards are distributed.

Impact

The missing checkpoint removes protection against flash loan attacks on reward distribution, potentially causing fund loss or reward dilution for legitimate users.

What the attacker needed

Staker is an active gauge with rewards; attacker can borrow funds; emergency_withdraw callable when not killed

The fix

The protocol team fixed this issue by adding the checkpoint call to the emergency withdrawal path.

How it was found

  1. Borrow funds to inflate apparent activity.
  2. Interact with the contract to increase staked balance.
  3. Call emergency_withdraw to withdraw without updating the gauge.
  4. Claim rewards unfairly from the gauge controller.

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.

A gauge checkpoint records how much each user has staked so rewards are shared fairly. The emergency withdrawal function forgot to do this recording, so someone could quickly borrow money, stake it, and withdraw without the system noticing, grabbing more rewards than they should.

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 0x37

View all

79 more disclosed reports, best first.

More from Yield Basis

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.