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
//! Taint event emission and conversion to [`crate::taint::Finding`].
//!
//! Extracted from the monolithic `ssa_transfer.rs`.  Contains:
//! * [`SsaTaintEvent`] — the raw event struct produced by the block-level
//!   worklist each time a tainted value reaches a sink.
//! * [`ssa_events_to_findings`] — event → `Finding` conversion with the
//!   `primary_location` invariant and dedup.
//! * Flow-path reconstruction helpers ([`reconstruct_flow_path`] and
//!   operand pickers).
//! * Small post-hoc utilities ([`block_distance`],
//!   [`extract_sink_arg_positions`], [`compute_path_hash`]).

use crate::cfg::Cfg;
use crate::labels::Cap;
use crate::ssa::ir::{SsaBody, SsaOp, SsaValue};
use crate::summary::SinkSite;
use crate::taint::domain::TaintOrigin;
use crate::taint::path_state::PredicateKind;
use petgraph::graph::NodeIndex;
use smallvec::SmallVec;
use std::collections::{HashSet, VecDeque};

/// Event emitted when taint reaches a sink in SSA analysis.
#[derive(Clone, Debug)]
pub struct SsaTaintEvent {
    pub sink_node: NodeIndex,
    pub tainted_values: Vec<(SsaValue, Cap, SmallVec<[TaintOrigin; 2]>)>,
    pub sink_caps: Cap,
    pub all_validated: bool,
    pub guard_kind: Option<PredicateKind>,
    /// Whether any callee in this event's taint path was resolved via a
    /// function summary (SSA, local, or global) rather than direct label.
    pub uses_summary: bool,
    /// Primary (callee-internal) sink location for cross-file attribution.
    ///
    /// Populated when this event was emitted via summary resolution and the
    /// callee summary carried a [`SinkSite`] whose `cap` intersects
    /// `sink_caps`.  When multiple [`SinkSite`]s for the same `(param_idx,
    /// cap mask)` match, the emission site produces one event per
    /// [`SinkSite`] so each downstream [`crate::taint::Finding`] carries a
    /// single primary attribution — the multi-primary case collapses to
    /// multiple single-primary events.
    ///
    /// `None` for:
    /// * intra-procedural sinks (`uses_summary == false`), where the
    ///   caller's sink span already names the dangerous instruction;
    /// * summary-resolved sinks whose callee summary carried only cap-only
    ///   [`SinkSite`]s (no source coordinates — e.g. pass-2 transient
    ///   summaries or local `LocalFuncSummary`-only callees).
    pub primary_sink_site: Option<SinkSite>,
}

pub(super) fn block_distance(ssa: &SsaBody, source_node: NodeIndex, sink_node: NodeIndex) -> u16 {
    let src_block = match ssa.cfg_node_map.get(&source_node) {
        Some(v) => ssa.def_of(*v).block,
        None => return 0,
    };
    let sink_block = match ssa.cfg_node_map.get(&sink_node) {
        Some(v) => ssa.def_of(*v).block,
        None => return 0,
    };
    if src_block == sink_block {
        return 0;
    }

    // BFS from src_block to sink_block
    let mut visited = HashSet::new();
    let mut queue = VecDeque::new();
    visited.insert(src_block);
    queue.push_back((src_block, 0u16));

    while let Some((blk, dist)) = queue.pop_front() {
        for &succ in &ssa.block(blk).succs {
            if succ == sink_block {
                return (dist + 1).min(255);
            }
            if visited.insert(succ) && dist + 1 < 255 {
                queue.push_back((succ, dist + 1));
            }
        }
    }
    0 // unreachable or not connected — conservative default
}

// ── Flow Path Reconstruction ─────────────────────────────────────────────

