Skip to content
Smart contracts Audit finding Advanced

Unsafe type casting of `poolValue` can malfunction the whole market

A signed pool value could become negative after combining a capped loss with a funding fee, and casting it to an unsigned integer produced an enormous number. That number then overflowed during a division, reverting the rebalance function and stalling the whole market.

WatchPug Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2022-11-float-capital-judging/issues/45

Found by

WATCHPUG

Summary

When poolValue is a negative number due to loss in valueChange and funding, the unsafe type casting from int256 to uint256 will result in a huge number close to 2**255 which will revert _rebalancePoolsAndExecuteBatchedActions() due to overflow when multiplied by 1e18 at L163.

Vulnerability Detail

If the funding rate is 100% per year and the EPOCH_LENGTH is 4 days, the funding fee for each epoch can be as much as ~1% on the effectiveValue.

Plus, the loss from valueChange is capped at 99%, but combining both can still result in a negative poolValue at L146.

At L163 uint256 price = uint256(poolValue).div(tokenSupply); the type casting from int256 to uint256 will result in a huge number close to 2**255.

MathUintFloat.div() will overflow when a number as large as 2**255 is multiplied by 1e18.

Impact

_rebalancePoolsAndExecuteBatchedActions will revert and cause the malfunction of the whole market.

Code Snippet

https://github.com/sherlock-audit/2022-11-float-capital/blob/main/contracts/market/template/MarketCore.sol#L118-L185

Tool used

Manual Review

Recommendation

Consider adding a new function to properly handle the bankruptcy of a specific pool.

Discussion

JasoonS

We seed the pools initially with sufficient un-extractable capital such that this shouldn't be an issue (it should never get close to 0 - even after millions of years and trillions of transactions that may have rounding down and all users withdrawing their funds).

We could create a safe cast function to check - but we made poolValue an int256 so that it is easier to operate on with other signed integers - not because it is ever possible for it to be negative. So it would be redundant in this case.

moose-code

@JasoonS Want to relook at this. @WooSungD @Stentonian maybe you also have thoughts.

I believe watchpug is explaining something different.

They are saying that poolValue can be negative, as a 99% capped loss of poolValue, in conjunction with a 1% funding fee (imagine the side is very overbalanced), will result in the pool value losing more than 100% in total.

A safe guard would be to check that with BOTH funding and value change, 99% is the maximum a pool can lose in any single iteration.

Given system parameterizations, where epoch length will never be that long and funding rate should never be that high, its unlikely this would be an issue in practice, but likely still worth making a change for.

Let me know if anyone has thoughts

JasoonS

Yes, you're right, went through these too fast.

We've discussed this internally a few times. This point should've made it into the readme.

We could add checks to the epoch length on construction to ensure were safe

JasoonS

Nothing to change here.

The epochs would need to be many days long at least for the funding to be close high enough for this to be an issue in the black swan event of a single price change delta being greater than the max change (33% percent).

I believe this to be an informational issue. We could've made it explicit in the readme. But the Video walkthrough implies that the epoch length would be an hour or shorter.

TLDR if the markets are configured correctly, this is a mathematical impossibility. We have plenty of lee-way with current config.

The original report as published on Sherlock, by WATCHPUG. 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 assumed the pool value could never go negative, so it cast a signed integer to an unsigned one without checking, and the later multiplication overflowed.

Impact

The rebalance function would revert, which the report says causes the whole market to malfunction; the maintainers argue this is only reachable with unusual configuration.

What the attacker needed

Pool value turns negative; high funding rate and long epoch; large price change loss

The fix

The report recommends adding a function to handle a pool going bankrupt, and the maintainers discussed capping combined funding and value-change losses or validating epoch length at construction.

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 pool's value is stored as a number that can be negative, but later code treats it as if it can only be positive. When a negative value is converted that way, it turns into an extremely large positive number. Multiplying that huge number overflows and the transaction fails, so the market's regular rebalancing stops working.

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 WatchPug

View all

197 more disclosed reports, best first.

More from Float Capital

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.