Arithmetic Underflow Vulnerability in Statistics Library Leading to Denial of Service
A critical arithmetic underflow vulnerability was identified in the Statistics library. This flaw occurs within the variance function, where subtracting the mean from data points can result in an underflow, causing the transaction to revert. Consequently, functions dependent on variance, such as stddev and LLMOracleCoordinator.sol::finalizeValidation and LLMOracleCoordinator.sol::validate, are perpetually reverted, leading to a Denial of Service (DoS) condition.
The vulnerability resides in the Statistics.sol library, particularly within the variance function. The problematic line is https://github.com/Cyfrin/2024-10-swan-dria/blob/main/contracts/libraries/Statistics.sol?plain=1#L22:
Since both data[i] and mean are unsigned integers (uint256), if data[i] is less than mean, the subtraction results in an underflow. In Solidity versions ^0.8.0, such underflows automatically trigger a revert with panic code 0x11 (Arithmetic operation overflowed outside of an unchecked block).
Due to the perpetual reversion of variance and stddev and finalizeValidation functions, validate function and validation process cannot complete successfully. This halts the entire validation process, effectively rendering the contract non-functional for its intended purpose.
variance: Directly causes underflow during subtraction.
stddev: Relies on variance, thereby inheriting the vulnerability.
LLMOracleCoordinator::validate and LLMOracleCoordinator::finalizeValidation: Utilizes stddev, leading to cascading failures and a Denial of Service (DoS).
The Likelihood of underflow is High and its almost always triggered because there will be at least one data point lower than the mean in a dataset. As a result, the contract encounters frequent reverts in the variance function, and, by extension, in all dependent functions like stddev and finalizeValidation and validate, making the contract highly susceptible to Denial of Service (DoS) through routine usage.
manual review and unit test
To verify the arithmetic underflow vulnerability in the variance function, the following steps can be followed. This involves deploying a wrapper contract around the Statistics library to directly test the affected functions, and then running a Hardhat test script to confirm the expected revert behavior.
Create a Wrapper Contract
Add test
Expected Outcome
When the testVariance function is called with the test data, the transaction should revert with the panic code 0x11, indicating an arithmetic overflow.
Similarly, calling the testStdDev function with its designated test data should also cause a revert due to the inherited vulnerability from variance.
To mitigate the identified arithmetic underflow vulnerability and prevent reverts in the variance and related functions, you can consider implementing check before subtraction:
Modify the variance function to check whether each data point data[i] is greater than or equal to the mean before performing the subtraction.
Example Adjustment:
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.