/// Reconstruct the taint flow path from source to sink by walking backward
/// through the SSA def-use chain.
///
/// Returns steps in source→sink order.
pub(super) fn reconstruct_flow_path(
    tainted_val: SsaValue,
    origin: &crate::taint::domain::TaintOrigin,
    sink_node: NodeIndex,
    ssa: &SsaBody,
    cfg: &Cfg,
) -> Vec<crate::taint::FlowStepRaw> {
    use crate::evidence::FlowStepKind;
    use crate::taint::FlowStepRaw;

    const MAX_STEPS: usize = 64;

    let mut steps = Vec::new();
    let mut visited = HashSet::new();

    // 1. Add sink step
    steps.push(FlowStepRaw {
        cfg_node: sink_node,
        var_name: cfg
            .node_weight(sink_node)
            .and_then(|n| n.call.callee.clone()),
        op_kind: FlowStepKind::Sink,
    });

    // 2. Walk backward from tainted_val
    let mut current = tainted_val;
    for _ in 0..MAX_STEPS {
        if !visited.insert(current) {
            break;
        }

        let def = ssa.def_of(current);
        let block = ssa.block(def.block);

        // Find the instruction for this value
        let inst = block
            .phis
            .iter()
            .chain(block.body.iter())
            .find(|i| i.value == current);

        let inst = match inst {
            Some(i) => i,
            None => break,
        };

        // Skip if same cfg_node as previous step (dedup consecutive same-line)
        if let Some(prev) = steps.last() {
            if prev.cfg_node == inst.cfg_node {
                // Still follow the chain, just don't add a duplicate step
                match &inst.op {
                    SsaOp::Source | SsaOp::Param { .. } | SsaOp::SelfParam | SsaOp::CatchParam => {
                        break;
                    }
                    SsaOp::Assign(uses) => {
                        current = pick_tainted_operand(uses, origin, ssa);
                        continue;
                    }
                    SsaOp::Call { args, receiver, .. } => {
                        current = pick_tainted_operand_call(args, receiver, origin, ssa);
                        continue;
                    }
                    SsaOp::Phi(operands) => {
                        let vals: SmallVec<[SsaValue; 4]> =
                            operands.iter().map(|(_, v)| *v).collect();
                        current = pick_tainted_operand(&vals, origin, ssa);
                        continue;
                    }
                    _ => break,
                }
            }
        }

        match &inst.op {
            SsaOp::Source | SsaOp::Param { .. } | SsaOp::SelfParam | SsaOp::CatchParam => {
                steps.push(FlowStepRaw {
                    cfg_node: inst.cfg_node,
                    var_name: inst.var_name.clone(),
                    op_kind: FlowStepKind::Source,
                });
                break;
            }
            SsaOp::Assign(uses) => {
                steps.push(FlowStepRaw {
                    cfg_node: inst.cfg_node,
                    var_name: inst.var_name.clone(),
                    op_kind: FlowStepKind::Assignment,
                });
                if uses.is_empty() {
                    break;
                }
                current = pick_tainted_operand(uses, origin, ssa);
            }
            SsaOp::Call { args, receiver, .. } => {
                steps.push(FlowStepRaw {
                    cfg_node: inst.cfg_node,
                    var_name: inst.var_name.clone(),
                    op_kind: FlowStepKind::Call,
                });
                current = pick_tainted_operand_call(args, receiver, origin, ssa);
            }
            SsaOp::Phi(operands) => {
                steps.push(FlowStepRaw {
                    cfg_node: inst.cfg_node,
                    var_name: inst.var_name.clone(),
                    op_kind: FlowStepKind::Phi,
                });
                let vals: SmallVec<[SsaValue; 4]> = operands.iter().map(|(_, v)| *v).collect();
                if vals.is_empty() {
                    break;
                }
                current = pick_tainted_operand(&vals, origin, ssa);
            }
            SsaOp::FieldProj { receiver, .. } => {
                // Treat field projection as a one-step assignment for
                // flow-step reconstruction: taint reaching `obj.f` came
                // from `obj`.  Phase 4 will refine the witness rendering
                // to include the field name in the step.
                steps.push(FlowStepRaw {
                    cfg_node: inst.cfg_node,
                    var_name: inst.var_name.clone(),
                    op_kind: FlowStepKind::Assignment,
                });
                let single: SmallVec<[SsaValue; 4]> = smallvec::smallvec![*receiver];
                current = pick_tainted_operand(&single, origin, ssa);
            }
            SsaOp::Const(_) | SsaOp::Nop | SsaOp::Undef => break,
        }
    }

    // 3. Reverse: was built sink→source, need source→sink
    steps.reverse();
    steps
}

/// Pick the operand whose definition is closest to the origin node (direct match preferred).
fn pick_tainted_operand(
    operands: &[SsaValue],
    origin: &crate::taint::domain::TaintOrigin,
    ssa: &SsaBody,
) -> SsaValue {
    // Prefer operand defined at the origin node
    for &op in operands {
        if ssa.def_of(op).cfg_node == origin.node {
            return op;
        }
    }
    // Fallback: pick first (heuristic)
    operands.first().copied().unwrap_or(SsaValue(0))
}

