keyhog-scanner 0.5.73

keyhog-scanner: high-performance SIMD-accelerated secret detection engine
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
/// Result from an explicit VYRE GPU scanner self-test.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VyreGpuSelfTest {
    /// Number of direct GPU matches produced by `GpuLiteralSet::scan`.
    pub direct_matches: usize,
    /// Number of matches produced by one coalesced scanner GPU dispatch.
    pub coalesced_matches: usize,
}

/// Force the VYRE GPU scanner and coalesced scanner paths.
///
/// Both counts are populated from real VYRE GPU scans.
pub fn vyre_gpu_self_test() -> Result<VyreGpuSelfTest, String> {
    #[cfg(not(feature = "gpu"))]
    {
        Err(
            "VYRE GPU self-test not available in the lean CI build (no WGPU driver compiled in). \
             Rebuild with `--features gpu`."
                .to_string(),
        )
    }
    #[cfg(feature = "gpu")]
    {
        vyre_gpu_self_test_impl()
    }
}

#[cfg(feature = "gpu")]
fn vyre_gpu_self_test_impl() -> Result<VyreGpuSelfTest, String> {
    use vyre::scan::GpuLiteralSet;
    use vyre_driver_wgpu::WgpuBackend;

    let patterns: Vec<Vec<u8>> = vec![b"needle".to_vec()];
    let pattern_refs: Vec<&[u8]> = patterns.iter().map(Vec::as_slice).collect();

    let backend = WgpuBackend::shared().map_err(|e| format!("failed to init wgpu backend: {e}"))?;
    let scanner = GpuLiteralSet::compile(&pattern_refs);

    let direct = scanner
        .scan(backend.as_ref(), b"needle", 100)
        .map_err(|error| format!("vyre direct GPU scan failed: {error}"))?;
    if direct.len() != 1 || direct[0].pattern_id != 0 || direct[0].start != 0 {
        return Err(format!(
            "vyre direct GPU scan returned unexpected matches: {direct:?}"
        ));
    }

    const COALESCED_ITEMS: usize = 100;
    let items: Vec<Vec<u8>> = (0..COALESCED_ITEMS)
        .map(|index| format!("id-{index:03}-needle").into_bytes())
        .collect();
    let mut buffer = Vec::with_capacity(items.iter().map(Vec::len).sum());
    for item in &items {
        buffer.extend_from_slice(item);
    }

    let coalesced = scanner
        .scan(backend.as_ref(), &buffer, 10_000)
        .map_err(|error| format!("vyre coalesced GPU scan failed: {error}"))?;
    if coalesced.len() != COALESCED_ITEMS {
        return Err(format!(
            "vyre coalesced GPU scan returned {} matches, expected {COALESCED_ITEMS}",
            coalesced.len()
        ));
    }

    Ok(VyreGpuSelfTest {
        direct_matches: direct.len(),
        coalesced_matches: coalesced.len(),
    })
}

/// One acquired peer proven by the production GPU region-presence self-test.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GpuRegionPresencePeerSelfTest {
    /// Exact scanner route exercised by the test.
    pub backend: crate::hw_probe::ScanBackend,
    /// `VyreBackend::id()` of the driver that ran the test.
    pub backend_id: &'static str,
    /// Number of findings emitted through the production GPU trigger path.
    pub matches: usize,
}

/// Status report from the production GPU region-presence self-test.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GpuRegionPresenceSelfTest {
    /// Every acquired CUDA or WGPU peer. All entries passed exact CPU parity.
    pub peers: Vec<GpuRegionPresencePeerSelfTest>,
}

/// Honest aggregate failure from the peer self-test.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GpuRegionPresenceSelfTestFailure {
    /// Exact peers acquired before parity execution began.
    pub acquired_backends: Vec<crate::hw_probe::ScanBackend>,
    /// Peer-specific acquisition, dispatch, or parity diagnostics.
    pub message: String,
}

impl std::fmt::Display for GpuRegionPresenceSelfTestFailure {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.write_str(&self.message)
    }
}

impl std::error::Error for GpuRegionPresenceSelfTestFailure {}

/// Build a minimal one-detector `CompiledScanner` and dispatch a scan through
/// the production GPU backend. A PASS proves device acquisition, compilation,
/// lowering, dispatch, and host readback on this host.
pub fn gpu_region_presence_self_test(
) -> Result<GpuRegionPresenceSelfTest, GpuRegionPresenceSelfTestFailure> {
    #[cfg(not(feature = "gpu"))]
    {
        Err(GpuRegionPresenceSelfTestFailure {
            acquired_backends: Vec::new(),
            message: "GPU region-presence self-test not available in the lean ci build. Rebuild with `--features gpu` to exercise the production GPU trigger path.".to_string(),
        })
    }
    #[cfg(feature = "gpu")]
    {
        gpu_region_presence_self_test_impl()
    }
}

