Overflow safety

From PlamenTSV/plamen · View interactive version

Overflow safety is an AI agent skill from the GitHub repository PlamenTSV/plamen. Trigger Pattern Always required for Soroban audits - Inject Into Breadth agents, depth-edge-case

How to use this skill

Skill file

OVERFLOW_SAFETY Skill (Soroban)

Trigger Pattern: Always required for Soroban audits

Inject Into: Breadth agents, depth-edge-case

Finding prefix: [OF-N]

Rules referenced: R10, R14

Soroban contracts are compiled Rust. Rust's overflow behavior depends on the build profile: in debug builds, overflows panic; in release builds (used for deployment), overflows silently wrap by default unless overflow-checks = true is set. Silent wrapping in financial arithmetic is a critical vulnerability.

1. Profile Check

Before tracing any arithmetic, inspect Cargo.toml for the release profile overflow setting:

| File | [profile.release] Present? | overflow-checks Setting | Safe? |

|------|------------------------------|--------------------------|-------|

| Cargo.toml | YES/NO | true / false / MISSING | Only if true |

Interpretation:

Finding threshold: If overflow-checks is not true, the entire overflow safety of the contract depends on manual use of checked/saturating arithmetic. This is a configuration-level finding regardless of whether Section 2 finds specific overflow sites.

2. Arithmetic Trace (if `overflow-checks` is false/missing)

If the profile check from Section 1 found that overflow protection is NOT enabled, trace ALL arithmetic operations in financial paths:

| Location | Expression | Operand Types | Max Realistic Value | Overflow Possible? | Impact if Wrapped |

|----------|-----------|--------------|--------------------|--------------------|------------------|

| {file:line} | {a + b} | u64 / i128 / u32 | {estimate} | YES/NO | {balance wraps to 0, share inflates, etc.} |

Financial paths to prioritize:

Wrapping arithmetic consequences:

3. Checked Arithmetic Patterns

Identify all financial arithmetic and classify whether safe arithmetic methods are used:

| Location | Operation | Method Used | Safe? |

|----------|-----------|-------------|-------|

| {file:line} | {description} | + / checked_add / saturating_add / wrapping_add | Only checked_* or saturating_* |

Safe methods:

Unsafe methods:

Flag any unchecked arithmetic where:

4. i128 Boundary Analysis

Soroban's native token and SEP-41 tokens frequently use i128 for amounts. Check operations near the boundaries:

| Location | Operation | Uses i128? | Near-Boundary Risk | Checked? |

|----------|-----------|-------------|-------------------|---------|

| {file:line} | {expression} | YES/NO | YES/NO | YES/NO |

Specific checks:

5. Division Precision

Soroban has no floating-point arithmetic. All division truncates toward zero (integer division). Incorrect division ordering causes precision loss or incorrect results:

| Location | Expression | Division-Before-Multiplication? | Precision Loss Estimate | Impact |

|----------|-----------|--------------------------------|------------------------|--------|

| {file:line} | {a / b * c} | YES → FLAG | {up to b-1 units lost} | {financial impact} |

Anti-pattern (division before multiplication):

// BAD: (amount / total_supply) loses precision before multiplying by rewards
let user_share = (user_balance / total_supply) * total_rewards;

Correct pattern (multiplication before division):

// GOOD: multiply first to preserve precision
let user_share = (user_balance * total_rewards) / total_supply;

Additional checks:

Finding Template

**ID**: [OF-N]
**Severity**: [Critical if balance wrapping, High if unchecked overflow in financial path, Medium if precision loss, Low if config-only]
**Step Execution**: ✓1,2,3,4,5 | ✗(reasons) | ?(uncertain)
**Rules Applied**: [R10:✓/✗, R14:✓/✗]
**Location**: src/{contract}.rs:LineN (or Cargo.toml for Section 1)
**Title**: {Overflow / Precision loss} in {fn_name} — {impact}
**Description**: [Specific arithmetic expression, operand types, worst-case values, and wrapping consequence]
**Impact**: [Balance wraps to near-zero / share inflation / reward theft / incorrect accounting]

Step Execution Checklist (MANDATORY)

| Section | Required | Completed? | Notes |

|---------|----------|------------|-------|

| 1. Profile Check | YES | ✓/✗/? | Read Cargo.toml [profile.release] |

| 2. Arithmetic Trace | IF overflow-checks not true | ✓/✗(N/A)/? | All financial paths |

| 3. Checked Arithmetic Patterns | YES | ✓/✗/? | All arithmetic in financial paths |

| 4. i128 Boundary Analysis | YES | ✓/✗/? | Wherever i128 used for amounts/shares |

| 5. Division Precision | YES | ✓/✗/? | All division operations in financial paths |

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.