From b3da5ece33bde07fb6cf4a4818e2cf8592f464c9 Mon Sep 17 00:00:00 2001 From: Sharang Parnerkar <30073382+mighty840@users.noreply.github.com> Date: Fri, 25 Sep 2026 12:58:38 +0200 Subject: [PATCH] feat(controls): the grounded surface check reads C and C++ (#376) The absence-based controls (integrity, updates, logging, access) were never checked on firmware: the surface check read no C/C++. It now does, without the vendor SDK, with embedded terms (crc32, ota_, bootloader, hal_flash, secure_boot, ecdsa, syslog); the control map counts C/C++ as checked for these controls. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p --- compliance-agent/src/controls/scan_triage.rs | 7 +- compliance-agent/src/controls/surface.rs | 106 +++++++++++++++---- control-map/data/cra_control_map.json | 32 ++++-- 3 files changed, 114 insertions(+), 31 deletions(-) diff --git a/compliance-agent/src/controls/scan_triage.rs b/compliance-agent/src/controls/scan_triage.rs index f6a5c40..0169d75 100644 --- a/compliance-agent/src/controls/scan_triage.rs +++ b/compliance-agent/src/controls/scan_triage.rs @@ -146,13 +146,18 @@ pub async fn grounded_surface_findings( return Vec::new(); } let checker = GroundedControlChecker::new(LlmControlJudge::new(llm)); + // The vendor SDK is not the customer's code: its surfaces are left out. + let tree = repo_path.to_path_buf(); + let vendor = tokio::task::spawn_blocking(move || crate::pipeline::vendor::vendor_dirs(&tree)) + .await + .unwrap_or_default(); let mut out = Vec::new(); for surf in surface::SURFACES { let Some(spec) = specs.get(surf.control_id) else { continue; // catalog doesn't carry this control }; - let regions = surface::retrieve(repo_path, surf.terms); + let regions = surface::retrieve(repo_path, surf.terms, &vendor); if regions.is_empty() { continue; } diff --git a/compliance-agent/src/controls/surface.rs b/compliance-agent/src/controls/surface.rs index f2d7fae..11865da 100644 --- a/compliance-agent/src/controls/surface.rs +++ b/compliance-agent/src/controls/surface.rs @@ -9,7 +9,9 @@ //! finding grounds to the surface snippet, so nothing fabricated survives. //! //! Retrieval is intentionally cheap and bounded: keyword match + a fixed window, -//! capped per control to keep the downstream LLM cost predictable. +//! capped per control to keep the downstream LLM cost predictable. C and C++ +//! firmware is read too, without its vendor SDK (HAL, CMSIS, middleware), which +//! would otherwise fill the cap with code the customer does not own. use std::path::Path; @@ -34,6 +36,7 @@ pub const SURFACES: &[Surface] = &[ "hmac", "integrity", "verify", + "crc32", ], }, Surface { @@ -60,29 +63,61 @@ pub const SURFACES: &[Surface] = &[ }, Surface { control_id: "cra-ai-24", // Security-Logging - terms: &["login", "authorize", "permission", "role", "admin", "audit"], + terms: &[ + "login", + "authorize", + "permission", + "role", + "admin", + "audit", + "syslog", + ], }, Surface { control_id: "cra-ai-27", // Log-Integrität und -Aufbewahrung - terms: &["logging", "logger", "getlogger", "audit_log"], + terms: &["logging", "logger", "getlogger", "audit_log", "syslog"], }, Surface { control_id: "cra-ai-28", // Sichere Update-Mechanismen - terms: &["update", "upgrade", "download", "firmware"], + terms: &[ + "update", + "upgrade", + "download", + "firmware", + "bootloader", + "ota_", + "hal_flash", + ], }, Surface { control_id: "cra-ai-29", // Update-Authentizität - terms: &["update", "signature", "verify", "pubkey", "certificate"], + terms: &[ + "update", + "signature", + "verify", + "pubkey", + "certificate", + "ecdsa", + "secure_boot", + ], }, Surface { control_id: "cra-ai-30", // Update-Integrität - terms: &["update", "checksum", "digest", "integrity", "verify"], + terms: &[ + "update", + "checksum", + "digest", + "integrity", + "verify", + "crc32", + ], }, ]; /// Source file extensions worth reading (skip binaries/assets/lockfiles). const CODE_EXTS: &[&str] = &[ - "py", "js", "ts", "tsx", "jsx", "go", "java", "rb", "php", "rs", "cs", "kt", + "py", "js", "ts", "tsx", "jsx", "go", "java", "rb", "php", "rs", "cs", "kt", "c", "h", "cc", + "cpp", "hpp", ]; /// Directories never worth walking. const SKIP_DIRS: &[&str] = &[ @@ -103,12 +138,13 @@ const MAX_REGIONS_PER_CONTROL: usize = 8; const MAX_FILE_BYTES: u64 = 512 * 1024; /// Deterministically retrieve up to [`MAX_REGIONS_PER_CONTROL`] code regions in -/// `repo_path` whose lines mention any of `terms`. Hits close together within a -/// file are merged into one region; results are capped to bound LLM cost. -pub fn retrieve(repo_path: &Path, terms: &[&str]) -> Vec { +/// `repo_path` whose lines mention any of `terms`, outside the `excluded` +/// (vendor) directories. Hits close together within a file are merged into one +/// region; results are capped to bound LLM cost. +pub fn retrieve(repo_path: &Path, terms: &[&str], excluded: &[String]) -> Vec { let lowered: Vec = terms.iter().map(|t| t.to_lowercase()).collect(); let mut regions = Vec::new(); - for entry in walk(repo_path) { + for entry in walk(repo_path, excluded) { if regions.len() >= MAX_REGIONS_PER_CONTROL { break; } @@ -173,15 +209,17 @@ fn has_code_ext(path: &Path) -> bool { .is_some_and(|e| CODE_EXTS.contains(&e)) } -fn walk(root: &Path) -> Vec { - walkdir::WalkDir::new(root) - .into_iter() - .filter_entry(|e| { - let name = e.file_name().to_string_lossy(); - !SKIP_DIRS.contains(&name.as_ref()) - }) - .filter_map(|e| e.ok()) - .collect() +/// Every entry under `root` outside the `excluded` directories and +/// [`SKIP_DIRS`]. +fn walk<'a>( + root: &'a Path, + excluded: &'a [String], +) -> impl Iterator + 'a { + crate::pipeline::vendor::walk_outside(root, excluded).filter(|e| { + !e.path() + .components() + .any(|c| SKIP_DIRS.contains(&c.as_os_str().to_string_lossy().as_ref())) + }) } #[cfg(test)] @@ -231,7 +269,7 @@ mod tests { "app/auth.py", "import x\n\n\n\n\n\n\ndef login(u, p):\n return check(u, p)\n", ); - let regions = retrieve(&dir, terms_for("cra-ai-11")); + let regions = retrieve(&dir, terms_for("cra-ai-11"), &[]); assert_eq!(regions.len(), 1); assert!(regions[0].content.contains("def login")); assert_eq!(regions[0].file, "app/auth.py"); @@ -244,7 +282,31 @@ mod tests { std::fs::create_dir_all(&dir).unwrap(); write(&dir, "README.md", "login and password and audit\n"); // not code ext write(&dir, "node_modules/pkg/index.js", "function login() {}\n"); // vendored - assert!(retrieve(&dir, terms_for("cra-ai-11")).is_empty()); + assert!(retrieve(&dir, terms_for("cra-ai-11"), &[]).is_empty()); + let _ = std::fs::remove_dir_all(&dir); + } + + #[test] + fn firmware_update_code_is_read_but_not_the_vendor_hal() { + let dir = std::env::temp_dir().join(format!("surface-{}", uuid::Uuid::new_v4())); + std::fs::create_dir_all(&dir).unwrap(); + write( + &dir, + "Core/Src/ota.c", + "#include \"ota.h\"\n\n\n\n\n\n\nint ota_apply(const uint8_t *image) {\n return HAL_FLASH_Program(0, 0, 0);\n}\n", + ); + write( + &dir, + "Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c", + "HAL_StatusTypeDef HAL_FLASH_Program(void) { return 0; }\n", + ); + write(&dir, "Core/Inc/main.h", "#pragma once\n"); + + let vendor = crate::pipeline::vendor::vendor_dirs(&dir); + let regions = retrieve(&dir, terms_for("cra-ai-28"), &vendor); + + assert_eq!(regions.len(), 1, "{regions:?}"); + assert_eq!(regions[0].file, "Core/Src/ota.c"); let _ = std::fs::remove_dir_all(&dir); } diff --git a/control-map/data/cra_control_map.json b/control-map/data/cra_control_map.json index 8bd4977..05fbba3 100644 --- a/control-map/data/cra_control_map.json +++ b/control-map/data/cra_control_map.json @@ -71,7 +71,9 @@ "php", "rust", "csharp", - "kotlin" + "kotlin", + "c", + "cpp" ], "cwe": [], "rules": [] @@ -211,7 +213,9 @@ "php", "rust", "csharp", - "kotlin" + "kotlin", + "c", + "cpp" ], "cwe": [], "rules": [] @@ -237,7 +241,9 @@ "php", "rust", "csharp", - "kotlin" + "kotlin", + "c", + "cpp" ], "cwe": [], "rules": [] @@ -511,7 +517,9 @@ "php", "rust", "csharp", - "kotlin" + "kotlin", + "c", + "cpp" ], "cwe": [], "rules": [] @@ -551,7 +559,9 @@ "php", "rust", "csharp", - "kotlin" + "kotlin", + "c", + "cpp" ], "cwe": [], "rules": [] @@ -577,7 +587,9 @@ "php", "rust", "csharp", - "kotlin" + "kotlin", + "c", + "cpp" ], "cwe": [], "rules": [] @@ -603,7 +615,9 @@ "php", "rust", "csharp", - "kotlin" + "kotlin", + "c", + "cpp" ], "cwe": [], "rules": [] @@ -629,7 +643,9 @@ "php", "rust", "csharp", - "kotlin" + "kotlin", + "c", + "cpp" ], "cwe": [], "rules": []