Re: [PATCH sched_ext/for-7.4-fixes] sched_ext: Fix kernel OOB memory read in scx_bpf_x_bstr kfuncs
From: Masoud Aghasi
Date: Sun Nov 29 2026 - 04:54:42 EST
On 29/11/2026 09:35, Masoud Aghasi wrote:
> For scx_bpf_exit_bstr(), scx_bpf_error_bstr(), scx_bpf_dump_bstr()
> and scx_bpf_sub_kill_bstr() kfuncs, fmt parameter is defined as a
> dynamic string pointer which could be of any length. This causes a
> kernel out-of-bound memory read inside the bpf_bprintf_prepare()
> for loop, when passing strings with lengths higher than INT_MAX.
>
> This patch fixes the bug by adding the __str suffix to the fmt
> parameter definition so the verifier could reject invalid strings.
>
I prepared the example bpf and user-space programs that
reproduces the bug.
bpf program:
```c
#include <scx/common.bpf.h>
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(max_entries, 3);
__type(key, __u32);
__type(value, char[1000000000]);
} strings SEC(".maps");
void BPF_STRUCT_OPS(bpf_sched_test_str_enqueue, struct task_struct *p,
u64 enq_flags) {
__u32 key = 0;
char *fmt = bpf_map_lookup_elem(&strings, &key);
if (!fmt) {
return;
}
unsigned long long params[] = {};
scx_bpf_exit_bstr(0, fmt, params, 0);
}
SEC(".struct_ops")
struct sched_ext_ops bpf_sched_test_str_ops = {
.enqueue = (void *)bpf_sched_test_str_enqueue,
.name = "sched_test_str",
};
char LICENSE[] SEC("license") = "GPL";
```
userspace program:
```c
#include <bpf/libbpf.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <bpf/bpf.h>
#include <bpf/btf.h>
#include <linux/btf.h>
#include <stdint.h>
static volatile sig_atomic_t exiting;
static void handle_signal(int signal) {
(void)signal;
exiting = 1;
}
int main(int argc, char **argv) {
const char *object_path = "bpf_sched_test_str.bpf.o";
struct bpf_object *obj = NULL;
struct bpf_map *ops_map = NULL;
struct bpf_link *link = NULL;
int err = 0;
if (argc > 1)
object_path = argv[1];
signal(SIGINT, handle_signal);
signal(SIGTERM, handle_signal);
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
printf("Opening BPF object: %s\n", object_path);
obj = bpf_object__open_file(object_path, NULL);
err = libbpf_get_error(obj);
if (err) {
obj = NULL;
fprintf(stderr, "Failed to open BPF object: %s\n", strerror(-err));
goto cleanup;
}
err = bpf_object__load(obj);
if (err) {
fprintf(stderr, "Failed to load BPF object: %s\n", strerror(-err));
goto cleanup;
}
struct bpf_map *str_map = bpf_object__find_map_by_name(obj, "strings");
if (!str_map) {
err = -ENOENT;
fprintf(stderr, "Failed to find strings map\n");
goto cleanup;
}
#define VALUE_SIZE 1000000000
char *value = malloc(VALUE_SIZE);
memset(value, 'A', VALUE_SIZE);
for (__u32 i = 0; i < 3; i++) {
__u32 key = i;
err = bpf_map__update_elem(str_map, &key, 4, value, VALUE_SIZE,
BPF_ANY);
if (err) {
fprintf(stderr, "bpf_map_update_elem failed\n");
goto cleanup;
}
}
ops_map = bpf_object__find_map_by_name(obj, "bpf_sched_test_str_ops");
if (!ops_map) {
err = -ENOENT;
fprintf(stderr, "Failed to find sched_ext struct_ops map\n");
goto cleanup;
}
link = bpf_map__attach_struct_ops(ops_map);
err = libbpf_get_error(link);
if (err) {
link = NULL;
fprintf(stderr, "Failed to attach scheduler: %s\n", strerror(-err));
goto cleanup;
}
err = 0;
printf("Press Ctrl+C to detach.\n");
while (!exiting)
pause();
printf("Detaching scheduler...\n");
cleanup:
if (link)
bpf_link__destroy(link);
if (obj)
bpf_object__close(obj);
return err ? 1 : 0;
}
```