Skip to content
Smart contracts Audit finding Advanced

Flash loan price manipulation in purchasePyroFlan()

The purchasePyroFlan() function calculates the flan-stablecoin price from Uniswap pool balances at a single point in time, which can be manipulated with a flash loan. An attacker could skew the pool price to obtain more flan and pyroflan than intended, especially when the pool has low liquidity.

sirhashalot Follow

Originally disclosed on Code4rena

Submitted by sirhashalot

The comment on line 54 of FlanBackstop.sol states “the opportunity for price manipulation through flash loans exists”, and I agree that this is a serious risk. While the acceptableHighestPrice variable attempts to limit the maximum price change of the flan-stablecoin LP, a flashloan sandwich attack can still occur within this limit and make up for the limitation with larger volumes or multiple flashloan attacks. Flashloan price manipulation is the cause for many major hacks, including bZx, Harvest, and others.

Proof of Concept

Line 83 of FlanBackstop.sol calculates the price of flan to stablecoin in the Uniswap pool based on the balances at a single point in time. Pool balances at a single point in time can be manipulated with flash loans, which can skew the numbers to the extreme. The single data point of LP balances is used to calculate the growth variable in line 103, and the growth variable influences the quantity of pyroflan a user receives in the premium calculation on line 108.

uint256 priceBefore = (balanceOfFlanBefore * getMagnitude(stablecoin)) / balanceOfStableBefore;
uint256 growth = ((priceBefore - tiltedPrice) * 100) / priceBefore;
uint256 premium = (flanToMint * (growth / 2)) / 100;

Problems can occur when the volumes that the purchasePyroFlan() function sends to the Uniswap pool are large compared to the pool’s liquidity volume, or if the Uniswap pool price is temporarily tilted with a flashloan (or a whale). Because this function purposefully changes the exchange rate of the LP, by transferring tokens to the LP in a 2-to-1 ratio, a large volume could caught a large price impact in the LP. The code attempts to protect against this manipulation in line 102 with a require statement, but this can be worked around by reducing the volume per flashloan and repeating the attack multiple times. A user can manipulate the LP, especially when the LP is new with low liquidity, in order to achieve large amounts of flan and pyroflan.

Recommended Mitigation Steps

Use a TWAP instead of the pool price at a single point in time to increase the cost of performing a flashloan sandwich attack. See the Uniswap v2 price oracle solutiondocumentation for more explanations on how Uniswap designed an approach to providing asset prices while reducing the change of manipulation.

gititGoro (Behodler) acknowledged and commented:

This is a very well constructed report and if Flan was not intended to target a 1:1 with stablecoins, I’d accept it but since we know Flan shouldn’t diverge far from 1:1, we don’t run very large risks. Essentially, if the flan price crashes dramatically, backstop no longer works so the purpose of this contract is to just boost liquidity for Flan when Flan is operating under normal ish conditions. It’s not intended to be black swan proof.


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

The contract derives the flan price from a single instantaneous Uniswap pool balance rather than a time-weighted average, so the price can be moved within one transaction.

Impact

An attacker could manipulate the pool to receive large amounts of flan and pyroflan, particularly when the pool is new and has low liquidity; the report notes the existing price-change limit can be worked around by reducing volume per flash loan and repeating the attack.

What the attacker needed

Ability to take a flash loan; low-liquidity Uniswap pool helps; multiple smaller attacks may be needed

The fix

The report recommends using a time-weighted average price (TWAP) instead of the single-point pool price to make a flash-loan sandwich attack more costly.

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 flash loan lets someone borrow a huge amount of cryptocurrency for a single transaction, as long as they return it in the same transaction. Here, the contract reads the price from a pool at one moment, so an attacker can use a flash loan to push that price around and get a better deal. The report says the existing price-change limit can be bypassed by doing the attack in smaller pieces or repeatedly.

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 sirhashalot

View all

10 more disclosed reports, best first.

More from Behodler contest

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.