/// Pick tainted operand for Call instructions (flatten args + receiver).
fn pick_tainted_operand_call(
    args: &[SmallVec<[SsaValue; 2]>],
    receiver: &Option<SsaValue>,
    origin: &crate::taint::domain::TaintOrigin,
    ssa: &SsaBody,
) -> SsaValue {
    let mut all_vals: SmallVec<[SsaValue; 8]> = SmallVec::new();
    for arg in args {
        all_vals.extend_from_slice(arg);
    }
    if let Some(r) = receiver {
        all_vals.push(*r);
    }
    pick_tainted_operand(&all_vals, origin, ssa)
}

/// Convert SSA taint events to the standard Finding struct.
///
/// # Invariants enforced by debug_assert!
///
/// The `primary_location` field carries the primary sink-location
/// attribution.  One invariant must hold across every emitted Finding:
///
/// * A populated `primary_location` implies the attribution came from a
///   [`SinkSite`] with resolved coordinates (`line != 0` AND `file_rel`
///   non-empty).  Cap-only sites are filtered to `None` here; they never
///   reach downstream formatters claiming a `(0, 0)` origin.
///
/// Note: this invariant is intentionally independent of `uses_summary`.
/// The taint-chain flag tracks summary-propagated *taint*, not summary-
/// resolved *sinks* — a local source can reach a cross-file sink, so
/// `primary_location.is_some()` does not imply `uses_summary == true`.
pub fn ssa_events_to_findings(
    events: &[SsaTaintEvent],
    ssa: &SsaBody,
    cfg: &Cfg,
) -> Vec<crate::taint::Finding> {
    type FindingDedupKey = (usize, usize, Option<(String, u32, u32)>);
    let mut findings = Vec::new();
    let mut seen: HashSet<FindingDedupKey> = HashSet::new();

    for event in events {
        // Suppress findings where all tainted variables were validated
        // (passed through an allowlist, type-check, or validation branch).
        if event.all_validated {
            let span = cfg[event.sink_node].ast.span;
            // Cap-agnostic: record the validated sink span so the
            // AST-pattern suppression gate (`TaintSuppressionCtx`) has
            // positive evidence that the SSA engine reached this sink
            // and proved safety.  Without this, validation/dominator/
            // early-return-style safe code is indistinguishable from
            // silent engine failure when the function emitted no
            // findings and contains no labelled Sanitizer.
            crate::taint::ssa_transfer::state::record_all_validated_span(span);

            // Mirror the path-safety pathway: when the SSA engine has
            // already proved every tainted input to a privileged
            // FILE_IO sink passed through validation, publish the sink
            // span so the state-analysis pass suppresses
            // `state-unauthed-access` on the same span.  Trust here
            // matches the trust the engine already extends when
            // dropping the taint flow finding.  Scoped to FILE_IO sinks
            // because that is the only sink class state-unauthed-access
            // currently fires on; broadening would risk stretching
            // validator-name heuristics into unrelated finding classes.
            if event.sink_caps.intersects(Cap::FILE_IO) {
                crate::taint::ssa_transfer::state::record_path_safe_suppressed_span(span);
            }
            continue;
        }

        let primary_location = event.primary_sink_site.as_ref().and_then(|s| {
            // Only promote to a Finding.primary_location when the site has
            // resolved coordinates (cap-only sites at (0, 0) carry no
            // attribution and would just add noise).
            if s.line == 0 {
                None
            } else {
                Some(crate::taint::SinkLocation {
                    file_rel: s.file_rel.clone(),
                    line: s.line,
                    col: s.col,
                    snippet: s.snippet.clone(),
                })
            }
        });

        // Data-integrity invariant: a populated primary_location must at least
        // carry resolved line coordinates.  `file_rel` may legitimately be
        // empty — when the scan root is the caller file itself (single-file
        // scans), every namespace normalizes to `""` and the callee's site
        // inherits that empty path; consumers resolve it against the file
        // under analysis.  Line==0 is the only filter-worthy invariant.
        debug_assert!(
            primary_location.as_ref().is_none_or(|l| l.line != 0),
            "primary_location must carry a resolved line coordinate",
        );

        // Dedup key includes primary location so multi-site events that
        // share a single (source, sink) pair still produce distinct findings
        // — one per resolved callee-internal site.
        let loc_key = primary_location
            .as_ref()
            .map(|l| (l.file_rel.clone(), l.line, l.col));
        for (val, caps, origins) in &event.tainted_values {
            let cap_specificity = (*caps & event.sink_caps).bits().count_ones() as u8;
            for origin in origins {
                if seen.insert((
                    origin.node.index(),
                    event.sink_node.index(),
                    loc_key.clone(),
                )) {
                    let hop_count = block_distance(ssa, origin.node, event.sink_node);
                    let flow_steps = reconstruct_flow_path(*val, origin, event.sink_node, ssa, cfg);
                    let path_hash = compute_path_hash(&flow_steps);
                    findings.push(crate::taint::Finding {
                        body_id: crate::cfg::BodyId(0), // set by caller
                        sink: event.sink_node,
                        source: origin.node,
                        path: vec![origin.node, event.sink_node],
                        source_kind: origin.source_kind,
                        path_validated: event.all_validated,
                        guard_kind: event.guard_kind,
                        hop_count,
                        cap_specificity,
                        uses_summary: event.uses_summary,
                        flow_steps,
                        symbolic: None,
                        source_span: origin.source_span.map(|(start, _)| start),
                        primary_location: primary_location.clone(),
                        engine_notes: smallvec::SmallVec::new(),
                        path_hash,
                        finding_id: String::new(),
                        alternative_finding_ids: smallvec::SmallVec::new(),
                    });
                }
            }
        }
    }

    findings
}

