Skip to content
Business logic Audit finding Intermediate

Unhandled smaller base decimals than 6 or bigger than the collateral's decimals

The market contract calculates decimal scaling factors using subtraction and division that can underflow or divide by zero when the base token has more decimals than the collateral, or fewer than six decimals. This causes view functions to revert or the market to become unusable, requiring redeployment.

SimaoAmaro Follow

Originally disclosed on Immunefi

  • Disclosed after under an hour

#35761 [SC-Low] Unhandled smaller base decimals than 6 or bigger than the collateral's decimals

Submitted on Oct 6th 2024 at 23:28:34 UTC by @SimaoAmaro for IOP | Swaylend

Description

Brief/Intro

The `market` converts between decimals when going from collateral to base or vice-versa (and price).

There are instances when depending on the decimal values of the collateral, base and price, it could lead to the `market` reverting to underflow or division by 0.

Vulnerability Details

`market::collateral_value_to_sell()` calculates the `scale` to go from collateral decimals to base by doing ```rust let scale = u256::from(10_u64).pow( collateral_configuration .decimals - storage .market_configuration .read() .base_token_decimals, ); ``` If the base token decimals are bigger than the collateral's decimals, this reverts.

In `market::available_to_borrow()`, the scale is calculated as ```rust let scale = u256::from(10_u64).pow( collateral_configuration .decimals + price_exponent - storage .market_configuration .read() .base_token_decimals, ); ``` which also underflows whenever the base token decimals is bigger than the collateral's + the price exponent.

Lastly, `market::update_base_principal()` gets the `accrual_descale_factor` by doing `u256::from(10_u64).pow(market_configuration.base_token_decimals) / BASE_ACCRUAL_SCALE;`, which is 0 whenever the base token decimals is smaller than 6. This will make it revert and the market becomes unusable (funds can not be supplied so no funds are stuck but it needs to be redeployed).

Impact Details

View functions (`market::collateral_value_to_sell()` and `market::available_to_borrow()` will not work or the contract has to be redeployed due to this issue.

References

https://github.com/Swaylend/swaylend-monorepo/blob/develop/contracts/market/src/main.sw?utm\\_source=immunefi#L716 https://github.com/Swaylend/swaylend-monorepo/blob/develop/contracts/market/src/main.sw?utm\\_source=immunefi#L568 https://github.com/Swaylend/swaylend-monorepo/blob/develop/contracts/market/src/main.sw?utm\\_source=immunefi#L1489-L1499

Proof of Concept

Proof of Concept

Change the decimals of usdc to 5 in tokens.json and run `cargo test --release main_test_no_debug -- --nocapture`. The rest fails on step 0 when supplying base due to division by 0.

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

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 contract assumes base token decimals are always between 6 and the collateral's decimals, so subtraction and division in scaling calculations are not guarded against underflow or zero.

Impact

View functions may not work or the contract may need to be redeployed, but no funds are lost.

What the attacker needed

Base token decimals < 6 or > collateral decimals

How it was found

  1. Change the decimals of usdc to 5 in tokens.json
  2. Run the test command cargo test --release main_test_no_debug -- --nocapture
  3. Observe that the test fails on step 0 when supplying base due to division by 0

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.

Smart contracts often need to convert between tokens with different numbers of decimal places. This report shows that some conversion formulas break when the token decimals are set in certain ways, causing the contract to stop working. The issue is about arithmetic mistakes, not an attacker stealing funds.

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 SimaoAmaro

View all

3 more disclosed reports, best first.

More from Swaylend

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.