Skip to content

Commit 56eaf57

Browse files
committed
selftests/bpf: more precise cpu_mitigations state detection
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]/ Signed-off-by: Eduard Zingerman <[email protected]>
1 parent 1c66f4a commit 56eaf57

File tree

1 file changed

+65
-2
lines changed

1 file changed

+65
-2
lines changed

tools/testing/selftests/bpf/unpriv_helpers.c

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,73 @@
11
// SPDX-License-Identifier: GPL-2.0-only
22

3+
#include <errno.h>
34
#include <stdbool.h>
45
#include <stdlib.h>
56
#include <stdio.h>
67
#include <string.h>
8+
#include <sys/utsname.h>
79
#include <unistd.h>
810
#include <fcntl.h>
11+
#include <zlib.h>
912

1013
#include "unpriv_helpers.h"
1114

12-
static bool get_mitigations_off(void)
15+
static gzFile open_config(void)
16+
{
17+
struct utsname uts;
18+
char buf[PATH_MAX];
19+
gzFile config;
20+
21+
if (uname(&uts)) {
22+
perror("uname");
23+
} else {
24+
snprintf(buf, sizeof(buf), "/boot/config-%s", uts.release);
25+
config = gzopen(buf, "rb");
26+
if (config)
27+
return config;
28+
fprintf(stderr, "gzopen %s: %s\n", buf, strerror(errno));
29+
}
30+
config = gzopen("/proc/config.gz", "rb");
31+
if (!config)
32+
perror("gzopen /proc/config.gz");
33+
return config;
34+
}
35+
36+
static bool config_contains(const char *pat)
37+
{
38+
bool ret = false;
39+
const char *msg;
40+
char buf[1024];
41+
gzFile config;
42+
int n, err;
43+
44+
config = open_config();
45+
if (!config)
46+
goto out;
47+
48+
for (;;) {
49+
if (!gzgets(config, buf, sizeof(buf))) {
50+
msg = gzerror(config, &err);
51+
if (err == Z_ERRNO)
52+
perror("gzgets /proc/config.gz");
53+
else if (err != Z_OK)
54+
fprintf(stderr, "gzgets /proc/config.gz: %s", msg);
55+
goto out;
56+
}
57+
n = strlen(buf);
58+
if (buf[n - 1] == '\n')
59+
buf[n - 1] = 0;
60+
if (strcmp(buf, pat) == 0) {
61+
ret = true;
62+
goto out;
63+
}
64+
}
65+
out:
66+
gzclose(config);
67+
return ret;
68+
}
69+
70+
static bool cmdline_contains(const char *pat)
1371
{
1472
char cmdline[4096], *c;
1573
int fd, ret = false;
@@ -27,7 +85,7 @@ static bool get_mitigations_off(void)
2785

2886
cmdline[sizeof(cmdline) - 1] = '\0';
2987
for (c = strtok(cmdline, " \n"); c; c = strtok(NULL, " \n")) {
30-
if (strncmp(c, "mitigations=off", strlen(c)))
88+
if (strncmp(c, pat, strlen(c)))
3189
continue;
3290
ret = true;
3391
break;
@@ -37,6 +95,11 @@ static bool get_mitigations_off(void)
3795
return ret;
3896
}
3997

98+
static bool get_mitigations_off(void)
99+
{
100+
return cmdline_contains("mitigations=off") || !config_contains("CONFIG_CPU_MITIGATIONS=y");
101+
}
102+
40103
bool get_unpriv_disabled(void)
41104
{
42105
bool disabled;

0 commit comments

Comments
 (0)