Skip to content
DoS Audit finding Intermediate

`leverageAmount` is incorrect in `SGLLeverage.sellCollateral` function due to calculation based on the new states of YieldBox after withdrawal

The `leverageAmount` in `SGLLeverage.sellCollateral` is calculated after withdrawing shares from YieldBox, so it can be higher than the actual tokens received due to rounding. This causes `leverageExecutor.getAsset()` to revert from insufficient tokens, resulting in a denial of service for `sellCollateral`.

bin2chen Follow with duc

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2024-02-tapioca-judging/issues/61

Found by

bin2chen, duc

Summary

See vulnerability detail

Vulnerability Detail

SGLLeverage.sellCollateral function attempts to remove the user's collateral in shares of YieldBox, then withdraws those collateral shares to collect collateral tokens. Subsequently, the received collateral tokens can be used to swap for asset tokens.

However, the leverageAmount variable in this function does not represent the actual withdrawn tokens from the provided shares because it is calculated after the withdrawal.

yieldBox.withdraw(collateralId, address(this), address(leverageExecutor), 0, calldata_.share);
uint256 leverageAmount = yieldBox.toAmount(collateralId, calldata_.share, false);

amountOut = leverageExecutor.getAsset(
    assetId, address(collateral), address(asset), leverageAmount, calldata_.from, calldata_.data
);

yieldBox.toAmount after withdrawal may return different from the actual withdrawn token amount, because the states of YieldBox has changed. Because the token amount is calculated with rounding down in YieldBox, leverageAmount will be higher than the actual withdrawn amount.

For example, before the withdrawal, YieldBox had 100 total shares and 109 total tokens. Now this function attempt to withdraw 10 shares (calldata_.share = 10) -> the actual withdrawn amount = 10 * 109 / 100 = 10 tokens After that, leverageAmount will be calculated based on the new yieldBox's total shares and total tokens -> leverageAmount = 10 * (109 - 10) / (100 - 10) = 11 tokens

The same vulnerability exists in BBLeverage.sellCollateral function.

Impact

Because leverageAmount can be higher than the actual withdrawn collateral tokens, leverageExecutor.getAsset() will revert due to not having enough tokens in the contract to pull. This results in a DOS of sellCollateral, break this functionality.

Code Snippet

https://github.com/sherlock-audit/2024-02-tapioca/blob/main/Tapioca-bar/contracts/markets/singularity/SGLLeverage.sol#L127-L128

Tool used

Manual Review

Recommendation

leverageAmount should be obtained from the return value of YieldBox.withdraw:

(uint256 leverageAmount, ) = yieldBox.withdraw(collateralId, address(this), address(leverageExecutor), 0, calldata_.share);

Discussion

cryptotechmaker

Fixed by https://github.com/Tapioca-DAO/Tapioca-bar/pull/356

sherlock-admin4

The protocol team fixed this issue in PR/commit https://github.com/Tapioca-DAO/Tapioca-bar/pull/356.

The original report as published on Sherlock, by bin2chen. 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 code calculates the token amount after the withdrawal, using updated YieldBox totals, instead of using the actual withdrawn amount returned by the withdrawal.

Impact

This could cause `sellCollateral` to revert due to insufficient tokens, resulting in a denial of service for that function.

What the attacker needed

User has collateral shares in YieldBox

The fix

The fix is to use the return value of `YieldBox.withdraw` to obtain `leverageAmount` instead of recalculating it afterwards.

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 withdraw shares from a vault, the amount of tokens you get is rounded down. The code here calculates the token amount after the withdrawal, using the vault's new totals, which can give a larger number than what was actually received. That larger number is then used to request tokens from another contract, which fails because there aren't enough tokens, breaking the function.

What is DoS?

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.

More by bin2chen

View all

139 more disclosed reports, best first.

More from Tapioca

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.