The CreditDelegation.updateVaultLastDistributedValues() function updates the state of a credit delegation by converting values from SD59x18 and UD60x18 types to int128 and uint128. However, this conversion does not include explicit checks to ensure that the values fit within the target type's range. If the values exceed the range of int128 or uint128, it could cause an overflow, leading to incorrect state updates and potential accounting discrepancies.
The updateVaultLastDistributedValues() function is defined as follows:
Type Conversion :
The function converts SD59x18 values (vaultDistributedRealizedDebtUsdPerShareX18 and vaultDistributedUnrealizedDebtUsdPerShareX18) to int128 using .intoInt256().toInt128().
Similarly, UD60x18 values (vaultDistributedUsdcCreditPerShareX18 and vaultDistributedWethRewardPerShareX18) are converted to uint128 using .intoUint128().
Overflow Risk :
SD59x18 and UD60x18 are high-precision fixed-point types with a much larger range than int128 and uint128.
If the input values exceed the range of int128 (-2^127 to 2^127 - 1) or uint128 (0 to 2^128 - 1), the conversion will result in an overflow.
Impact on State :
An overflow during conversion could lead to incorrect state updates, causing discrepancies in debt and reward accounting.
An attacker or malicious market manipulates the inputs to updateVaultLastDistributedValues() such that one or more values exceed the range of int128 or uint128.
For example:
A vaultDistributedRealizedDebtUsdPerShareX18 value greater than 2^127 - 1 causes an overflow when converted to int128.
A vaultDistributedUsdcCreditPerShareX18 value greater than 2^128 - 1 causes an overflow when converted to uint128.
The overflow results in incorrect state updates, leading to improper accounting of debt and rewards.
Users relying on these values may experience financial losses due to incorrect calculations.
Incorrect Accounting : Overflow during type conversion leads to incorrect state updates, affecting debt and reward distributions.
Financial Losses : Users may receive incorrect amounts of debt or rewards, leading to financial losses.
System Integrity : Persistent accounting discrepancies could undermine trust in the system and disrupt its functionality.
Manual Code Review : Analyzed the type conversions in updateVaultLastDistributedValues() and identified potential overflow risks.
Slither : Static analysis tool used to detect unsafe type conversions and potential overflow vulnerabilities.
MythX : Security analysis platform used to verify the impact of overflow risks on the smart contract.
Add explicit checks to ensure that the input values fit within the range of the target types before performing the conversion. For example:
Use SafeMath Libraries : Replace direct type conversions with safe math libraries that include built-in overflow checks.
Input Validation : Validate all inputs to ensure they adhere to expected ranges before performing any operations.
Event Logging : Emit events whenever the function is called to provide transparency and enable monitoring.
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.