12 — Arithmetic Rounding Exploit
Severity: 7 | Real: Multiple DeFi rounding exploits, share price manipulation
Division-before-multiplication loses precision — attacker extracts dust per share across millions of operations.
Verification Procedure
Step 1: Find all division operations
grep -rn --include="*.rs" "checked_div\|/ " programs/*/src/instructions/- Record: Every division with context
Step 2: Verify multiplication-before-division order
# For each share/price/fee calculation, verify: (a * b) / c, NOT a * (b / c)
# Read the actual code for each financial formula- ✅ PASS: Every formula multiplies first, divides last
- ❌ FAIL: Any formula where division happens before final multiplication (precision loss)
Step 3: Check for rounding direction consistency
# Deposits: round DOWN (fewer shares — favors vault)
# Withdrawals: round DOWN (less assets — favors vault)
# Fee calculations: round UP (more fees — favors protocol)- ✅ PASS: All rounding consistently favors the protocol/vault
- ❌ FAIL: Any calculation where rounding favors the user (extractable value)
Step 4: Check for u128 intermediate calculations
grep -rn --include="*.rs" "as u128\|u128::from" programs/*/src/instructions/- ✅ PASS: Multiplications that could overflow u64 use u128 intermediates
- ❌ FAIL: Two u64 values multiplied without widening first
Step 5: Round-to-nearest helpers on value paths
rg -n 'try_round_u(64|128)|\.round\(\)|round_half|RoundingMode::(Nearest|HalfUp|HalfEven)|\+ *HALF_WAD|to_imprecise\(' programs/- ❌ FAIL: nearest/half-up rounding on any user-value conversion (Neodyme SPL token-lending disclosure: a deposit→redeem round trip returned n+1; fixed by replacing
roundwithfloor) - Size it as unit value × round trips per transaction (compute-bound when an attacker program loops CPIs) × transactions (AR-071)
Sources: sannykim/solsec → https://neodyme.io/blog/lending_disclosure [paraphrased, cited]; SlowMist best practices README.md "Loss of precision" [https://github.com/slowmist/solana-smart-contract-security-best-practices] (Apache-2.0, attributed to SlowMist).
Overall verdict:
- ✅: Mul-before-div, consistent rounding direction favoring vault, u128 intermediates
- ⚠️: Correct order but missing u128 on some calculations
- ❌: Division before multiplication in financial formulas