SCWE-039: Insecure Use of Inline Assembly
Stable Version v1.0
This content is in the version-(v1.0) and still under active development, so it is subject to change any time (e.g. structure, IDs, content, URLs, etc.).
Relationships¶
- CWE-704: Incorrect Type Conversion or Cast
CWE-704 Link
Description¶
Insecure use of inline assembly refers to vulnerabilities that arise when low-level assembly code is used improperly. This can lead to: - Incorrect type conversions or casts. - Exploitation of vulnerabilities in low-level operations. - Loss of funds or data.
Remediation¶
- Avoid inline assembly: Use high-level Solidity code whenever possible.
- Validate inputs: Ensure all inputs to assembly code are properly validated.
- Test thoroughly: Conduct extensive testing to ensure assembly code is secure.
Examples¶
- Insecure Inline Assembly- Unsafe Type Casting Leads to Exploitable Overflow ⚠️ Why is this Vulnerable?
- Casting a large uint256 into uint8 without bounds checking causes integer truncation.
- If value = 257, it becomes 1 (256 is lost).
-
Attackers can bypass security checks if truncation affects authentication or balance checks.
-
Secure Alternative — High-Level Code with Bounds Checking
Fixes: - Bounds checking (require) prevents unintended truncation.
- Prefer high-level Solidity over assembly when possible; if assembly is required, validate inputs first.