The conversion functions (convertTokenAmountToUd60x18, convertUd60x18ToTokenAmount, etc.) in the Collateral library assume that self.decimals is always ≤ SYSTEM_DECIMALS. However, this assumption is not explicitly validated during initialization or updates. If self.decimals exceeds SYSTEM_DECIMALS, it could lead to incorrect calculations, resulting in financial losses or system malfunctions.
The following conversion functions are defined in the Collateral library:
convertTokenAmountToUd60x18convertUd60x18ToTokenAmountAssumption on self.decimals : The functions assume that self.decimals is always ≤ SYSTEM_DECIMALS. This assumption is documented but not enforced programmatically.
Potential Overflow/Underflow : If self.decimals exceeds SYSTEM_DECIMALS, the conversion logic may produce incorrect results due to improper scaling.
No Validation : The decimals field is not validated when initializing or updating the Collateral.Data struct.
An attacker or malicious admin sets an invalid value for self.decimals (e.g., self.decimals = 20 while SYSTEM_DECIMALS = 18).
The conversion functions (convertTokenAmountToUd60x18 and convertUd60x18ToTokenAmount) are called with valid input values.
Due to the invalid self.decimals, the functions produce incorrect results:
convertTokenAmountToUd60x18 over-scales the input amount, leading to inflated values.
convertUd60x18ToTokenAmount under-scales the normalized amount, leading to deflated values.
These incorrect conversions propagate through the system, causing financial losses or operational failures.
Financial Losses : Incorrect pricing or accounting could lead to improper debt/reward distributions, affecting users and the protocol.
System Malfunctions : Invalid conversions could disrupt critical operations like collateral valuation, liquidation, and trading.
Trust Erosion : Users may lose confidence in the protocol due to inconsistent or inaccurate calculations.
Manual Code Review : Analyzed the conversion functions and their assumptions about self.decimals.
Slither : Static analysis tool used to identify missing validations and potential overflow/underflow risks.
MythX : Security analysis platform used to verify vulnerabilities in the smart contract.
Add explicit validation for self.decimals during initialization or updates to ensure it adheres to the required constraints. For example:
Centralized Validation : Implement a centralized validation mechanism for all fields in the Collateral.Data struct to prevent invalid configurations.
Event Logging : Emit events whenever self.decimals is updated to provide transparency and enable monitoring.
Unit Tests : Add unit tests to verify the behavior of the conversion functions for edge cases (e.g., self.decimals = 0, self.decimals = SYSTEM_DECIMALS + 1).
Here’s how the convertTokenAmountToUd60x18 function can be updated to include runtime checks:
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.