Skip to content
Smart contracts Audit finding Advanced

Overflow risk in Market contract

The Market contract's solvency check multiplies collateral share by large precision constants before converting to amount, which can overflow for realistic 18-decimal collateral values. This causes user actions like borrow or repay to revert, potentially freezing funds.

KIntern_NA Follow

Originally disclosed on Code4rena

Submitted by KIntern_NA

https://github.com/Tapioca-DAO/tapioca-bar-audit/blob/2286f80f928f41c8bc189d0657d74ba83286c668/contracts/markets/Market.sol#L415-L421

https://github.com/Tapioca-DAO/tapioca-bar-audit/blob/2286f80f928f41c8bc189d0657d74ba83286c668/contracts/markets/Market.sol#L390-L396

Actions of users (borrow, repay, removeCollateral, …) in Martket contract might be reverted by overflow, resulting in their funds might be frozen.

Proof of concept

Function _isSolvent in Market contract use conversion from share to amount of yieldBox.

yieldBox.toAmount(
    collateralId,
    collateralShare *
        (EXCHANGE_RATE_PRECISION / FEE_PRECISION) *
        collateralizationRate,
    false
)

It will trigger _toAmount function in YieldBoxRebase contract

function _toAmount(
    uint256 share,
    uint256 totalShares_,
    uint256 totalAmount,
    bool roundUp
) internal pure returns (uint256 amount) {
    totalAmount++;
    totalShares_ += 1e8;

    amount = (share * totalAmount) / totalShares_;

    if (roundUp && (amount * totalShares_) / totalAmount < share) {
        amount++;
    }
}

The calculation amount = (share * totalAmount) / totalShares_ might be overflow because share * totalAmount = collateralShare * (EXCHANGE_RATE_PRECISION / FEE_PRECISION) * collateralizationRate * totalAmount

In the default condition, EXCHANGE_RATE_PRECISION = 1e18, FEE_PRECISION = 1e5, collateralizationRate = 0.75e18

The collateralShare is equal to around 1e8 * collateralAmount by default (because totalAmount++; totalShares_ += 1e8; is present in the _toAmount function).

=> share * totalAmount ~= (collateralAmount * 1e8) * (1e18 / 1e5) * 0.75e18 * totalAmount = collateralAmount * totalAmount * 0.75e39

This formula will overflow when collateralAmount * totalAmount > 1.5e38. This situation can occur easily with 18-decimal collateral. As a consequence, user transactions will revert due to overflow, resulting in the freezing of market functionalities.

The same issue applies to the calculation of _computeMaxBorrowableAmount in the Market contract.

Recommended Mitigation Steps

Reduce some variables used to trigger yieldBox.toAmount(), such as EXCHANGE_RATE_PRECISION and collateralizationRate, and use these variables to calculate with the obtained amount. Example, the expected amount can be calculated as:

yieldBox.toAmount(
    collateralId,
    collateralShare
    false
) * (EXCHANGE_RATE_PRECISION / FEE_PRECISION) * collateralizationRate

0xRektora (Tapioca) confirmed


The original report as published on Code4rena, by KIntern_NA. 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 multiplication of collateral share by precision constants and collateralization rate before calling yieldBox.toAmount causes an integer overflow for large collateral amounts.

Impact

User transactions such as borrow, repay, and removeCollateral could revert, potentially freezing funds and disrupting market functionality.

What the attacker needed

18-decimal collateral; large collateral amount

The fix

The calculation should be rearranged to convert shares to amount first, then apply the precision constants and collateralization rate to the result.

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 smart contract calculates a user's collateral value by multiplying several large numbers together. If the result is too big for the number type, the calculation fails and the transaction is rejected. This can stop users from borrowing or repaying, locking their funds.

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 KIntern_NA

View all

21 more disclosed reports, best first.

More from Tapioca Invitational

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.