/// Compute a stable hash over the sequence of intermediate CFG nodes
/// that a tainted value traversed from source to sink.  Used as part of
/// the dedup key so two flows that share `(body_id, sink, source)` but
/// cross different intermediate variables are preserved as distinct
/// findings rather than collapsed to one.
///
/// Hashes the `(cfg_node.index(), op_kind-tag, var_name)` tuple per
/// step.  `op_kind` is captured as a small integer tag so changes in
/// enum encoding do not silently alter the hash; `var_name` is included
/// because two flows may touch the same cfg_node via different phi
/// operands (same node, different variable).
fn compute_path_hash(steps: &[crate::taint::FlowStepRaw]) -> u64 {
    use std::collections::hash_map::DefaultHasher;
    use std::hash::{Hash, Hasher};
    let mut hasher = DefaultHasher::new();
    for step in steps {
        step.cfg_node.index().hash(&mut hasher);
        // Encode FlowStepKind as a stable small integer.  Using the
        // discriminant directly would tie us to enum ordering; an
        // explicit tag is more resilient to reordering.
        let kind_tag: u8 = match step.op_kind {
            crate::evidence::FlowStepKind::Source => 0,
            crate::evidence::FlowStepKind::Assignment => 1,
            crate::evidence::FlowStepKind::Call => 2,
            crate::evidence::FlowStepKind::Phi => 3,
            crate::evidence::FlowStepKind::Sink => 4,
        };
        kind_tag.hash(&mut hasher);
        step.var_name.hash(&mut hasher);
    }
    hasher.finish()
}

/// Given an SSA taint event at a sink, find which argument positions of the
/// sink call instruction were tainted.
pub(super) fn extract_sink_arg_positions(event: &SsaTaintEvent, ssa: &SsaBody) -> Vec<usize> {
    let ssa_val = match ssa.cfg_node_map.get(&event.sink_node) {
        Some(v) => *v,
        None => return vec![],
    };

    let def = ssa.def_of(ssa_val);
    let block = &ssa.blocks[def.block.0 as usize];

    let inst = block
        .phis
        .iter()
        .chain(block.body.iter())
        .find(|i| i.value == ssa_val);

    let inst = match inst {
        Some(i) => i,
        None => return vec![],
    };

    if let SsaOp::Call { args, .. } = &inst.op {
        let tainted_vals: HashSet<SsaValue> =
            event.tainted_values.iter().map(|(v, _, _)| *v).collect();

        let mut positions = Vec::new();
        for (i, arg_vals) in args.iter().enumerate() {
            if arg_vals.iter().any(|v| tainted_vals.contains(v)) {
                positions.push(i);
            }
        }
        positions
    } else {
        vec![]
    }
}