fix: u64 overflow in decimal scaling (compute_*_value_to_*) - #12
Open
mehvetero wants to merge 1 commit into
Open
fix: u64 overflow in decimal scaling (compute_*_value_to_*)#12mehvetero wants to merge 1 commit into
mehvetero wants to merge 1 commit into
Conversation
compute_collateral_value_to_buck (bucket.move L704) and compute_buck_value_to_collateral (bucket.move L715, bottle.move L161) multiply a u64 result from mul_factor by pow(10, decimal_diff). mul_factor internally promotes to u128 for safe arithmetic, but the subsequent scaling multiplication is raw u64. When collateral_decimal > buck_decimal (9) and the raw value is large, this overflows and aborts — blocking TCR calculation, health checks, liquidation, and redemption for that collateral type. Fix: cast both operands to u128 before multiplying, then downcast. Same pattern mul_factor already uses internally.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
compute_collateral_value_to_buckandcompute_buck_value_to_collateral(in bothbucket.moveandbottle.move) scale a value bypow(10, decimal_diff)using raw u64 multiplication. Whencollateral_decimal > buck_decimal(9), this can overflow and abort.The upstream
mul_factorcall on the line above already promotes to u128 internally — the scaling multiplication immediately after it undoes that protection.Affected locations
protocol/sources/bucket.movecollateral_raw_value * pow(10, buck_decimal - collateral_decimal)protocol/sources/bucket.movebuck_raw_value * pow(10, collateral_decimal - buck_decimal)protocol/sources/bottle.movebuck_raw_value * pow(10, collateral_decimal - buck_decimal)Impact
These functions are called by
is_in_recovery_mode,is_healthy_bottle,is_liquidatable,handle_redeem, andrecord_repay_capped. An overflow aborts all of them — meaning for a collateral type wheredecimal_diff > 0and amounts are large enough:Currently deployed buckets use SUI (9 decimals = buck_decimal), so
decimal_diff = 0andpow(10, 0) = 1— no overflow possible on existing collateral. The issue surfaces if a futurecreate_bucketcall registers a collateral type with more than 9 decimals.Fix
Cast both operands to u128 before multiplying, then downcast to u64. This matches the pattern
mul_factoralready uses internally (framework/sources/math.move L7-11).Context
Found while running move-test-gen
--lint(MOV-002 rule) against this repo. The rule flags u64 multiplications without u128 promotion. After filtering test modules and known-safe patterns (Coin params, Witness params, etc.), these three sites remained.Reviewed the OtterSec V1 audit (OS-BKT-ADV-00, "Improper Conversion") — that finding addressed missing decimal adjustment in
record_repay_capped, which led tocompute_buck_value_to_collateralbeing introduced as the fix. The overflow in the fixed version is a separate issue.No funds at risk on current deployments (SUI-only buckets). This is a preventive fix for future collateral types.