Skip to content

Commit fe4ce43

Browse files
eddyz87Kernel Patches Daemon
authored andcommitted
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_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]>
1 parent 13e07b5 commit fe4ce43

File tree

1 file changed

+91
-3
lines changed

1 file changed

+91
-3
lines changed

tools/testing/selftests/bpf/unpriv_helpers.c

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

2889
cmdline[sizeof(cmdline) - 1] = '\0';
2990
for (c = strtok(cmdline, " \n"); c; c = strtok(NULL, " \n")) {
30-
if (strncmp(c, "mitigations=off", strlen(c)))
91+
if (strncmp(c, pat, strlen(c)))
3192
continue;
3293
ret = true;
3394
break;
@@ -37,8 +98,21 @@ static bool get_mitigations_off(void)
3798
return ret;
3899
}
39100

101+
static int get_mitigations_off(void)
102+
{
103+
int enabled_in_config;
104+
105+
if (cmdline_contains("mitigations=off"))
106+
return true;
107+
enabled_in_config = config_contains("CONFIG_CPU_MITIGATIONS=y");
108+
if (enabled_in_config < 0)
109+
return -1;
110+
return !enabled_in_config;
111+
}
112+
40113
bool get_unpriv_disabled(void)
41114
{
115+
int mitigations_off;
42116
bool disabled;
43117
char buf[2];
44118
FILE *fd;
@@ -52,5 +126,19 @@ bool get_unpriv_disabled(void)
52126
disabled = true;
53127
}
54128

55-
return disabled ? true : get_mitigations_off();
129+
if (disabled)
130+
return true;
131+
132+
/*
133+
* Some unpriv tests rely on spectre mitigations being on.
134+
* If mitigations are off or status can't be determined
135+
* assume that unpriv tests are disabled.
136+
*/
137+
mitigations_off = get_mitigations_off();
138+
if (mitigations_off < 0) {
139+
fprintf(stderr,
140+
"Can't determine if mitigations are enabled, disabling unpriv tests.");
141+
return true;
142+
}
143+
return mitigations_off;
56144
}

0 commit comments

Comments
 (0)