nyx-scanner 0.5.0

A multi-language static analysis tool for detecting security vulnerabilities
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
//! Symbolic execution targeting: candidate selection and constraint analysis
//! for taint findings.
//!
//! After SSA taint analysis produces findings, this module selects candidates
//! (non-trivial paths, non-validated) and runs constraint analysis on each
//! path to determine feasibility. Results are stored as `SymbolicVerdict` on
//! the finding, which flows through to Evidence and confidence scoring.
//!
//! Symbolic expression trees (`SymbolicValue`) preserve computation structure
//! through the path walk, enabling richer witness strings.

#![allow(
    clippy::collapsible_if,
    clippy::manual_ignore_case_cmp,
    clippy::needless_borrow
)]

pub mod executor;
pub mod heap;
pub mod interproc;
pub mod loops;
#[cfg(feature = "smt")]
pub mod smt;
pub mod state;
pub mod strings;
pub mod transfer;
pub mod value;
pub mod witness;

pub use state::{PathConstraint, SymbolicState};
pub use value::{MAX_EXPR_DEPTH, Op, SymbolicValue};

use std::collections::{HashMap, HashSet};

use crate::cfg::Cfg;
use crate::evidence::{SymbolicVerdict, Verdict};
use crate::ssa::const_prop::ConstLattice;
use crate::ssa::heap::PointsToResult;
use crate::ssa::ir::{BlockId, SsaBody, SsaValue};
use crate::ssa::type_facts::TypeFactResult;
use crate::summary::GlobalSummaries;
use crate::symbol::Lang;
use crate::taint::Finding;

/// Context for symbolic execution analysis.
///
/// Bundles all parameters needed by the symex pipeline: SSA body, CFG,
/// optimization results, and optional cross-file summary context for
/// interprocedural symbolic modeling.
pub struct SymexContext<'a> {
    pub ssa: &'a SsaBody,
    pub cfg: &'a Cfg,
    pub const_values: &'a HashMap<SsaValue, ConstLattice>,
    pub type_facts: &'a TypeFactResult,
    /// Cross-file summaries for interprocedural symbolic modeling.
    /// When `Some`, callee calls can be modeled via `SsaFuncSummary`
    /// instead of being treated as opaque `Unknown`.
    pub global_summaries: Option<&'a GlobalSummaries>,
    pub lang: Lang,
    pub namespace: &'a str,
    /// Points-to analysis results for object identity resolution in the
    /// field-sensitive symbolic heap.
    pub points_to: Option<&'a PointsToResult>,
    /// Pre-lowered intra-file function bodies for interprocedural symbolic
    /// execution. Keyed by canonical `FuncKey`.
    pub callee_bodies: Option<
        &'a std::collections::HashMap<
            crate::symbol::FuncKey,
            crate::taint::ssa_transfer::CalleeSsaBody,
        >,
    >,
    /// SCC membership: maps normalized function name → SCC index.
    /// Used by interprocedural symex for mutual recursion detection.
    pub scc_membership: Option<&'a HashMap<String, usize>>,
    /// Cross-file callee bodies for interprocedural symbolic execution.
    /// Provides body resolution via `GlobalSummaries.resolve_callee_body()`.
    pub cross_file_bodies: Option<&'a GlobalSummaries>,
}

/// Maximum candidates to analyse per file (budget bound).
const MAX_CANDIDATES: usize = 50;

/// Maximum blocks on a path before we skip symex (too expensive).
const MAX_PATH_BLOCKS: usize = 100;

/// Runtime feature gate for SMT solving.  Default ON when compiled with the
/// `smt` feature; controlled at runtime by
/// `analysis.engine.symex.smt` in `nyx.conf` (or `--smt / --no-smt`).
#[cfg(feature = "smt")]
pub fn smt_enabled() -> bool {
    crate::utils::analysis_options::current().symex.smt
}

/// SMT solving is not available without the `smt` compile-time feature.
#[cfg(not(feature = "smt"))]
pub fn smt_enabled() -> bool {
    false
}

/// Feature gate: check if cross-file symbolic body execution is enabled.
///
/// Controlled by `analysis.engine.symex.cross_file` in `nyx.conf` (default
/// `true`) or the `--cross-file-symex / --no-cross-file-symex` CLI flag.
/// When disabled: body extraction, persistence, loading, and resolution are
/// all skipped.
pub fn cross_file_symex_enabled() -> bool {
    crate::utils::analysis_options::current().symex.cross_file
}

