Two Pyth prices can be used in the same transaction to attack the LP pools
Source: https://github.com/sherlock-audit/2024-02-perpetual-judging/issues/123
The protocol has acknowledged this issue.
Found by
Bauer, IllIllI, PUSH0, jokr
Summary
Pyth oracles use a pull model, where the consumer of the price needs to provide a signed price from an offline provider. There are no guarantees that the price at the current time is the freshest price, which means an attacker can enter an LP position at one base price, and exit in another, all in the same transaction.
Vulnerability Detail
The OracleMaker and SpotHedgeBaseMaker both allow LPs to contribute funds in exchange for getting an LP position. Outside of the requirment that the current price is within the maxAge, there are no other freshness checks. An attacker can create a contract which, given two signed base prices, calls updatePrice() and deposit()s at the lower price, then calls updatePrice() at the higher price, and calls withdraw() at the higher price, for a risk-free profit.
For both the OracleMaker and the SpotHedgeBaseMaker, there are no fees for doing deposit()/withdraw(), and a flash loan can be used to magnify the effect of any price difference between two oracle readings. While both makers support a having a whitelist for who is able to deposit/withdraw, the code doesn't require one, the whitelist isn't mentioned in the contest readme, and the comments in both makers anticipate having to deal with malicious LPs. It appears that the whitelist will be used to ensure first-depositor inflation attacks are mitigated until the pools contain sufficient capital.
The fact that there is a maxAge available does not prevent the issue, because Pyth updates are multiple times a second, whereas a block can only have one timestamp.
Impact
Value accrual that should have gone to the existing LPs is siphoned off by the attacker.
Code Snippet
The number of shares given depends on whatever the most recently stored price is:
// File: src/maker/OracleMaker.sol : OracleMaker.deposit() #1
189 @> uint256 price = _getPrice();
...
201 @> uint256 vaultValueXShareDecimals = _getVaultValueSafe(vault, price).formatDecimals(
202 INTERNAL_DECIMALS,
203 shareDecimals
204 );
205 uint256 amountXShareDecimals = amountXCD.formatDecimals(collateralToken.decimals(), shareDecimals);
206:@> shares = (amountXShareDecimals * totalSupply()) / vaultValueXShareDecimals;
The amount of collateral given back is based on whatever the most recently stored price is:
// File: src/maker/OracleMaker.sol : OracleMaker.withdraw() #2
234 uint256 redeemedRatio = shares.divWad(totalSupply());
...
241 uint256 price = _getPrice();
242 @> uint256 vaultValue = _getVaultValueSafe(vault, price);
243 IERC20Metadata collateralToken = IERC20Metadata(_getAsset());
244 @> uint256 withdrawnAmountXCD = vaultValue.mulWad(redeemedRatio).formatDecimals(
245 INTERNAL_DECIMALS,
246 collateralToken.decimals()
247: );
The SpotHedgeBaseMaker has the same issue.
Tool used
Manual Review
Recommendation
Require that LP deposits and withdrawals be done by the trusted relayers
Discussion
sherlock-admin2
1 comment(s) were left on this issue during the judging contest.
takarez commented:
The readMe says : "Oracle (Pyth) is expected to accurately report the price of market"
IllIllI000
The PR is unrelated to the issue and the issue is not addressed by any other PRs. The PR instead increases the precision of the LP shares-related calculations. The sponsor acknowledges that the submitted issue is not addressed. The sponsor plans to address the issue prior to changing from allowlisted LPs to permissionless ones.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is known
A high-severity security bug in Perpetual. Sherlock made it public on 5 April 2024. Found in the 219 audit contest (finding H-1), also found by 3 other wardens.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.