ssbd-tools 项目是一个程序集合,用于利用 x86 处理器中提供的 Speculative Store Bypass Disable (SSBD) 功能。SSBD 是一种基于处理器的缓解措施,针对被称为 Variant 4 并分配了 CVE-2018-3639 的 Speculative Store Bypass 攻击。Linux 内核引入了用于使用 SSBD 的每进程控制,这些工具可用于利用这些控制并帮助验证其正确性。
Linux 内核提供了几种不同的操作模式,可以在启动时通过 spec_store_bypass_disable 内核参数为 x86 系统上的 SSBD 进行选择。内核参数文档 对此有很好的描述。以下是该文档中描述这些选项的片段:
on - Unconditionally disable Speculative Store Bypass
off - Unconditionally enable Speculative Store Bypass
auto - Kernel detects whether the CPU model contains an
implementation of Speculative Store Bypass and
picks the most appropriate mitigation. If the
CPU is not vulnerable, "off" is selected. If the
CPU is vulnerable the default mitigation is
architecture and Kconfig dependent. See below.
prctl - Control Speculative Store Bypass per thread
via prctl. Speculative Store Bypass is enabled
for a process by default. The state of the control
is inherited on fork.
seccomp - Same as "prctl" above, but all seccomp threads
will disable SSB unless they explicitly opt out.
Not specifying this option is equivalent to
spec_store_bypass_disable=auto.
Default mitigations:
X86: If CONFIG_SECCOMP=y "seccomp", otherwise "prctl"
SSBD 功能通过设置处理器的一个特定型号寄存器(MSR)中的单个位来启用/禁用。确切的 MSR 以及 MSR 内的位位置因 CPU 厂商、同一厂商的不同 CPU 系列以及内核是否在虚拟机监视器(hypervisor)下运行而不同。这些工具遵循 Intel 和 AMD 发布的指南,以确定 SSBD 位在当前执行环境中的位置。
某些系统可能没有可用的 SSBD 支持。这可能是由于多种原因:
intel-microcode 和 amd64-microcode)来获取更新后的微码。AMD 15h、16h 和 17h 系列处理器不需要更新微码。ssbd-exec 程序在执行另一个程序之前使用每进程 SSBD 控制。它可以使用 PR_SET_SPECULATION_CTRL prctl 来允许推测执行(-p enable 使用 PR_SPEC_ENABLE)、通过 SSBD 禁止推测执行(-p disable 使用 PR_SPEC_DISABLE)、或通过 SSBD 永久禁止所有未来子进程中的推测执行(-p force-disable 使用 PR_SPEC_FORCE_DISABLE)。
它还可以加载一个宽松的 seccomp 过滤器(-s empty),在 x86 上默认会使进程选择加入 SSBD 缓解。还有一个选项(-s spec-allow)用于加载不会使进程选择加入 SSBD 的宽松过滤器。
$ ./ssbd-exec -- grep Spec /proc/self/status
Speculation_Store_Bypass: thread vulnerable
$ ./ssbd-exec -p disable -- grep -e Spec -e Seccomp /proc/self/status
Seccomp: 0
Speculation_Store_Bypass: thread mitigated
$ ./ssbd-exec -s empty -- grep -e Spec -e Seccomp /proc/self/status
Seccomp: 2
Speculation_Store_Bypass: thread force mitigated
ssbd-verify 程序验证实际 SSBD 位(位于特定于你的处理器的 MSR 和位偏移中)是否为预期值。0 表示未使用 SSBD,而 1 表示正在使用。
该程序还可用于验证任务的 PR_GET_SPECULATION_CTRL prctl 值是否设置为允许推测执行(-p enable 验证 PR_SPEC_ENABLE)、通过 SSBD 禁止推测执行(-p disable 验证 PR_SPEC_DISABLE)、或通过 SSBD 永久禁止所有未来子进程中的推测执行(-p force-disable 验证 PR_SPEC_FORCE_DISABLE)。
该程序可以配置为使用 -t SECONDS 选项重复验证 SSBD 位的值。如果 SECONDS 为 0,程序将在验证 SSBD 位的同时无限循环。SECONDS 为非零值时,程序将在指定的时间内验证 SSBD 位。
该程序要求加载 msr 内核模块,并且用户具有 root 权限才能从相应的 MSR 读取 SSBD 位。
$ sudo ./ssbd-verify 0
$ sudo ./ssbd-verify 1
FAIL: SSBD bit verification failed (expected 1, got 0)
$ ./ssbd-exec -p disable -- sudo ./ssbd-verify 1
$ sudo ./ssbd-exec -s empty -- ./ssbd-verify 1
注意:上面的命令需要对 ssbd-exec 使用 sudo,因为在加载 seccomp 过滤器之前使用了 NO_NEW_PRIVS。如果在设置 NO_NEW_PRIVS 之后使用 sudo,则无法提升权限。
ssbd-toggle 程序只是在一个无限循环中反复切换 SSBD 位的开和关,直到程序被终止。它可以与 ssbd-verify 结合使用,以确保内核切换到其任务时,ssbd-verify 进程始终具有预期的 SSBD 位值。
该程序要求加载 msr 内核模块,并且用户具有 root 权限才能读写相应的 MSR。
$ sudo ./ssbd-toggle
^C
要构建这些工具,请运行 make:
$ make
要运行一些基本的自动化测试以确保 SSBD 在你的系统上按预期工作,请以 root 身份运行 check 目标:
$ sudo make check
PASS