/// Feature gate: check if symbolic execution targeting is enabled.
///
/// Controlled by `analysis.engine.symex.enabled` in `nyx.conf` (default
/// `true`) or the `--symex / --no-symex` CLI flag.
pub fn is_enabled() -> bool {
    crate::utils::analysis_options::current().symex.enabled
}

/// Run symex analysis on eligible findings, mutating them in place.
///
/// Pre-filters: skips path_validated findings and those with fewer than 2
/// flow steps. Respects the per-file candidate budget.
pub fn annotate_findings(findings: &mut [Finding], ctx: &SymexContext) {
    let mut budget = MAX_CANDIDATES;
    for finding in findings.iter_mut() {
        if budget == 0 {
            break;
        }
        if finding.flow_steps.len() < 2 || finding.path_validated {
            continue;
        }
        finding.symbolic = Some(analyse_finding_path(finding, ctx));
        budget -= 1;
    }
}

/// Extract the ordered sequence of SSA blocks along a finding's flow path.
///
/// Maps `flow_steps` CFG nodes through `ssa.cfg_node_map` to SSA blocks,
/// deduplicating consecutive blocks.
pub(super) fn extract_path_blocks(finding: &Finding, ssa: &SsaBody) -> Vec<BlockId> {
    let mut blocks = Vec::new();
    let mut seen = HashSet::new();
    for step in &finding.flow_steps {
        if let Some(&val) = ssa.cfg_node_map.get(&step.cfg_node) {
            if val.0 < ssa.value_defs.len() as u32 {
                let block = ssa.value_defs[val.0 as usize].block;
                if seen.insert(block) {
                    blocks.push(block);
                }
            }
        }
    }
    blocks
}

/// Run constraint and symbolic analysis on a single finding's taint path.
///
/// Delegates to the multi-path exploration engine which walks the CFG from
/// source to sink, forking at branch points where both successors lie on
/// some source-to-sink path. Produces an aggregate verdict across all
/// explored paths.
fn analyse_finding_path(finding: &Finding, ctx: &SymexContext) -> SymbolicVerdict {
    let path_blocks = extract_path_blocks(finding, ctx.ssa);

    if path_blocks.is_empty() {
        return SymbolicVerdict {
            verdict: Verdict::Inconclusive,
            constraints_checked: 0,
            paths_explored: 0,
            witness: None,
            interproc_call_chains: Vec::new(),
            cutoff_notes: Vec::new(),
        };
    }

    if path_blocks.len() < 2 {
        // Short path (single block, no branches) — skip the multi-path
        // explorer but still run a linear transfer to extract a witness.
        let witness = linear_witness(finding, ctx, &path_blocks);
        return SymbolicVerdict {
            verdict: Verdict::Inconclusive,
            constraints_checked: 0,
            paths_explored: 1,
            witness,
            interproc_call_chains: Vec::new(),
            cutoff_notes: Vec::new(),
        };
    }

    if path_blocks.len() > MAX_PATH_BLOCKS {
        return SymbolicVerdict {
            verdict: Verdict::Inconclusive,
            constraints_checked: 0,
            paths_explored: 0,
            witness: Some("path too long for symex budget".into()),
            interproc_call_chains: Vec::new(),
            cutoff_notes: Vec::new(),
        };
    }

    let result = executor::explore_finding(finding, ctx);
    result.aggregate_verdict()
}

