fix(sast): recognise common embedded vendor layouts (#369) #386

Merged
sharang merged 1 commits from fix/vendor-layouts into main 2026-09-25 09:58:24 +00:00
+114 -3
View File
@@ -6,8 +6,14 @@
//! are, so the SAST scanners skip it. The SBOM and CVE matching still cover it.
//!
//! Matching is deliberately narrow: a customer's own `src/drivers/` must never
//! be skipped. Only names that are vendor SDK conventions count, plus the
//! STM32Cube layout, where `Drivers/` sits next to the generated `Core/`.
//! be skipped. What counts:
//! - names that are vendor SDK conventions (`CMSIS`, `Third_Party`, `vendor`,
//! `…_HAL_Driver`, `STM32…_HAL`);
//! - ST's generated layouts, by ST's own capitalisation: `Drivers/` next to
//! `Core/`, `Middlewares/`, and `Middleware/` next to `Drivers/` (the
//! FreeRTOS/ST reference projects);
//! - a vendored library wherever it sits, recognised by the files it always
//! ships (Mbed TLS, lwIP, the FreeRTOS kernel, wolfSSL, FatFs).
use std::path::Path;
@@ -26,6 +32,23 @@ const VENDOR_DIR_NAMES: &[&str] = &[
/// `STM32F4xx_HAL_Driver` or `STM32F10x_StdPeriph_Driver`.
const VENDOR_DIR_SUFFIXES: &[&str] = &["_hal_driver", "_stdperiph_driver"];
/// Files a third-party library always ships, relative to its root; a
/// directory holding all the files of one entry is that library.
const LIBRARY_MARKERS: &[&[&str]] = &[
// Mbed TLS 2.x and 3.x.
&["include/mbedtls/version.h"],
&["include/mbedtls/build_info.h"],
// lwIP.
&["src/include/lwip/init.h"],
// The FreeRTOS kernel.
&["tasks.c", "include/FreeRTOS.h"],
// wolfSSL.
&["wolfssl/version.h", "src/ssl.c"],
// FatFs, flat or with its `source/` folder.
&["ff.c", "ff.h"],
&["source/ff.c", "source/ff.h"],
];
/// Every entry under `root` outside `.git` and the `excluded` directories
/// (relative paths with `/` separators, as [`vendor_dirs`] returns them).
pub fn walk_outside<'a>(
@@ -83,6 +106,13 @@ fn is_vendor_dir(name: &str, path: &Path) -> bool {
if VENDOR_DIR_SUFFIXES.iter().any(|s| name.ends_with(s)) {
return true;
}
// ST's HAL without the `_Driver` suffix, e.g. `STM32U5xx_HAL`.
if name.starts_with("stm32") && name.ends_with("_hal") {
return true;
}
if is_st_layout_dir(path) || is_library_root(path) {
return true;
}
if VENDOR_DIR_NAMES.contains(&name) {
// Zephyr keeps the project's own boards at `boards/<vendor>/<board>`,
// and its template names that folder literally `vendor`.
@@ -96,6 +126,30 @@ fn is_vendor_dir(name: &str, path: &Path) -> bool {
.is_some_and(|parent| has_child_dir(parent, "core"))
}
/// Whether `path` is one of ST's generated vendor folders, by ST's own
/// capitalisation (a customer's lower-case `middleware/` is their code):
/// `Middlewares/` always, `Middleware/` and `Drivers/` when they sit together.
fn is_st_layout_dir(path: &Path) -> bool {
let Some(name) = path.file_name().and_then(|n| n.to_str()) else {
return false;
};
let sibling = |other: &str| path.parent().is_some_and(|p| p.join(other).is_dir());
match name {
"Middlewares" => true,
"Middleware" => sibling("Drivers"),
"Drivers" => sibling("Middleware") || sibling("Middlewares"),
_ => false,
}
}
/// Whether `path` is the root of a vendored third-party library: it holds
/// all the marker files of one entry of [`LIBRARY_MARKERS`].
fn is_library_root(path: &Path) -> bool {
LIBRARY_MARKERS
.iter()
.any(|files| files.iter().all(|f| path.join(f).is_file()))
}
/// Whether the parent of `path` is named `name`, case-insensitively.
fn parent_is(path: &Path, name: &str) -> bool {
path.parent()
@@ -130,6 +184,17 @@ mod tests {
Tree(root)
}
}
impl Tree {
/// Add empty files (paths relative to the root).
fn with_files(self, files: &[&str]) -> Self {
for f in files {
let path = self.0.join(f);
std::fs::create_dir_all(path.parent().expect("parent")).expect("mkdir");
std::fs::write(path, "").expect("write");
}
self
}
}
impl Drop for Tree {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
@@ -144,7 +209,53 @@ mod tests {
"Drivers/CMSIS/Include",
"Middlewares/Third_Party/FreeRTOS",
]);
assert_eq!(vendor_dirs(&t.0), ["Drivers", "Middlewares/Third_Party"]);
// All of `Middlewares/` is ST's (USB, FreeRTOS, third-party alike).
assert_eq!(vendor_dirs(&t.0), ["Drivers", "Middlewares"]);
}
#[test]
fn the_freertos_reference_layout_marks_middleware_and_drivers() {
// FreeRTOS/iot-reference-stm32u5: no Core/, `Middleware/` singular,
// the HAL without the `_Driver` suffix.
let t = Tree::new(&[
"Drivers/STM32U5xx_HAL/Src",
"Drivers/CMSIS/Include",
"Middleware/ARM/mbedtls",
"Middleware/FreeRTOS/Kernel",
"Projects/b_u585i_iot02a_ntz/Src",
]);
assert_eq!(vendor_dirs(&t.0), ["Drivers", "Middleware"]);
}
#[test]
fn middlewares_is_vendor_code_without_third_party() {
// anon767/STM32F7HTTPS: Mbed TLS directly under `Middlewares/`.
let t = Tree::new(&["Src", "Inc", "Middlewares/mbedTLS/library"]);
assert_eq!(vendor_dirs(&t.0), ["Middlewares"]);
}
#[test]
fn a_vendored_library_is_recognised_by_its_files() {
let t = Tree::new(&["src/app"]).with_files(&[
"lib/mbedtls/include/mbedtls/version.h",
"lib/lwip/src/include/lwip/init.h",
"lib/kernel/tasks.c",
"lib/kernel/include/FreeRTOS.h",
"components/fatfs/source/ff.c",
"components/fatfs/source/ff.h",
"src/app/main.c",
]);
assert_eq!(
vendor_dirs(&t.0),
["components/fatfs", "lib/kernel", "lib/lwip", "lib/mbedtls"]
);
}
#[test]
fn a_customers_own_middleware_folder_is_kept() {
// Lower-case, not ST's generated name: the customer's code.
let t = Tree::new(&["src/middleware/can", "src/drivers/uart", "src/app"]);
assert!(vendor_dirs(&t.0).is_empty());
}
#[test]