Skip to content
Crypto Audit finding Advanced

Attacker can steal LPs funds by using different oracle prices in the same transaction

The Pyth oracle can return different prices within the same transaction, allowing a malicious liquidity provider to withdraw at a higher price and deposit at a lower price, profiting at the expense of traders.

bareli Follow with santipu_ and xiaoming90

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2024-03-flat-money-fix-review-contest-judging/issues/27

The protocol has acknowledged this issue.

Found by

bareli, santipu_, xiaoming90

Summary

In the previous audit, there was an issue that pointed out that the PYTH oracle can return different prices within the same transaction. The fix implemented was to prevent the attack path described there, but a malicious LP can steal funds using a similar attack path.

Vulnerability Detail

The attack path is as follows:

  1. The attacker has some liquidity deposited as an LP with address A.
  2. The attacker announces the withdrawal of all liquidity with address A.
  3. In the same txn, the attacker announces the deposit of liquidity (same amount as the withdrawal) with address B.
  4. After the minimum execution time has elapsed, retrieve two prices from the Pyth oracle where the second price is lower than the first one.
  5. Execute the withdrawal of address A sending the higher price.
  6. Execute the deposit of address B sending the lower price.

As a result, the attacker will have the same deposited liquidity in the protocol, plus a profit from the price deviation.

Even though the withdrawal may reduce the profits of this attack, it doesn't completely prevent it. Moreover, if the attacker is the only LP on the market, there's not going to be a withdrawal fee, increasing the profits of such an attack at the expense of the traders.

The requirements of this attack are the same as the ones described in the original issue, hence the medium severity.

Sidenote: Now that I'm checking better the fixes on the previous audit, I think that the second attack path described by the original issue is still not fixed:

Another possible strategy would pass through the following steps:

  • Create a leverage position.
  • Announce another leverage position with the same size.
  • In the same block, announce a limit close order.
  • After the minimum execution time has elapsed, retrieve two prices from the Pyth oracle where the second price is lower than the first one.
  • Execute the limit close order sending the first price.
  • Execute the open order sending the second price.

The result in this case is having a position with the same size as the original one, but having either lowered the position.lastPrice or getting a profit from the original position, depending on how the price has moved since the original position was opened.

I don't have access to the exact pull request of the fix but I'm pretty sure the attack path described above is still feasible.

Impact

Different oracle prices can be fetched in the same transaction, which can be used to create arbitrage opportunities that can make a profit with no risk at the expense of users on the other side of the trade.

Code Snippet

https://github.com/sherlock-audit/2024-03-flat-money-fix-review-contest/blob/main/flatcoin-v1/src/OracleModule.sol#L63

Tool used

Manual Review

Recommendation

Given the time constraints of this contest, I haven't been able to think about a quick fix for this issue. Even if the protocol team doesn't allow to update the price feeds within the same block, an attacker can still update them by directly calling the PYTH contract.

Discussion

sherlock-admin4

1 comment(s) were left on this issue during the judging contest.

santipu_ commented:

Medium

rashtrakoff

The original scenario is not possible imo because the limit order's execution time is changed when a delayed order adjusts a position. However, I think other variations like the one you described is still possible.

I think to only solution would be to use the same price in a block for all orders. This would still allow for multi-block arbitrage attacks but that will be incredibly difficult to perform and probably will result in losses mostly.

itsermin

I think this scenario is part of a range of oracle frontrunning issues. It doesn't neccesarily require 2 prices in a single block to profit.

In a simpler example issue, the attacker:

  • Announces a leverage open

  • Fetches 2 Pyth prices

  • Executes open on the lower price

  • Announces a close

  • Fetches 2 Pyth prices

  • Executes close on the higher price

Just like the original example, this example is profitable over time if it wasn't for the trading fee that the attacker has to pay on each order. Whether the original example steps 5 and 6 are done in a single block or consecutive blocks is also not critical either.

The trading fee mitigates any of these types of oracle frontrunning issues from being profitable.

The original report as published on Sherlock, by bareli. 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 protocol allows multiple oracle price updates within a single transaction, so the same transaction can use different prices for different operations.

Impact

The attacker could make a risk-free profit at the expense of traders; withdrawal fees may reduce but not eliminate the profit, and if the attacker is the only LP there is no withdrawal fee.

What the attacker needed

Attacker is an LP with liquidity; attacker can announce withdrawal and deposit; minimum execution time has elapsed; attacker can fetch two Pyth prices

The fix

The protocol should use the same oracle price for all orders within a block, though the report notes this would still allow multi-block arbitrage.

How it was found

  1. The attacker deposits liquidity as an LP with address A.
  2. The attacker announces withdrawal of all liquidity with address A.
  3. In the same transaction, the attacker announces deposit of the same amount with address B.
  4. After the minimum execution time, the attacker retrieves two Pyth prices where the second is lower than the first.
  5. The attacker executes the withdrawal for address A using the higher price.
  6. The attacker executes the deposit for address B using the lower price.

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 liquidity provider (LP) puts money into a pool that traders use. The protocol uses an oracle to get the current price of an asset. If the oracle can give two different prices in one transaction, an attacker can use the higher price to take money out and the lower price to put money back in, keeping the difference as profit.

What is Crypto?

Encryption, signatures, randomness or certificate checks that do not protect what they claim to.

More from Flat Money Fix Review Contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Cryptographic weaknesses

The lesson behind this report.

Encryption, signatures, randomness or certificate checks that do not protect what they claim to.