The Standard

The Standard
DeFiHardhat
20,000 USDC
View results
Submission Details
Severity: medium
Invalid

[M] Calculated slippage params via oracle can lead to loss of funds from Sandwich attacks

Summary

Relying on oracle price data when determining the slippage parameter during a swap is problematic,
as chainlink oracles, especially mainnet, have upwards of 2% threshold before triggering a price update.
If swapping between volatile assets, the errors will compound causing even bigger variation.
These variations can be exploited via sandwich attacks.

Vulnerability Details

In the swap function of the SmartVaultV3 contract, a token exchange occurs between two different assets.
A key component of this function is the calculation of minimumAmountOut, which is intended to provide
slippage protection.

function swap(bytes32 _inToken, bytes32 _outToken, uint256 _amount) external onlyOwner {
...
uint256 minimumAmountOut = calculateMinimumAmountOut(_inToken, _outToken, _amount);
...
}

The minimumAmountOut is calculated in the calculateMinimumAmountOut function. This function, in turn,
calls calculator.tokenToEur and calculator.eurToToken, both of which rely on oracle data provided by Chainlink.

function calculateMinimumAmountOut(...) private view returns (uint256) {
...
uint256 collateralValueMinusSwapValue = euroCollateral() - calculator.tokenToEur(getToken(_inTokenSymbol), _amount);
return collateralValueMinusSwapValue >= requiredCollateralValue ?
0 : calculator.eurToToken(getToken(_outTokenSymbol), requiredCollateralValue - collateralValueMinusSwapValue);
}

The tokenToEur and eurToToken functions in the PriceCalculator contract fetch the latest round data from Chainlink
oracles. This data is crucial as it determines the exchange rates used in the slippage calculation.

The Chainlink oracle updates are triggered based on certain thresholds, such as a deviation threshold or a
heartbeat threshold. In highly volatile markets, there can be significant price movements within a short period.
If the oracle data does not update quickly enough to reflect these market changes,
the minimumAmountOut calculated may be based on outdated prices.

This latency in oracle updates can expose the swap function to sandwich attacks. In such attacks, an attacker
can manipulate the market price before and after the swap transaction, taking advantage of the outdated slippage
protection and potentially causing financial losses to the user.

Impact

The consequence of this vulnerability is that the slippage protection provided by minimumAmountOut might
not effectively safeguard against rapid price movements, leading to potential losses for users
executing swaps, especially in volatile market conditions.

Tools Used

Manual Review

Recommendations

Allow users to specify their own slippage value, instead of relying on oracle data. You can check
that their value specified falls within a certain range of the oracle value.

Updates

Lead Judging Commences

hrishibhat Lead Judge over 1 year ago
Submission Judgement Published
Validated
Assigned finding tags:

Slippage-issue

hrishibhat Lead Judge over 1 year ago
Submission Judgement Published
Invalidated
Reason: Known issue
Assigned finding tags:

Slippage-issue

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.