#[cfg(feature = "gpu")]
fn gpu_region_presence_self_test_impl(
) -> Result<GpuRegionPresenceSelfTest, GpuRegionPresenceSelfTestFailure> {
    use crate::engine::CompiledScanner;
    use crate::hw_probe::ScanBackend;
    use keyhog_core::{Chunk, ChunkMetadata, DetectorFile};

    // The probe MUST be a credential keyhog actually REPORTS, not one it
    // suppresses. A plain dictionary word (e.g. "needle") triggers phase-1 and
    // extracts, but is then correctly dropped by low-entropy/placeholder
    // suppression on EVERY backend - so asserting "GPU found > 0" on such a word
    // is a false failure that has nothing to do with the GPU kernel. This probe
    // mirrors the proven `scan_engine_self_test` shape: a distinctive literal
    // PREFIX ("KHGPUSELFTEST_") that the GPU literal-set anchors on to drive
    // region-presence -> trigger, followed by a mixed-case high-entropy suffix
    // that survives suppression so the match is emitted end to end.
    const PLANTED: &str = "KHGPUSELFTEST_A1b2C3d4E5f6";
    let detector =
        toml::from_str::<DetectorFile>(include_str!("../../data/gpu-self-test-detector.toml"))
            .map(|file| file.detector)
            .map_err(|error| GpuRegionPresenceSelfTestFailure {
                acquired_backends: Vec::new(),
                message: format!("bundled GPU self-test detector TOML is invalid: {error}"),
            })?;

    let scanner = CompiledScanner::compile_with_gpu_policy(
        vec![detector],
        crate::compiled_scanner::GpuInitPolicy::ForceEnabled,
    )
    .map_err(|error| GpuRegionPresenceSelfTestFailure {
        acquired_backends: Vec::new(),
        message: format!("GPU scanner compilation failed during self-test: {error}"),
    })?;

    let candidates = scanner.gpu_backend_candidates();
    let acquired_backends: Vec<_> = candidates
        .iter()
        .filter(|candidate| candidate.is_eligible())
        .map(|candidate| candidate.backend)
        .collect();
    if acquired_backends.is_empty() {
        let diagnostics = candidates
            .iter()
            .map(|candidate| {
                let diagnostic = match candidate.acquisition_error.as_deref() {
                    Some(reason) => reason,
                    None => "driver was not acquired and returned no diagnostic",
                };
                format!("{}: {diagnostic}", candidate.backend.label())
            })
            .collect::<Vec<_>>()
            .join("; ");
        return Err(GpuRegionPresenceSelfTestFailure {
            acquired_backends,
            message: format!("no GPU region-presence peer was acquired ({diagnostics})"),
        });
    }

    let make_chunk = || Chunk {
        data: format!("gpu_secret = {PLANTED}").into(),
        metadata: ChunkMetadata::default(),
    };

    // CPU baseline on the SAME detector+chunk. This is the oracle: it proves the
    // planted secret is detectable AT ALL on this build, so a low GPU count means
    // a real GPU phase-1 divergence rather than an invalid/suppressed probe.
    let cpu_results = scanner
        .scan_chunks_with_backend(&[make_chunk()], ScanBackend::CpuFallback)
        .map_err(|error| GpuRegionPresenceSelfTestFailure {
            acquired_backends: acquired_backends.clone(),
            message: format!("CPU baseline dispatch failed during GPU self-test: {error}"),
        })?;
    let cpu_total: usize = cpu_results.iter().map(Vec::len).sum();
    if cpu_total == 0 {
        return Err(GpuRegionPresenceSelfTestFailure {
            acquired_backends,
            message: "GPU self-test probe matched on no backend (CPU baseline is zero); fix the self-test probe so it survives suppression.".to_string(),
        });
    }

    let mut peers = Vec::with_capacity(acquired_backends.len());
    let mut failures = Vec::new();
    for candidate in candidates
        .into_iter()
        .filter(|candidate| candidate.is_eligible())
    {
        let route = candidate.backend;
        let Some(backend_id) = candidate.driver_id else {
            failures.push(format!(
                "{}: acquired driver returned no identity",
                route.label()
            ));
            continue;
        };
        let degrade_before = scanner.runtime_status().gpu_degrade_count;
        let results = match scanner.scan_coalesced_gpu_region_presence(
            &[make_chunk()],
            route,
            scanner.execution_route_for_backend(route),
        ) {
            Ok(results) => results,
            Err(error) => {
                failures.push(format!(
                    "{} ({backend_id}): dispatch failed: {error}",
                    route.label()
                ));
                continue;
            }
        };
        if scanner.runtime_status().gpu_degrade_count > degrade_before {
            let diagnostic = match scanner.last_gpu_degrade_reason() {
                Some(reason) => reason,
                None => "runtime degrade recorded without a diagnostic".to_owned(),
            };
            failures.push(format!("{} ({backend_id}): {diagnostic}", route.label()));
            continue;
        }
        let total: usize = results.iter().map(Vec::len).sum();
        if total != cpu_total {
            failures.push(format!(
                "{} ({backend_id}): found {total} match(es), CPU found {cpu_total}",
                route.label()
            ));
            continue;
        }
        peers.push(GpuRegionPresencePeerSelfTest {
            backend: route,
            backend_id,
            matches: total,
        });
    }
    if !failures.is_empty() {
        let passed = peers
            .iter()
            .map(|peer| format!("{} ({})", peer.backend.label(), peer.backend_id))
            .collect::<Vec<_>>()
            .join(", ");
        let passed = if passed.is_empty() {
            "none".to_string()
        } else {
            passed
        };
        return Err(GpuRegionPresenceSelfTestFailure {
            acquired_backends,
            message: format!(
                "GPU region-presence peer parity failed: {}; passed peers: {passed}",
                failures.join("; ")
            ),
        });
    }
    Ok(GpuRegionPresenceSelfTest { peers })
}

