linux/tools/testing/selftests/bpf/unpriv_helpers.c
Eduard Zingerman cd7312a78f selftests/bpf: include limits.h needed for PATH_MAX directly
Constant PATH_MAX is used in function unpriv_helpers.c:open_config().
This constant is provided by include file <limits.h>.
The dependency was added by commit [1], which does not include
<limits.h> directly, relying instead on <limits.h> being included from
zlib.h -> zconf.h.
As it turns out, this is not the case for all systems, e.g. on
Fedora 41 zlib 1.3.1 is used, and there <limits.h> is not included
from zconf.h. Hence, there is a compilation error on Fedora 41.

[1] commit fc2915bb8b ("selftests/bpf: More precise cpu_mitigations state detection")

Fixes: fc2915bb8b ("selftests/bpf: More precise cpu_mitigations state detection")
Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
Acked-by: Viktor Malik <vmalik@redhat.com>
Link: https://lore.kernel.org/r/20250618093134.3078870-1-eddyz87@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2025-06-18 06:52:07 -07:00

144 lines
2.7 KiB
C

// SPDX-License-Identifier: GPL-2.0-only
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/utsname.h>
#include <unistd.h>
#include <fcntl.h>
#include <zlib.h>
#include "unpriv_helpers.h"
static gzFile open_config(void)
{
struct utsname uts;
char buf[PATH_MAX];
gzFile config;
if (uname(&uts)) {
perror("uname");
goto config_gz;
}
snprintf(buf, sizeof(buf), "/boot/config-%s", uts.release);
config = gzopen(buf, "rb");
if (config)
return config;
fprintf(stderr, "gzopen %s: %s\n", buf, strerror(errno));
config_gz:
config = gzopen("/proc/config.gz", "rb");
if (!config)
perror("gzopen /proc/config.gz");
return config;
}
static int config_contains(const char *pat)
{
const char *msg;
char buf[1024];
gzFile config;
int n, err;
config = open_config();
if (!config)
return -1;
for (;;) {
if (!gzgets(config, buf, sizeof(buf))) {
msg = gzerror(config, &err);
if (err == Z_ERRNO)
perror("gzgets /proc/config.gz");
else if (err != Z_OK)
fprintf(stderr, "gzgets /proc/config.gz: %s", msg);
gzclose(config);
return -1;
}
n = strlen(buf);
if (buf[n - 1] == '\n')
buf[n - 1] = 0;
if (strcmp(buf, pat) == 0) {
gzclose(config);
return 1;
}
}
gzclose(config);
return 0;
}
static bool cmdline_contains(const char *pat)
{
char cmdline[4096], *c;
int fd, ret = false;
fd = open("/proc/cmdline", O_RDONLY);
if (fd < 0) {
perror("open /proc/cmdline");
return false;
}
if (read(fd, cmdline, sizeof(cmdline) - 1) < 0) {
perror("read /proc/cmdline");
goto out;
}
cmdline[sizeof(cmdline) - 1] = '\0';
for (c = strtok(cmdline, " \n"); c; c = strtok(NULL, " \n")) {
if (strncmp(c, pat, strlen(c)))
continue;
ret = true;
break;
}
out:
close(fd);
return ret;
}
static int get_mitigations_off(void)
{
int enabled_in_config;
if (cmdline_contains("mitigations=off"))
return 1;
enabled_in_config = config_contains("CONFIG_CPU_MITIGATIONS=y");
if (enabled_in_config < 0)
return -1;
return !enabled_in_config;
}
bool get_unpriv_disabled(void)
{
int mitigations_off;
bool disabled;
char buf[2];
FILE *fd;
fd = fopen("/proc/sys/" UNPRIV_SYSCTL, "r");
if (fd) {
disabled = (fgets(buf, 2, fd) == buf && atoi(buf));
fclose(fd);
} else {
perror("fopen /proc/sys/" UNPRIV_SYSCTL);
disabled = true;
}
if (disabled)
return true;
/*
* Some unpriv tests rely on spectre mitigations being on.
* If mitigations are off or status can't be determined
* assume that unpriv tests are disabled.
*/
mitigations_off = get_mitigations_off();
if (mitigations_off < 0) {
fprintf(stderr,
"Can't determine if mitigations are enabled, disabling unpriv tests.");
return true;
}
return mitigations_off;
}