Mitigation Strategy: Enforce Strict Gas Limits (Sway-Specific)
-
Description:
- Analyze Gas Costs: Use
forc build --gas-estimation
and any available Sway-specific profiling tools to get precise gas cost estimates for each function. Focus on Sway constructs like loops, storage access (reads/writes), and complex data structure manipulations. - Set Realistic Limits (Sway Attribute): Use the
#[payable(gas_limit = X)]
attribute directly in your Sway code. This is a Sway-level enforcement mechanism. ChooseX
carefully, balancing a small safety margin with preventing DoS. - Test with Gas Limits (Sway Testing): Use
forc test
and ensure your test suite includes scenarios that approach the gas limits. This verifies the limits are correctly enforced by the FuelVM. - Iterate and Refine: As the Sway compiler and FuelVM evolve, gas costs will change. Re-run gas estimation and adjust the
gas_limit
attribute in your Sway code periodically.
- Analyze Gas Costs: Use
-
Threats Mitigated:
- Resource Exhaustion DoS (High Severity): Directly prevents Sway code from executing beyond the specified gas limit, stopping DoS attacks that rely on expensive operations.
- Unexpectedly High Transaction Costs (Medium Severity): Provides a hard limit within the Sway code itself, preventing users from accidentally submitting overly expensive transactions.
-
Impact:
- Resource Exhaustion DoS: Very high impact. This is a primary defense against gas-based DoS.
- Unexpectedly High Transaction Costs: High impact, as it sets a contract-enforced limit.
-
Currently Implemented:
#[payable(gas_limit = 500000)]
is used on themint
andtransfer
functions intoken.sw
.
-
Missing Implementation:
#[payable(gas_limit = ...)]
is missing on theapprove
function.- Gas cost analysis and attribute updates are needed after recent Sway code modifications.
Mitigation Strategy: Input Validation and Size Limits (Sway Code)
-
Description:
- Identify Sway Inputs: For every Sway function, identify all input parameters (arrays, strings, structs, etc.).
- Define Constraints (Sway Types & Logic): Use Sway's type system (e.g., fixed-size arrays
[u8; 32]
) andrequire()
statements within the Sway code to define and enforce constraints. Example:require(input_array.len() <= MAX_ARRAY_LENGTH, "Input array too large");
- Sway-Specific Checks: Leverage Sway's features for validation. For example, if you have an
enum
, ensure input values are valid members of that enum. - Test with Invalid Inputs (Sway Tests): Write
forc test
cases that deliberately provide invalid inputs to your Sway functions, verifying therequire()
statements and type checks work as expected.
-
Threats Mitigated:
- Resource Exhaustion DoS (High Severity): Prevents Sway code from processing excessively large inputs that could lead to high gas consumption.
- Logic Errors (Medium Severity): Ensures Sway functions only receive data conforming to expected types and ranges, preventing unexpected behavior.
-
Impact:
- Resource Exhaustion DoS: High impact, as it stops oversized inputs at the Sway code level.
- Logic Errors: Medium-high impact, improving the robustness of the Sway code.
-
Currently Implemented:
require(message.len() <= 256, "Message too long");
insend_message
function.
-
Missing Implementation:
- Missing size limits on arrays passed to
process_data
. - No validation of
user_id
string format.
- Missing size limits on arrays passed to
Mitigation Strategy: Use Checked Arithmetic Operations (Sway Intrinsics)
-
Description:
- Identify Arithmetic (Sway Code): Locate all instances of
+
,-
,*
,/
in your Sway code. - Replace with Sway Intrinsics: Replace every unchecked operator with its Sway-provided checked counterpart:
checked_add
,checked_sub
,checked_mul
,checked_div
. These are intrinsic to Sway. - Handle
Option
(Sway Pattern Matching): The checked functions return anOption<u64>
. Use Sway'smatch
statement orunwrap_or
to handle theNone
case (overflow/underflow), typically reverting the transaction usingrevert(ERROR_CODE);
. - Test Overflow/Underflow (Sway Tests): Write
forc test
cases that specifically cause overflows and underflows, verifying your Sway code handles them correctly.
- Identify Arithmetic (Sway Code): Locate all instances of
-
Threats Mitigated:
- Arithmetic Overflows/Underflows (High Severity): Completely prevents vulnerabilities arising from integer overflows and underflows in Sway code.
-
Impact:
- Arithmetic Overflows/Underflows: Extremely high impact. This is the fundamental mitigation.
-
Currently Implemented:
checked_add
andchecked_mul
are used incalculate_reward
.
-
Missing Implementation:
- Unchecked
-
is used inupdate_balance
. This must be changed tochecked_sub
.
- Unchecked
Mitigation Strategy: Effects-Interaction Pattern (Sway Code Structure)
-
Description:
- Structure Sway Functions: Organize each Sway function in this order:
- Checks (Sway
require
): All input validation, authorization (usingmsg_sender()
), and preconditions, implemented usingrequire()
statements in Sway. - Effects (Sway State Updates): Modify the contract's state (storage variables) only after all checks pass. Use Sway's assignment operators.
- Interactions (Sway
call
): Make external calls to other contracts (using Sway'scall
mechanism) after state updates.
- Checks (Sway
- Minimize Post-Interaction State Changes (Sway Discipline): Strictly avoid modifying Sway's contract state after an external call, unless absolutely unavoidable (and then use extreme caution and consider Sway-compatible mutexes if available).
- Structure Sway Functions: Organize each Sway function in this order:
-
Threats Mitigated:
- Reentrancy-Like Issues (Medium Severity): Reduces the risk, even though Sway prevents direct reentrancy to the same contract. Focuses on safe interaction between Sway contracts.
-
Impact:
- Reentrancy-Like Issues: Medium-high impact, by enforcing a safe calling pattern within the Sway code.
-
Currently Implemented:
transfer
function mostly follows this pattern.
-
Missing Implementation:
claim_rewards
calls an external contract before updating the user's balance in Sway storage. This needs refactoring.
Mitigation Strategy: Avoid Unsafe asm
Blocks (Sway Code Choice)
-
Description:
- Prioritize Sway: Use Sway's built-in functions, standard library, and language features exclusively whenever possible.
- Justification and Documentation (If Unavoidable): If
asm
is absolutely required, provide a very strong justification and extremely detailed documentation within the Sway code itself, explaining why Sway's features are insufficient and outlining theasm
block's precise behavior and risks. - Isolation (Sway Code Organization): Keep
asm
blocks as small and self-contained as possible within the Sway code. - Extensive Sway-Level Testing: Even with
asm
, write as manyforc test
cases as possible to test the surrounding Sway code and the overall contract behavior, including edge cases and potential interactions with theasm
block.
-
Threats Mitigated:
- Memory Safety Violations (High Severity):
asm
bypasses Sway's memory safety. Avoiding it eliminates this risk entirely. - Logic Errors (High Severity): Incorrect
asm
can cause arbitrary, hard-to-debug issues. - Non-Deterministic Behavior (Medium Severity):
asm
might behave differently across FuelVM versions.
- Memory Safety Violations (High Severity):
-
Impact:
- All Threats: Extremely high impact if
asm
is avoided completely.
- All Threats: Extremely high impact if
-
Currently Implemented:
- No
asm
blocks are currently used in the project.
- No
-
Missing Implementation:
- N/A (since
asm
is avoided)
- N/A (since
Mitigation Strategy: Leverage Sway's Type System (Sway Type Definitions)
-
Description:
- Define Custom Sway Types: Use
struct
andenum
extensively to create custom types that precisely model your data. Avoid generic types (likeu64
) where a more specific Sway type (e.g.,Balance
,UserID
) is appropriate. - Sway Type Aliases: Use
type
aliases to give meaningful names to complex Sway types, improving readability. - Enforce Constraints (Sway Compiler): Let the Sway compiler enforce type constraints. For example, if a value should only be one of a few options, use a Sway
enum
. - Review for Shadowing (Sway Code Review): Carefully examine your Sway code for any instances of variable shadowing.
- Define Custom Sway Types: Use
-
Threats Mitigated:
- Type Confusion Errors (Medium Severity): The Sway compiler will prevent many type-related errors.
- Logic Errors (Medium Severity): Clearer Sway types make the code easier to understand and maintain, reducing logic errors.
- Shadowing-Related Bugs (Medium Severity): Prevents unexpected behavior.
-
Impact:
- Type Confusion Errors: High impact, as the Sway compiler enforces type safety.
- Logic Errors: Medium impact, by improving code clarity.
- Shadowing-Related Bugs: Medium impact.
-
Currently Implemented:
struct User
andstruct Asset
are defined.enum TransactionState
is used.
-
Missing Implementation:
- More specific Sway types could be used in some areas (e.g., a
Balance
type instead of justu64
). - A code review focused on Sway-specific shadowing is needed.
- More specific Sway types could be used in some areas (e.g., a