#[cfg(target_os = "linux")]
fn is_keyhog_compute_symbol(symbol: &str) -> bool {
    let symbol = symbol.to_ascii_lowercase();
    let keyhog_owned = symbol.contains("keyhog_scanner")
        || symbol.contains("keyhog::")
        || symbol.contains("keyhog_");
    let compute_primitive = [
        "dispatch_kernel",
        "gpu_kernel",
        "kernel_main",
        "launch_kernel",
        "compute_shader",
    ]
    .iter()
    .any(|marker| symbol.contains(marker));
    let policy_or_test = symbol.contains("verify_gpu_kernel") || symbol.contains("test_gpu_kernel");
    keyhog_owned && compute_primitive && !policy_or_test
}

#[cfg(target_os = "linux")]
fn verify_linked_artifact(path: &std::path::Path) -> Result<(), String> {
    let bytes = std::fs::read(path).map_err(|error| {
        format!(
            "failed to read GPU ownership artifact '{}': {error}",
            path.display()
        )
    })?;
    let object = goblin::Object::parse(&bytes).map_err(|error| {
        format!(
            "failed to parse GPU ownership artifact '{}': {error}",
            path.display()
        )
    })?;
    let goblin::Object::Elf(elf) = object else {
        return Err(format!(
            "GPU ownership artifact '{}' is not a supported ELF executable",
            path.display()
        ));
    };
    let symbols: Vec<&str> = elf
        .syms
        .iter()
        .filter_map(|symbol| elf.strtab.get_at(symbol.st_name))
        .chain(
            elf.dynsyms
                .iter()
                .filter_map(|symbol| elf.dynstrtab.get_at(symbol.st_name)),
        )
        .collect();
    if symbols.is_empty() {
        return Err(format!(
            "GPU ownership artifact '{}' has no inspectable linked symbols",
            path.display()
        ));
    }
    if let Some(symbol) = symbols
        .iter()
        .copied()
        .find(|symbol| is_keyhog_compute_symbol(symbol))
    {
        return Err(format!(
            "KeyHog owns prohibited linked GPU compute symbol: {symbol}"
        ));
    }
    if !symbols.iter().any(|symbol| symbol.contains("vyre")) {
        return Err(format!(
            "GPU ownership artifact '{}' contains no linked VYRE symbols",
            path.display()
        ));
    }
    Ok(())
}

#[cfg(not(target_os = "linux"))]
fn verify_linked_artifact(path: &std::path::Path) -> Result<(), String> {
    Err(format!(
        "linked GPU ownership inspection is unavailable for '{}'; inspect the packaged source tree on this platform",
        path.display()
    ))
}

fn verify_source_artifacts(search_root: &std::path::Path) -> Result<(), String> {
    let forbidden_extensions = ["wgsl", "ptx", "cu", "spv", "metal", "hlsl"];
    let mut dirs_to_visit = vec![search_root.to_path_buf()];
    while let Some(dir) = dirs_to_visit.pop() {
        let entries = std::fs::read_dir(&dir).map_err(|error| {
            format!(
                "failed to enumerate GPU ownership source '{}': {error}",
                dir.display()
            )
        })?;
        for entry in entries {
            let path = entry
                .map_err(|error| format!("failed to enumerate GPU ownership source: {error}"))?
                .path();
            if path.is_dir() {
                if let Some(name) = path.file_name().and_then(|name| name.to_str()) {
                    if !name.starts_with('.') && name != "target" {
                        dirs_to_visit.push(path);
                    }
                }
            } else if path
                .extension()
                .and_then(|extension| extension.to_str())
                .is_some_and(|extension| forbidden_extensions.contains(&extension))
            {
                return Err(format!(
                    "KeyHog repository contains prohibited GPU kernel file: {}",
                    path.display()
                ));
            }
        }
    }
    Ok(())
}

/// Inspect a linked executable or packaged source tree using the ownership policy.
pub fn verify_gpu_kernel_ownership_separation_at_path(
    artifact: &std::path::Path,
) -> Result<(), String> {
    if artifact.is_file() {
        verify_linked_artifact(artifact)
    } else {
        verify_source_artifacts(artifact)
    }
}

/// Confirm the running scanner and its packaged source own no GPU compute kernels.
pub fn verify_gpu_kernel_ownership_separation() -> Result<(), String> {
    #[cfg(target_os = "linux")]
    verify_linked_artifact(
        &std::env::current_exe()
            .map_err(|error| format!("failed to locate running scanner artifact: {error}"))?,
    )?;
    verify_source_artifacts(std::path::Path::new(env!("CARGO_MANIFEST_DIR")))
}