/// Run a minimal linear symbolic transfer on `path_blocks` and extract
/// a witness. Used for short paths (single block, no branches) that
/// don't need the full multi-path exploration engine.
fn linear_witness(
    finding: &Finding,
    ctx: &SymexContext,
    path_blocks: &[BlockId],
) -> Option<String> {
    let mut sym_state = SymbolicState::new();

    // Seed constants from const_prop
    sym_state.seed_from_const_values(&ctx.const_values);

    // Seed source flow steps as tainted symbols before transfer.
    for step in &finding.flow_steps {
        if let Some(&val) = ctx.ssa.cfg_node_map.get(&step.cfg_node) {
            if matches!(step.op_kind, crate::evidence::FlowStepKind::Source) {
                sym_state.set(val, value::SymbolicValue::Symbol(val));
                sym_state.mark_tainted(val);
            }
        }
    }

    // Build context structs for transfer
    let summary_ctx = ctx.global_summaries.map(|gs| transfer::SymexSummaryCtx {
        global_summaries: gs,
        lang: ctx.lang,
        namespace: ctx.namespace,
        type_facts: Some(ctx.type_facts),
    });
    let heap_ctx = ctx.points_to.map(|pts| transfer::SymexHeapCtx {
        points_to: pts,
        ssa: ctx.ssa,
        lang: ctx.lang,
        const_values: ctx.const_values,
    });

    // Transfer each block in order
    for &bid in path_blocks {
        if let Some(block) = ctx.ssa.blocks.get(bid.0 as usize) {
            transfer::transfer_block(
                &mut sym_state,
                block,
                ctx.cfg,
                ctx.ssa,
                summary_ctx.as_ref(),
                heap_ctx.as_ref(),
                None, // no interproc for short paths
                Some(ctx.lang),
            );
        }
    }

    // After transfer, mark all Symbol values that appear in the sink
    // expression as tainted. The transfer builds the expression tree from
    // base SSA values (parameters, etc.); we mark them tainted so that
    // witness extraction can identify tainted sub-expressions.
    if let Some(&sink_val) = ctx.ssa.cfg_node_map.get(&finding.sink) {
        let sink_sym = sym_state.get(sink_val);
        mark_symbols_tainted(&sink_sym, &mut sym_state);
    }

    // Extract witness
    witness::extract_witness(&sym_state, finding, ctx.ssa, ctx.cfg)
        .or_else(|| sym_state.get_sink_witness(finding, ctx.ssa))
}

