2026-02-25 21:16:36 -05:00
|
|
|
use crate::evidence::Confidence;
|
|
|
|
|
use crate::patterns::{Pattern, PatternCategory, PatternTier, Severity};
|
2025-06-17 01:17:48 +02:00
|
|
|
|
2026-02-25 21:16:36 -05:00
|
|
|
/// Rust AST patterns.
|
|
|
|
|
///
|
|
|
|
|
/// Rust taint rules already cover `Command::new`/`arg`/`status`/`output` sinks
|
|
|
|
|
/// and `env::var` / `fs::read_to_string` sources, so we do NOT duplicate those.
|
|
|
|
|
/// Patterns here focus on **unsafe memory**, **panicking APIs**, and structural
|
|
|
|
|
/// code-quality signals specific to Rust.
|
2025-06-17 01:17:48 +02:00
|
|
|
pub const PATTERNS: &[Pattern] = &[
|
2026-02-25 21:16:36 -05:00
|
|
|
// ── Tier A: Memory Safety (unsafe) ─────────────────────────────────
|
2025-06-24 20:27:06 +02:00
|
|
|
Pattern {
|
2026-02-25 21:16:36 -05:00
|
|
|
id: "rs.memory.transmute",
|
|
|
|
|
description: "std::mem::transmute — unchecked type reinterpretation",
|
|
|
|
|
query: r#"(call_expression
|
|
|
|
|
function: (scoped_identifier
|
|
|
|
|
path: (identifier) @p (#eq? @p "mem")
|
|
|
|
|
name: (identifier) @f (#eq? @f "transmute")))
|
|
|
|
|
@vuln"#,
|
2025-06-24 20:27:06 +02:00
|
|
|
severity: Severity::High,
|
2026-02-25 21:16:36 -05:00
|
|
|
tier: PatternTier::A,
|
|
|
|
|
category: PatternCategory::MemorySafety,
|
|
|
|
|
confidence: Confidence::High,
|
2025-06-24 20:27:06 +02:00
|
|
|
},
|
|
|
|
|
Pattern {
|
2026-02-25 21:16:36 -05:00
|
|
|
id: "rs.memory.copy_nonoverlapping",
|
|
|
|
|
description: "ptr::copy_nonoverlapping — raw pointer memcpy",
|
|
|
|
|
query: r#"(call_expression
|
|
|
|
|
function: (scoped_identifier
|
|
|
|
|
path: (identifier) @p (#eq? @p "ptr")
|
|
|
|
|
name: (identifier) @f (#eq? @f "copy_nonoverlapping")))
|
|
|
|
|
@vuln"#,
|
2025-06-24 20:27:06 +02:00
|
|
|
severity: Severity::High,
|
2026-02-25 21:16:36 -05:00
|
|
|
tier: PatternTier::A,
|
|
|
|
|
category: PatternCategory::MemorySafety,
|
|
|
|
|
confidence: Confidence::High,
|
2025-06-24 20:27:06 +02:00
|
|
|
},
|
|
|
|
|
Pattern {
|
2026-02-25 21:16:36 -05:00
|
|
|
id: "rs.memory.get_unchecked",
|
|
|
|
|
description: "get_unchecked / get_unchecked_mut — unchecked indexing",
|
|
|
|
|
query: r#"(call_expression
|
|
|
|
|
function: (field_expression
|
|
|
|
|
field: (field_identifier) @m
|
|
|
|
|
(#match? @m "^get_unchecked(_mut)?$")))
|
|
|
|
|
@vuln"#,
|
2025-06-24 20:27:06 +02:00
|
|
|
severity: Severity::High,
|
2026-02-25 21:16:36 -05:00
|
|
|
tier: PatternTier::A,
|
|
|
|
|
category: PatternCategory::MemorySafety,
|
|
|
|
|
confidence: Confidence::High,
|
2025-06-24 20:27:06 +02:00
|
|
|
},
|
|
|
|
|
Pattern {
|
2026-02-25 21:16:36 -05:00
|
|
|
id: "rs.memory.mem_zeroed",
|
|
|
|
|
description: "std::mem::zeroed — zero-initialised memory may be UB for non-POD types",
|
|
|
|
|
query: r#"(call_expression
|
|
|
|
|
function: (scoped_identifier
|
|
|
|
|
path: (identifier) @p (#eq? @p "mem")
|
|
|
|
|
name: (identifier) @n (#eq? @n "zeroed")))
|
|
|
|
|
@vuln"#,
|
2025-06-24 20:27:06 +02:00
|
|
|
severity: Severity::High,
|
2026-02-25 21:16:36 -05:00
|
|
|
tier: PatternTier::A,
|
|
|
|
|
category: PatternCategory::MemorySafety,
|
|
|
|
|
confidence: Confidence::High,
|
2025-06-24 20:27:06 +02:00
|
|
|
},
|
|
|
|
|
Pattern {
|
2026-02-25 21:16:36 -05:00
|
|
|
id: "rs.memory.ptr_read",
|
|
|
|
|
description: "ptr::read / ptr::read_volatile — raw pointer dereference",
|
|
|
|
|
query: r#"(call_expression
|
|
|
|
|
function: (scoped_identifier
|
|
|
|
|
path: (identifier) @p (#eq? @p "ptr")
|
|
|
|
|
name: (identifier) @n (#match? @n "^read(_volatile)?$")))
|
|
|
|
|
@vuln"#,
|
2025-06-24 20:27:06 +02:00
|
|
|
severity: Severity::High,
|
2026-02-25 21:16:36 -05:00
|
|
|
tier: PatternTier::A,
|
|
|
|
|
category: PatternCategory::MemorySafety,
|
|
|
|
|
confidence: Confidence::High,
|
2025-06-24 20:27:06 +02:00
|
|
|
},
|
2026-02-25 21:16:36 -05:00
|
|
|
// ── Tier A: Code quality / robustness ──────────────────────────────
|
2025-06-24 20:27:06 +02:00
|
|
|
Pattern {
|
2026-02-25 21:16:36 -05:00
|
|
|
id: "rs.quality.unsafe_block",
|
|
|
|
|
description: "unsafe block — manual memory safety obligation",
|
|
|
|
|
query: "(unsafe_block) @vuln",
|
2025-06-24 20:27:06 +02:00
|
|
|
severity: Severity::Medium,
|
2026-02-25 21:16:36 -05:00
|
|
|
tier: PatternTier::A,
|
|
|
|
|
category: PatternCategory::MemorySafety,
|
|
|
|
|
confidence: Confidence::High,
|
2025-06-24 20:27:06 +02:00
|
|
|
},
|
|
|
|
|
Pattern {
|
2026-02-25 21:16:36 -05:00
|
|
|
id: "rs.quality.unsafe_fn",
|
|
|
|
|
description: "unsafe fn declaration",
|
|
|
|
|
query: r#"(function_item
|
|
|
|
|
(function_modifiers) @mods
|
|
|
|
|
(#match? @mods "^unsafe"))
|
|
|
|
|
@vuln"#,
|
2025-06-24 20:27:06 +02:00
|
|
|
severity: Severity::Medium,
|
2026-02-25 21:16:36 -05:00
|
|
|
tier: PatternTier::A,
|
|
|
|
|
category: PatternCategory::MemorySafety,
|
|
|
|
|
confidence: Confidence::High,
|
2025-06-24 20:27:06 +02:00
|
|
|
},
|
|
|
|
|
Pattern {
|
2026-02-25 21:16:36 -05:00
|
|
|
id: "rs.quality.unwrap",
|
|
|
|
|
description: ".unwrap() — panics on None/Err",
|
|
|
|
|
query: r#"(call_expression
|
|
|
|
|
function: (field_expression
|
|
|
|
|
field: (field_identifier) @name (#eq? @name "unwrap")))
|
|
|
|
|
@vuln"#,
|
2025-06-24 20:27:06 +02:00
|
|
|
severity: Severity::Low,
|
2026-02-25 21:16:36 -05:00
|
|
|
tier: PatternTier::A,
|
|
|
|
|
category: PatternCategory::CodeQuality,
|
|
|
|
|
confidence: Confidence::High,
|
2025-06-24 20:27:06 +02:00
|
|
|
},
|
|
|
|
|
Pattern {
|
2026-02-25 21:16:36 -05:00
|
|
|
id: "rs.quality.expect",
|
|
|
|
|
description: ".expect() — panics on None/Err",
|
|
|
|
|
query: r#"(call_expression
|
|
|
|
|
function: (field_expression
|
|
|
|
|
field: (field_identifier) @name (#eq? @name "expect")))
|
|
|
|
|
@vuln"#,
|
2025-06-24 20:27:06 +02:00
|
|
|
severity: Severity::Low,
|
2026-02-25 21:16:36 -05:00
|
|
|
tier: PatternTier::A,
|
|
|
|
|
category: PatternCategory::CodeQuality,
|
|
|
|
|
confidence: Confidence::High,
|
2025-06-24 20:27:06 +02:00
|
|
|
},
|
|
|
|
|
Pattern {
|
2026-02-25 21:16:36 -05:00
|
|
|
id: "rs.quality.panic_macro",
|
|
|
|
|
description: "panic! macro invocation",
|
|
|
|
|
query: r#"(macro_invocation (identifier) @id (#eq? @id "panic")) @vuln"#,
|
|
|
|
|
severity: Severity::Low,
|
|
|
|
|
tier: PatternTier::A,
|
|
|
|
|
category: PatternCategory::CodeQuality,
|
|
|
|
|
confidence: Confidence::High,
|
2025-06-24 20:27:06 +02:00
|
|
|
},
|
|
|
|
|
Pattern {
|
2026-02-25 21:16:36 -05:00
|
|
|
id: "rs.quality.todo",
|
|
|
|
|
description: "todo!() / unimplemented!() placeholder left in code",
|
|
|
|
|
query: r#"(macro_invocation
|
|
|
|
|
(identifier) @id
|
|
|
|
|
(#match? @id "^(todo|unimplemented)$"))
|
|
|
|
|
@vuln"#,
|
|
|
|
|
severity: Severity::Low,
|
|
|
|
|
tier: PatternTier::A,
|
|
|
|
|
category: PatternCategory::CodeQuality,
|
|
|
|
|
confidence: Confidence::High,
|
2025-06-24 20:27:06 +02:00
|
|
|
},
|
2026-02-25 21:16:36 -05:00
|
|
|
// ── Tier A: Narrowing cast ─────────────────────────────────────────
|
2025-06-24 20:27:06 +02:00
|
|
|
Pattern {
|
2026-02-25 21:16:36 -05:00
|
|
|
id: "rs.memory.narrow_cast",
|
|
|
|
|
description: "`as` cast to 8/16-bit integer — possible truncation",
|
|
|
|
|
query: r#"(type_cast_expression
|
|
|
|
|
type: (primitive_type) @to
|
|
|
|
|
(#match? @to "^(u8|i8|u16|i16)$"))
|
|
|
|
|
@vuln"#,
|
|
|
|
|
severity: Severity::Low,
|
|
|
|
|
tier: PatternTier::A,
|
|
|
|
|
category: PatternCategory::MemorySafety,
|
|
|
|
|
confidence: Confidence::Medium,
|
2025-06-24 20:27:06 +02:00
|
|
|
},
|
|
|
|
|
Pattern {
|
2026-02-25 21:16:36 -05:00
|
|
|
id: "rs.memory.mem_forget",
|
|
|
|
|
description: "std::mem::forget — may leak resources",
|
|
|
|
|
query: r#"(call_expression
|
|
|
|
|
function: (scoped_identifier
|
|
|
|
|
path: (identifier) @p (#eq? @p "mem")
|
|
|
|
|
name: (identifier) @n (#eq? @n "forget")))
|
|
|
|
|
@vuln"#,
|
2025-06-24 20:27:06 +02:00
|
|
|
severity: Severity::Low,
|
2026-02-25 21:16:36 -05:00
|
|
|
tier: PatternTier::A,
|
|
|
|
|
category: PatternCategory::MemorySafety,
|
|
|
|
|
confidence: Confidence::High,
|
2025-06-24 20:27:06 +02:00
|
|
|
},
|
2025-06-17 01:17:48 +02:00
|
|
|
];
|