Threat: Unauthorized State Modification
- Threat: Unauthorized State Change
- Description: An attacker exploits a logic flaw or missing access control check within the Sway code to modify the contract's state in an unintended way. This bypasses intended restrictions implemented in Sway. The vulnerability is specifically in how the Sway contract handles state and permissions.
- Impact:
- Funds stolen or misdirected.
- Ownership or permissions changed without authorization.
- Contract data corrupted, potentially leading to a complete loss of functionality.
- Sway Component Affected:
- Functions that modify
storage
variables. - Missing or incorrect
require()
statements that should enforce access control before state changes. - Incorrect use of
msg_sender()
for authorization.
- Functions that modify
- Risk Severity: Critical
- Mitigation Strategies:
- Strict Access Control (Sway-Specific): Use
msg_sender()
correctly within Sway functions to identify the caller and enforce authorization rules. Userequire()
statements before any state modification to check permissions based onmsg_sender()
or other role-based logic defined in Sway. - Input Validation (Sway-Specific): Thoroughly validate all inputs to functions that modify state, using Sway's type system and
require()
statements to enforce constraints. This includes checking data types, ranges, and lengths within the Sway code. - Immutability (Sway Design): Design the Sway contract to make as much of the state immutable as possible, reducing the attack surface. This is a fundamental design principle within the Sway contract itself.
- Strict Access Control (Sway-Specific): Use
Threat: Reentrancy Attack
- Threat: Reentrancy Exploitation
- Description: An attacker uses a malicious contract to recursively call back into the vulnerable Sway contract before the initial Sway function invocation completes its state updates. This exploits the order of operations within the Sway contract's execution.
- Impact:
- Funds stolen from the contract.
- Unauthorized modification of contract state, leading to inconsistent or corrupted data.
- Contract becoming unusable due to corrupted state.
- Sway Component Affected:
- Sway functions that make external calls to other contracts (especially untrusted contracts).
- Sway state variables (
storage
) that are modified after external calls within a Sway function.
- Risk Severity: High
- Mitigation Strategies:
- Checks-Effects-Interactions Pattern (Sway Implementation): Strictly follow this pattern within the Sway code:
- Checks: Perform all necessary checks (input validation, authorization using
msg_sender()
andrequire()
) in Sway. - Effects: Update the Sway contract's state (
storage
variables). - Interactions: Make external calls.
- Checks: Perform all necessary checks (input validation, authorization using
- Reentrancy Guard (Mutex - Sway Implementation): Implement a reentrancy guard (a boolean flag in
storage
) within the Sway contract to prevent recursive calls. Set the flag totrue
before making an external call and tofalse
after the call returns. Check the flag at the beginning of the Sway function usingrequire()
.
- Checks-Effects-Interactions Pattern (Sway Implementation): Strictly follow this pattern within the Sway code:
Threat: Logic Errors (Sway-Specific)
- Threat: Exploitation of Logic Flaws within Sway Code
- Description: The Sway contract's code contains logical errors that allow an attacker to achieve unintended outcomes, even if there are no specific vulnerabilities like reentrancy. This is a broad category encompassing any flaw in the Sway contract's intended behavior as expressed in Sway.
- Impact:
- Wide range of impacts, depending on the specific logic error within the Sway code. Could include funds theft, unauthorized access, data corruption, or complete contract malfunction.
- Sway Component Affected:
- Any part of the Sway contract's code, including functions, control flow statements (if/else, loops), and data structure manipulations written in Sway.
- Risk Severity: High to Critical (depending on the impact)
- Mitigation Strategies:
- Thorough Code Review (Sway Focus): Multiple developers should review the Sway code, specifically looking for logical errors and edge cases in the Sway implementation.
- Extensive Testing (Sway-Specific): Write comprehensive unit tests, integration tests, and property-based tests targeting the Sway code to cover all possible execution paths and scenarios.
- Formal Verification (Sway): Consider using formal verification tools (if/when available for Sway) to mathematically prove the correctness of critical Sway logic.
- Simple Design (Sway): Keep the Sway contract's logic as simple and straightforward as possible. Avoid unnecessary complexity in the Sway code.
- Audits (Sway Expertise): Engage professional security auditors with expertise in Sway to review the code.
- Threat: Vulnerable Sway Dependency
- Description: The Sway contract relies on an external Sway library or contract (declared as a
dep
inForc.toml
) that contains a vulnerability. The vulnerability is within the Sway code of the dependency. - Impact:
- The vulnerability in the Sway dependency can be exploited to compromise the main Sway contract.
- Sway Component Affected:
dep
declarations inForc.toml
(specifying Sway dependencies).- Any Sway code that uses functions from the external Sway library.
- Risk Severity: High
- Mitigation Strategies:
- Use Trusted Dependencies (Sway Ecosystem): Only use Sway dependencies from trusted sources within the Sway/Fuel ecosystem.
- Audit Dependencies (Sway Code): Review the Sway code of all dependencies for potential vulnerabilities.
- Pin Dependency Versions (Forc.toml): Specify exact versions of Sway dependencies in
Forc.toml
to prevent unexpected updates that might introduce vulnerabilities. - Monitor for Vulnerabilities (Sway Community): Stay informed about security vulnerabilities in your Sway dependencies through the Sway community and any vulnerability reporting channels.
- Fork and Maintain (Sway): If a critical Sway dependency is unmaintained or has known vulnerabilities, consider forking the Sway code and maintaining your own secure version.