Skip to content
Audit finding Advanced

WrappedIbbtc and WrappedIbbtcEth contracts do not filter out price feed outliers

The WrappedIbbtc and WrappedIbbtcEth contracts use a price per share value without checking for outliers, so if the price feed is manipulated or malfunctions, users could mint or burn at a distorted price. An attacker could exploit this to gain an economic advantage.

hyh Follow

Originally disclosed on Code4rena

Submitted by hyh

Impact

If price feed is manipulated in any way or there is any malfunction based volatility on the market, both contracts will pass it on a user. In the same time it’s possible to construct mitigation mechanics for such cases, so user economics would be affected by sustainable price movements only. As price outrages provide a substantial attack surface for the project it’s worth adding some complexity to the implementation.

Proof of Concept

In WrappedIbbtcEth pricePerShare variable is updated by externally run updatePricePerShare function (WrappedIbbtcEth.sol L72), and then used in mint/burn/transfer functions without additional checks via balanceToShares function: WrappedIbbtcEth.sol L155

In WrappedIbbtc price is requested via pricePerShare function(WrappedIbbtc.sol L123), and used in the same way without additional checks via balanceToShares function.

Recommended Mitigation Steps

Introduce a minting/burning query that runs on a schedule, separating user funds contribution and actual mint/burn. With user deposit or burn, the corresponding action to be added to commitment query, which execution for mint or redeem will later be sparked by off-chain script according to fixed schedule. This also can be open to public execution with gas compensation incentive, for example as it’s done in Tracer protocol: PoolKeeper.sol L131

Full code of an implementation is too big to include in the report, but viable versions are available publicly (Tracer protocol version can be found at the same repo, implementation/PoolCommitter sol ).

Once the scheduled mint/redeem query is added, the additional logic to control for price outliers will become possible there, as in this case mint/redeem execution can be conditioned to happen on calm market only, where various definitions of calm can be implemented. One of the approaches is to keep track of recent prices and require that new price each time be within a threshold from median of their array.

Example:

// Introduce small price tracking arrays:
uint256\[] private times;
uint256\[] private prices;

// Current position in array
uint8 curPos;

// Current length, grows from 0 to totalMaxPos as prices are being added
uint8 curMaxPos;

// Maximum length, we track up to totalMaxPos prices
uint8 totalMaxPos = 10;

// Price movement threshold
uint256 moveThreshold = 0.1\*1e18;

We omit the full implementation here as it is lengthy enough and can vary. The key steps are:

  • Run query for scheduled mint/redeem with logic: if next price is greater than median of currently recorded prices by threshold, add it to the records, but do not mint/redeem.
  • That is, when scheduled mint/redeem is run, on new price request, WrappedIbbtcEth.core.pricePerShare() or WrappedIbbtc.oracle.pricePerShare(), get newPrice and calculate current price array median, curMed
  • prices[curPos] = newPrice
  • if (curMaxPos < totalMaxPos) {curMaxPos += 1}
  • if (curPos == curMaxPos) {curPos = 0} else {curPos += 1}
  • if (absolutevalueof(newPrice - curMed) < moveThreshold * curMed / 1e18) {do*mint/redeem; return*0_status}
  • else {return_1_status}

Schedule should be frequent enough, say once per 30 minutes, which is kept while returned status is 0. While threshold condition isn’t met and returned status is 1, it runs once per 10 minutes. The parameters here are subject to calibration.

This way if the price movement is sustained the mint/redeem happens after price array median comes to a new equilibrium. If price reverts, the outbreak will not have material effect mint/burn operations. This way the contract vulnerability is considerably reduced as attacker would need to keep distorted price for period long enough, which will happen after the first part of deposit/withdraw cycle. I.e. deposit and mint, burn and redeem operations will happen not simultaneously, preventing flash loans to be used to elevate the quantities, and for price to be effectively distorted it would be needed to keep it so for substantial amount of time.

dapp-whisperer (BadgerDAO) confirmed:

Minting and burning happens atomically within larger function calls and our current approach isn’t amenable to this change.

The original report as published on Code4rena, by hyh. 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

Impact

If the price feed is manipulated or malfunctions, users could mint or burn at an incorrect price, potentially causing economic loss or gain for the attacker; the report does not demonstrate a specific exploit.

What the attacker needed

Price feed manipulation or malfunction; ability to mint or burn

The fix

The contracts should be changed to separate user deposit/burn requests from the actual mint/burn execution, running the latter on a schedule and only when the new price is within a threshold of the median of recent prices.

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.

These contracts let users deposit an asset and receive shares, or burn shares to get the asset back. The number of shares per asset depends on a price that is set from an external source. If that price is briefly wrong, the contract does not notice and users get the wrong amount, which someone could abuse to profit.

More by hyh

View all

137 more disclosed reports, best first.

More from BadgerDAO ibBTC Wrapper contest

View all

The newest disclosed reports for this program.

Similar reports

Same bug class and techniques, clearest first.