mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Masoud Aghasi <maghasi@disroot.org>
To: sched-ext@lists.linux.dev
Cc: tj@kernel.org, void@manifault.com, arighi@nvidia.com,
	changwoo@igalia.com, linux-kernel@vger.kernel.org
Subject: Re: [PATCH sched_ext/for-7.4-fixes] sched_ext: Fix kernel OOB memory read in scx_bpf_x_bstr kfuncs
Date: Sun, 29 Nov 2026 09:54:28 +0000	[thread overview]
Message-ID: <057537e6-9ae1-4918-9052-0589590892c1@disroot.org> (raw)
In-Reply-To: <20261129093555.3890002-1-maghasi@disroot.org>

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;
}
```

  reply	other threads:[~2026-11-29  9:54 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29  9:35 Masoud Aghasi
2026-11-29  9:54 ` Masoud Aghasi [this message]
2026-11-29 14:11 ` Andrea Righi
2026-11-29 15:33   ` Masoud Aghasi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=057537e6-9ae1-4918-9052-0589590892c1@disroot.org \
    --to=maghasi@disroot.org \
    --cc=arighi@nvidia.com \
    --cc=changwoo@igalia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sched-ext@lists.linux.dev \
    --cc=tj@kernel.org \
    --cc=void@manifault.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®