Report ID
#34660
Report type
Smart Contract
Has PoC?
Yes
Target
https://etherscan.io/address/0xBA150C2ae0f8450D4B832beeFa3338d4b5982d26
Impacts
Griefing (e.g. no profit motive for an attacker, but damage to the users or the protocol)
Description
The provided test below results indicate a vulnerability in the calcReserveAtRatioSwap function of the ConstantProduct2 contract. The function fails to handle incorrect ratios, specifically a ratio of zero, resulting in unexpected behavior instead of a revert.
Test Case:
Initializes two arrays: reserves and incorrectRatios with specific values. Calls calcReserveAtRatioSwap twice with different parameters. Asserts that the first call's result is greater than zero. Expects the second call to revert but it doesn't.
Failure Analysis:
The test fails because the second call to calcReserveAtRatioSwap with incorrect ratios (specifically, a ratio of 0) does not revert as expected. This suggests that the ConstantProduct2 contract's logic might not be handling incorrect ratios correctly.
Impact:
The failure to handle incorrect ratios can lead to several negative consequences:
Incorrect Calculations: The function might return incorrect reserve values, leading to incorrect pricing and trading decisions.
Exploitability: Malicious actors could potentially manipulate the contract's state by providing invalid inputs, leading to financial losses or other adverse effects. If this behavior is exploited, it could cause disruptions in any protocol relying on the ConstantProduct2 contract for liquidity or price calculations. Specifically:
Liquidity Calculations: Inaccurate liquidity calculations could lead to mispriced trades.
Protocol Integrity: The absence of error handling may allow malicious actors to pass zero ratios, leading to unintended outcomes and potential loss of funds.
Proof of concept
POC:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import "forge-std/Test.sol";
import "../src/functions/ConstantProduct2.sol"; // Adjust the path as necessary
contract ConstantProduct2Test is Test {
ConstantProduct2 constantProduct2;
}
Root Cause Analysis
The calcReserveAtRatioSwap function calculates reserves based on the input ratios. When one of the ratios is zero, the function performs a multiplication that results in a zero value, which it returns instead of reverting. This lack of validation for zero ratios could lead to
unexpected and incorrect behavior in other parts of the contract or dependent systems.