Skip to content

Commit d6bbc77

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 d6bbc77

File tree

1 file changed

+66
-2
lines changed

1 file changed

+66
-2
lines changed

tools/testing/selftests/bpf/unpriv_helpers.c

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,74 @@
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+
config = gzopen("/proc/config.gz", "rb");
22+
if (config)
23+
return config;
24+
perror("gzopen /proc/config.gz");
25+
if (uname(&uts) < 0) {
26+
perror("uname");
27+
return NULL;
28+
}
29+
snprintf(buf, sizeof(buf), "/boot/config-%s", uts.release);
30+
config = gzopen(buf, "rb");
31+
if (config)
32+
return config;
33+
fprintf(stderr, "gzopen %s: %s\n", buf, strerror(errno));
34+
return NULL;
35+
}
36+
37+
static bool scan_config(const char *pat)
38+
{
39+
bool ret = false;
40+
const char *msg;
41+
char buf[1024];
42+
gzFile config;
43+
int n, err;
44+
45+
config = open_config();
46+
if (!config)
47+
goto out;
48+
49+
for (;;) {
50+
if (!gzgets(config, buf, sizeof(buf))) {
51+
msg = gzerror(config, &err);
52+
if (err == Z_ERRNO)
53+
perror("gzgets /proc/config.gz");
54+
else if (err != Z_OK)
55+
fprintf(stderr, "gzgets /proc/config.gz: %s", msg);
56+
goto out;
57+
}
58+
n = strlen(buf);
59+
if (buf[n - 1] == '\n')
60+
buf[n - 1] = 0;
61+
if (strcmp(buf, pat) == 0) {
62+
ret = true;
63+
goto out;
64+
}
65+
}
66+
out:
67+
gzclose(config);
68+
return ret;
69+
}
70+
71+
static bool scan_cmdline(const char *pat)
1372
{
1473
char cmdline[4096], *c;
1574
int fd, ret = false;
@@ -27,7 +86,7 @@ static bool get_mitigations_off(void)
2786

2887
cmdline[sizeof(cmdline) - 1] = '\0';
2988
for (c = strtok(cmdline, " \n"); c; c = strtok(NULL, " \n")) {
30-
if (strncmp(c, "mitigations=off", strlen(c)))
89+
if (strncmp(c, pat, strlen(c)))
3190
continue;
3291
ret = true;
3392
break;
@@ -37,6 +96,11 @@ static bool get_mitigations_off(void)
3796
return ret;
3897
}
3998

99+
static bool get_mitigations_off(void)
100+
{
101+
return scan_cmdline("mitigations=off") || !scan_config("CONFIG_CPU_MITIGATIONS=y");
102+
}
103+
40104
bool get_unpriv_disabled(void)
41105
{
42106
bool disabled;

0 commit comments

Comments
 (0)