The function calculateTimeWeightedAverage is vulnerable to timestamp manipulation, which can lead to an underflow in the calculation of duration. This occurs in the following lines of code:
If timestamp is manipulated to be smaller than periods[i].startTime, the calculation of duration will underflow because endTime (which is set to timestamp) will be smaller than periods[i].startTime. This underflow will cause the transaction to revert, potentially disrupting the contract's functionality.
Transaction Reverts: The underflow will cause the transaction to revert, leading to a denial-of-service (DoS) condition.
Timestamp Manipulation: In blockchain environments, timestamps can sometimes be manipulated by miners (within certain limits). If the timestamp parameter is derived from block.timestamp or provided by an untrusted source, an attacker could exploit this to trigger the underflow.
Invalid Input: If the periods array contains invalid data (e.g., startTime > endTime), the function will fail, even without malicious intent.
To mitigate this issue, the following steps should be taken:
Input Validation: Ensure that timestamp is greater than or equal to periods[i].startTime before performing the subtraction.
Check Period Validity: Ensure that periods[i].startTime < periods[i].endTime for all periods.
Handle Edge Cases: Skip periods where timestamp <= periods[i].startTime or startTime >= endTime.
Here’s an example of how to fix the issue:
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.