/// Recursively mark all `Symbol(v)` values in an expression tree as tainted.
fn mark_symbols_tainted(expr: &value::SymbolicValue, state: &mut SymbolicState) {
    match expr {
        value::SymbolicValue::Symbol(v) => {
            state.mark_tainted(*v);
        }
        value::SymbolicValue::BinOp(_, l, r) | value::SymbolicValue::Concat(l, r) => {
            mark_symbols_tainted(l, state);
            mark_symbols_tainted(r, state);
        }
        value::SymbolicValue::Call(_, args) => {
            for a in args {
                mark_symbols_tainted(a, state);
            }
        }
        value::SymbolicValue::Phi(ops) => {
            for (_, v) in ops {
                mark_symbols_tainted(v, state);
            }
        }
        value::SymbolicValue::ToLower(s)
        | value::SymbolicValue::ToUpper(s)
        | value::SymbolicValue::Trim(s)
        | value::SymbolicValue::StrLen(s)
        | value::SymbolicValue::Replace(s, _, _)
        | value::SymbolicValue::Encode(_, s)
        | value::SymbolicValue::Decode(_, s) => {
            mark_symbols_tainted(s, state);
        }
        value::SymbolicValue::Substr(s, start, end) => {
            mark_symbols_tainted(s, state);
            mark_symbols_tainted(start, state);
            if let Some(e) = end {
                mark_symbols_tainted(e, state);
            }
        }
        value::SymbolicValue::Concrete(_)
        | value::SymbolicValue::ConcreteStr(_)
        | value::SymbolicValue::Unknown => {}
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ssa::ir::{BlockId, SsaBlock, SsaBody, SsaValue, Terminator, ValueDef};
    use crate::ssa::type_facts::TypeFactResult;
    use petgraph::graph::NodeIndex;
    use smallvec::smallvec;

    fn empty_type_facts() -> TypeFactResult {
        TypeFactResult {
            facts: HashMap::new(),
        }
    }

    fn make_value_def(block: BlockId, cfg_node: NodeIndex) -> ValueDef {
        ValueDef {
            var_name: None,
            cfg_node,
            block,
        }
    }

    #[test]
    fn is_enabled_tracks_runtime_default() {
        // The process-wide runtime is a `OnceLock`; without any prior install,
        // [`is_enabled`] reflects `AnalysisOptions::default().symex.enabled`.
        // Flipping the toggle is covered by `analysis_options` unit tests that
        // don't cross process boundaries.
        assert_eq!(
            is_enabled(),
            crate::utils::AnalysisOptions::default().symex.enabled
        );
    }

    #[test]
    fn extract_path_blocks_basic() {
        use crate::taint::FlowStepRaw;

        let n0 = NodeIndex::new(0);
        let n1 = NodeIndex::new(1);
        let b0 = BlockId(0);
        let b1 = BlockId(1);

        let ssa = SsaBody {
            blocks: vec![
                SsaBlock {
                    id: b0,
                    phis: vec![],
                    body: vec![],
                    terminator: Terminator::Goto(b1),
                    preds: smallvec![],
                    succs: smallvec![b1],
                },
                SsaBlock {
                    id: b1,
                    phis: vec![],
                    body: vec![],
                    terminator: Terminator::Return(None),
                    preds: smallvec![b0],
                    succs: smallvec![],
                },
            ],
            entry: b0,
            value_defs: vec![make_value_def(b0, n0), make_value_def(b1, n1)],
            cfg_node_map: [(n0, SsaValue(0)), (n1, SsaValue(1))].into_iter().collect(),
            exception_edges: vec![],
            field_interner: crate::ssa::ir::FieldInterner::default(),
            field_writes: std::collections::HashMap::new(),
        };

        let finding = Finding {
            body_id: crate::cfg::BodyId(0),
            sink: n1,
            source: n0,
            path: vec![n0, n1],
            source_kind: crate::labels::SourceKind::UserInput,
            path_validated: false,
            guard_kind: None,
            hop_count: 1,
            cap_specificity: 1,
            uses_summary: false,
            flow_steps: vec![
                FlowStepRaw {
                    cfg_node: n0,
                    var_name: Some("x".into()),
                    op_kind: crate::evidence::FlowStepKind::Source,
                },
                FlowStepRaw {
                    cfg_node: n1,
                    var_name: Some("x".into()),
                    op_kind: crate::evidence::FlowStepKind::Sink,
                },
            ],
            symbolic: None,
            source_span: None,
            primary_location: None,
            engine_notes: smallvec::SmallVec::new(),
            path_hash: 0,
            finding_id: String::new(),
            alternative_finding_ids: smallvec::SmallVec::new(),
        };

        let blocks = extract_path_blocks(&finding, &ssa);
        assert_eq!(blocks, vec![b0, b1]);
    }

    #[test]
    fn analyse_no_branches_confirmed() {
        use crate::taint::FlowStepRaw;

        let n0 = NodeIndex::new(0);
        let n1 = NodeIndex::new(1);
        let b0 = BlockId(0);
        let b1 = BlockId(1);

        let ssa = SsaBody {
            blocks: vec![
                SsaBlock {
                    id: b0,
                    phis: vec![],
                    body: vec![],
                    terminator: Terminator::Goto(b1),
                    preds: smallvec![],
                    succs: smallvec![b1],
                },
                SsaBlock {
                    id: b1,
                    phis: vec![],
                    body: vec![],
                    terminator: Terminator::Return(None),
                    preds: smallvec![b0],
                    succs: smallvec![],
                },
            ],
            entry: b0,
            value_defs: vec![make_value_def(b0, n0), make_value_def(b1, n1)],
            cfg_node_map: [(n0, SsaValue(0)), (n1, SsaValue(1))].into_iter().collect(),
            exception_edges: vec![],
            field_interner: crate::ssa::ir::FieldInterner::default(),
            field_writes: std::collections::HashMap::new(),
        };

        let finding = Finding {
            body_id: crate::cfg::BodyId(0),
            sink: n1,
            source: n0,
            path: vec![n0, n1],
            source_kind: crate::labels::SourceKind::UserInput,
            path_validated: false,
            guard_kind: None,
            hop_count: 1,
            cap_specificity: 1,
            uses_summary: false,
            flow_steps: vec![
                FlowStepRaw {
                    cfg_node: n0,
                    var_name: Some("x".into()),
                    op_kind: crate::evidence::FlowStepKind::Source,
                },
                FlowStepRaw {
                    cfg_node: n1,
                    var_name: Some("x".into()),
                    op_kind: crate::evidence::FlowStepKind::Sink,
                },
            ],
            symbolic: None,
            source_span: None,
            primary_location: None,
            engine_notes: smallvec::SmallVec::new(),
            path_hash: 0,
            finding_id: String::new(),
            alternative_finding_ids: smallvec::SmallVec::new(),
        };

        let ctx = SymexContext {
            ssa: &ssa,
            cfg: &Cfg::new(),
            const_values: &HashMap::new(),
            type_facts: &empty_type_facts(),
            global_summaries: None,
            lang: crate::symbol::Lang::JavaScript,
            namespace: "test.js",
            points_to: None,
            callee_bodies: None,
            scc_membership: None,
            cross_file_bodies: None,
        };
        let verdict = analyse_finding_path(&finding, &ctx);
        assert_eq!(verdict.verdict, Verdict::Confirmed);
        assert_eq!(verdict.constraints_checked, 0);
        assert_eq!(verdict.paths_explored, 1);
    }

    #[test]
    fn annotate_skips_validated() {
        use crate::taint::FlowStepRaw;

        let n0 = NodeIndex::new(0);
        let n1 = NodeIndex::new(1);

        let mut finding = Finding {
            body_id: crate::cfg::BodyId(0),
            sink: n1,
            source: n0,
            path: vec![n0, n1],
            source_kind: crate::labels::SourceKind::UserInput,
            path_validated: true, // should be skipped
            guard_kind: None,
            hop_count: 1,
            cap_specificity: 1,
            uses_summary: false,
            flow_steps: vec![
                FlowStepRaw {
                    cfg_node: n0,
                    var_name: Some("x".into()),
                    op_kind: crate::evidence::FlowStepKind::Source,
                },
                FlowStepRaw {
                    cfg_node: n1,
                    var_name: Some("x".into()),
                    op_kind: crate::evidence::FlowStepKind::Sink,
                },
            ],
            symbolic: None,
            source_span: None,
            primary_location: None,
            engine_notes: smallvec::SmallVec::new(),
            path_hash: 0,
            finding_id: String::new(),
            alternative_finding_ids: smallvec::SmallVec::new(),
        };

        let ssa = SsaBody {
            blocks: vec![],
            entry: BlockId(0),
            value_defs: vec![],
            cfg_node_map: HashMap::new(),
            exception_edges: vec![],
            field_interner: crate::ssa::ir::FieldInterner::default(),
            field_writes: std::collections::HashMap::new(),
        };

        let ctx = SymexContext {
            ssa: &ssa,
            cfg: &Cfg::new(),
            const_values: &HashMap::new(),
            type_facts: &empty_type_facts(),
            global_summaries: None,
            lang: crate::symbol::Lang::JavaScript,
            namespace: "test.js",
            points_to: None,
            callee_bodies: None,
            scc_membership: None,
            cross_file_bodies: None,
        };
        annotate_findings(std::slice::from_mut(&mut finding), &ctx);
        // Should remain None — skipped due to path_validated
        assert!(finding.symbolic.is_none());
    }

    #[test]
    fn annotate_skips_short_path() {
        use crate::taint::FlowStepRaw;

        let n0 = NodeIndex::new(0);

        let mut finding = Finding {
            body_id: crate::cfg::BodyId(0),
            sink: n0,
            source: n0,
            path: vec![n0],
            source_kind: crate::labels::SourceKind::UserInput,
            path_validated: false,
            guard_kind: None,
            hop_count: 0,
            cap_specificity: 1,
            uses_summary: false,
            flow_steps: vec![FlowStepRaw {
                cfg_node: n0,
                var_name: Some("x".into()),
                op_kind: crate::evidence::FlowStepKind::Source,
            }],
            symbolic: None,
            source_span: None,
            primary_location: None,
            engine_notes: smallvec::SmallVec::new(),
            path_hash: 0,
            finding_id: String::new(),
            alternative_finding_ids: smallvec::SmallVec::new(),
        };

        let ssa = SsaBody {
            blocks: vec![],
            entry: BlockId(0),
            value_defs: vec![],
            cfg_node_map: HashMap::new(),
            exception_edges: vec![],
            field_interner: crate::ssa::ir::FieldInterner::default(),
            field_writes: std::collections::HashMap::new(),
        };

        let ctx = SymexContext {
            ssa: &ssa,
            cfg: &Cfg::new(),
            const_values: &HashMap::new(),
            type_facts: &empty_type_facts(),
            global_summaries: None,
            lang: crate::symbol::Lang::JavaScript,
            namespace: "test.js",
            points_to: None,
            callee_bodies: None,
            scc_membership: None,
            cross_file_bodies: None,
        };
        annotate_findings(std::slice::from_mut(&mut finding), &ctx);
        // Should remain None — only 1 flow step
        assert!(finding.symbolic.is_none());
    }
}