Skip to content

selftests/bpf: more precise cpu_mitigations state detection #9083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: bpf-next_base
Choose a base branch
from

Conversation

eddyz87
Copy link
Collaborator

@eddyz87 eddyz87 commented Jun 10, 2025

test_progs and test_verifier binaries execute unpriv tests under the following conditions:

  • unpriv BPF is enabled;
  • CPU mitigations are enabled (see [1] for details).

The detection of the "mitigations enabled" state is performed by unpriv_helpers.c:get_mitigations_off() via inspecting kernel boot command line, looking for a parameter "mitigations=off".

Such detection scheme won't work for certain configurations, e.g. when CONFIG_CPU_MIGITGATIONS is disabled and boot parameter is not supplied.

Miss-detection leads to test_progs executing tests meant to be run only with mitigations enabled, e.g.
verifier_and.c:known_subreg_with_unknown_reg(), and reporting false failures.

Internally, verifier sets bpf_verifier_env->bypass_spec_{v1,v4} basing on the value returned by kernel/cpu.c:cpu_mitigations_off(). This function is backed by a variable kernel/cpu.c:cpu_mitigations.

This state is not fully introspect-able via sysfs. The closest proxy is /sys/devices/system/cpu/vulnerabilities/spectre_v1, but it reports "vulnerable" state only if mitigations are disabled and current cpu is vulnerable, while verifier does not check cpu state.

There are only two ways the kernel/cpu.c:cpu_mitigations can be set:

  • via boot parameter;
  • via CONFIG_CPU_MIGITGATIONS option.

This commit updates unpriv_helpers.c:get_mitigations_off() to scan /proc/config.gz for CONFIG_CPU_MIGITGATIONS value in addition to boot command line check.

Tested using the following configurations:

  • mitigations enabled (unpriv tests are enabled)
  • mitigations disabled via boot cmdline (unpriv tests skipped)
  • mitigations disabled via CONFIG_CPU_MIGITGATIONS (unpriv tests skipped)

[1] https://lore.kernel.org/bpf/[email protected]/

@kernel-patches-daemon-bpf kernel-patches-daemon-bpf bot force-pushed the bpf-next_base branch 6 times, most recently from 6e514a8 to ddab433 Compare June 12, 2025 17:32
@eddyz87 eddyz87 force-pushed the read-config-in-mitigations-off branch from 3e45213 to d6bbc77 Compare June 12, 2025 21:15
@eddyz87 eddyz87 force-pushed the read-config-in-mitigations-off branch from d6bbc77 to 56eaf57 Compare June 12, 2025 23:48
@kernel-patches-daemon-bpf kernel-patches-daemon-bpf bot force-pushed the bpf-next_base branch 7 times, most recently from 6726d1b to 13e07b5 Compare June 13, 2025 22:09
@eddyz87 eddyz87 force-pushed the read-config-in-mitigations-off branch from 56eaf57 to b28f058 Compare June 14, 2025 02:15
test_progs and test_verifier binaries execute unpriv tests under the
following conditions:
- unpriv BPF is enabled;
- CPU mitigations are enabled (see [1] for details).

The detection of the "mitigations enabled" state is performed by
unpriv_helpers.c:get_mitigations_off() via inspecting kernel boot
command line, looking for a parameter "mitigations=off".

Such detection scheme won't work for certain configurations,
e.g. when CONFIG_CPU_MITIGATIONS is disabled and boot parameter is
not supplied.

Miss-detection leads to test_progs executing tests meant to be run
only with mitigations enabled, e.g.
verifier_and.c:known_subreg_with_unknown_reg(), and reporting false
failures.

Internally, verifier sets bpf_verifier_env->bypass_spec_{v1,v4}
basing on the value returned by kernel/cpu.c:cpu_mitigations_off().
This function is backed by a variable kernel/cpu.c:cpu_mitigations.

This state is not fully introspect-able via sysfs. The closest proxy
is /sys/devices/system/cpu/vulnerabilities/spectre_v1, but it reports
"vulnerable" state only if mitigations are disabled *and* current cpu
is vulnerable, while verifier does not check cpu state.

There are only two ways the kernel/cpu.c:cpu_mitigations can be set:
- via boot parameter;
- via CONFIG_CPU_MITIGATIONS option.

This commit updates unpriv_helpers.c:get_mitigations_off() to scan
/boot/config-$(uname -r) and /proc/config.gz for
CONFIG_CPU_MITIGATIONS value in addition to boot command line check.

Tested using the following configurations:
- mitigations enabled (unpriv tests are enabled)
- mitigations disabled via boot cmdline (unpriv tests skipped)
- mitigations disabled via CONFIG_CPU_MITIGATIONS
  (unpriv tests skipped)

[1] https://lore.kernel.org/bpf/[email protected]/

Reported-by: Mykyta Yatsenko <[email protected]>
Signed-off-by: Eduard Zingerman <[email protected]>
@eddyz87 eddyz87 force-pushed the read-config-in-mitigations-off branch from b28f058 to fc3efa3 Compare June 17, 2025 00:37
@kernel-patches-daemon-bpf kernel-patches-daemon-bpf bot force-pushed the bpf-next_base branch 6 times, most recently from 7801208 to a994d4a Compare June 19, 2025 16:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant