加固工具

在产品发布之前,确保软件尽可能安全至关重要。这一过程称为“加固”(hardening),即增强系统的安全性,以保护其免受潜在威胁和漏洞的影响。

从高层来看,加固 Zephyr 应用可以分为两个方面:

  1. 禁用可能导致安全漏洞的特性和编译标志(例如确保不使用 “实验性” 特性,禁用断言、shell 等通常用于调试的特性)。

  2. 启用可提高安全性的可选特性(例如栈哨兵、硬件栈保护等)。其中一些特性可能依赖于硬件。

为简化这一过程,Zephyr 提供了一个 加固工具,用于根据 安全工作组 定义的一套加固偏好来分析应用的配置。该工具会检查构建目标中的 KConfig 选项,并提供针对性的建议和推荐,以调整与安全相关的选项。

用法

Using west:

# From the root of the zephyr repository
west build -b reel_board samples/hello_world
west build -t hardenconfig

Using CMake and ninja:

# From the root of the zephyr repository
# Use cmake to configure a Ninja-based buildsystem:
cmake -Bbuild -GNinja -DBOARD=reel_board samples/hello_world

# Now run the build tool on the generated build system:
ninja -Cbuild hardenconfig

输出应与下表类似。对于每个被设置为可能导致安全漏洞的值的配置选项,表中会给出应改用的推荐值。

+---------------------------------------+-----------+---------------+----------------+
| Name                                  | Current   | Recommended   | Check result   |
+=======================================+===========+===============+================+
| CONFIG_BUILD_OUTPUT_STRIPPED          | n         | y             | FAIL           |
+---------------------------------------+-----------+---------------+----------------+
| CONFIG_FAULT_DUMP                     | 2         | 0             | FAIL           |
+---------------------------------------+-----------+---------------+----------------+
| CONFIG_MPU_STACK_GUARD                | n         | y             | FAIL           |
+---------------------------------------+-----------+---------------+----------------+
| CONFIG_OVERRIDE_FRAME_POINTER_DEFAULT | n         | y             | FAIL           |
+---------------------------------------+-----------+---------------+----------------+
| CONFIG_STACK_SENTINEL                 | n         | y             | FAIL           |
+---------------------------------------+-----------+---------------+----------------+
| CONFIG_EXCEPTION_DEBUG                | y         | n             | FAIL           |
+---------------------------------------+-----------+---------------+----------------+
| CONFIG_PRINTK                         | y         | n             | FAIL           |
+---------------------------------------+-----------+---------------+----------------+