TL;DR
Re-hosting embedded firmware in emulators is often a manual, "trial-and-error" nightmare because of complex hardware dependencies. This paper introduces an automated approach that uses graph-cut algorithms and simulated annealing to identify the optimal boundary where hardware-dependent code can be swapped for high-level models, significantly reducing the manual effort required for firmware security testing.
What Problem Does This Paper Address?
Embedded systems are everywhere, but testing their firmware is difficult. You either need the physical hardware (which doesn't scale) or an emulator like QEMU. While QEMU is great at emulating CPUs, it lacks support for the thousands of unique peripherals (sensors, radios, timers) found in the wild.
To get around this, researchers use "re-hosting"—running the firmware in a virtual environment by replacing hardware-dependent code with high-level software models. However, deciding which functions to replace is currently a slow, manual process: you run the firmware until it crashes, identify the hardware dependency, write a model, and repeat. This paper aims to automate that decision-making process.
Methods
The authors treat the firmware binary as a mathematical graph where nodes are functions and edges are function calls. Their approach follows four main steps:
- Graph Construction: Using Ghidra, they perform static binary analysis to build a Control Flow Graph (CFG) and a Data Flow Graph (DFG).
- Node Classification: They categorize functions into four groups:
- Must Retain: Core logic the user wants to test.
- Must Remove: Functions that directly touch hardware (identified via memory-mapped I/O addresses).
- Suggested Retain/Remove: Functions in the "middle" that could go either way.
- Weighting: They assign a "cost" to each node based on two factors:
- Effort: How hard is it to write a proxy for this function? (e.g., functions that write to global memory are harder to model).
- Utility: How much of the original code's functionality is preserved if we keep this node?
- Optimization: They use a Min-Cut algorithm to find the initial "cheapest" boundary to separate hardware from logic. They then refine this using Simulated Annealing, an iterative optimization technique that helps find "better" solutions by escaping local minima in the graph.
Datasets
The researchers evaluated their approach using several real-world firmware sets:
- STM32-Cube HAL library: Standard firmware for STM32F4 boards.
- STM Nucleo programs: A selection of programs previously used in the "Pretender" re-hosting project.
- Atmel Advanced Software Framework: Firmware for SAM R21 boards.
- Specific applications included UART-printf, FatFS (file system), and various network stacks (UDP/TCP echo clients and servers).
Key Insights and Ideas
- The "Effort vs. Utility" Trade-off: The core "aha" moment is that re-hosting isn't just about removing hardware calls; it's about finding the point of least resistance. If a function is "leaky" (affects global state), it's a bad candidate for removal because your manual model would have to be incredibly complex to maintain that state.
- Scaling the Problem: By targeting the Hardware Abstraction Layer (HAL) rather than individual hardware registers, the problem scales from supporting 10,000+ unique devices to supporting just a few dozen HALs.
- Policy-Driven Emulation: The system allows users to set a "policy" (via weights), letting them choose whether they want a high-fidelity emulation (more manual work, more original code) or a quick-and-dirty version (less work, more modeled code).
Key Results
- Monotonic Scaling: The evaluation showed that as the "Utility" weight increases, the proportion of nodes retained increases predictably, proving the algorithm behaves logically across different firmware vendors.
- Efficiency: The Min-Cut algorithm is extremely fast (under 0.2 seconds), while even the most complex Simulated Annealing runs (100,000 iterations) finished in about 98 seconds.
- Manual Work Reduction: In a manual example (UDP-echo-client), the algorithm suggested a cut that required implementing only one node on the boundary instead of four, significantly reducing the developer's workload.
- Static Analysis Limits: The authors quantified how much static analysis misses. For example, they had to write custom scripts to handle "ARM Thumb mode" addressing, which standard tools often misinterpret.
Limitations and Future Work
- Static Analysis Imperfection: The approach relies heavily on Ghidra. If the binary analysis fails to resolve a pointer or a function call (common in complex C++ or obfuscated code), the resulting graph—and the "cut"—will be inaccurate.
- Thumb Mode Complexity: Handling the +1 pointer offset in ARM Thumb mode remains a subtle challenge for automated tools.
- Comparison Difficulties: The authors noted it is difficult to perform a direct head-to-head comparison with the original HALucinator because that tool didn't share the same explicit goal of removing all hardware interactions.
- Future Direction: Integrating this automated "cutting" directly into an emulation loop to provide real-time suggestions to researchers as they develop hardware models.
Summary updated