Re: [PATCH sched_ext/for-7.4-fixes v2] sched_ext: Fix kernel OOB memory read in scx_bpf_x_bstr kfuncs
From: Andrea Righi
Date: Mon Nov 30 2026 - 02:58:13 EST
On Mon, Nov 30, 2026 at 08:25:13AM +0100, Andrea Righi wrote:
> Hi Masoud,
>
> On Mon, Nov 30, 2026 at 05:49:57AM +0000, Masoud Aghasi wrote:
> > The format string arguments of the sched_ext BPF kfuncs ending in
> > _bstr() are passed to bpf_bprintf_prepare(), which searches for
> > a terminating NUL using strnchr().
> >
> > Previously, the verifier only checked that these pointers were
> > accessible for a single byte. It did not guarantee that the format
> > strings were NUL-terminated. Consequently, an unterminated format
> > string could cause an out-of-bounds read during the initial
> > strnchr() call.
> >
> > Additionally, format strings larger than INT_MAX could cause
> > signed integer overflow in the subsequent formatting loop, which
> > uses an int index. This could result in out-of-bounds accesses
> > regardless of whether the format string was NUL-terminated.
> >
> > Annotate the format string arguments with __str so the verifier
> > validates that they point to valid NUL-terminated strings.
> >
> > Also, limit the maximum format string length to INT_MAX to
> > prevent signed integer overflow in the formatting loop.
> >
> > Fixes: f0e1a0643a59 ("sched_ext: Implement BPF extensible scheduler class")
> > Signed-off-by: Masoud Aghasi <maghasi@xxxxxxxxxxx>
>
> There's one issue I didn't consider in my previous review, this breaks
> compatibility with existing BPF schedulers in both directions (see
> below).
This should fix the issue maintaining backward and forward compatibility
with the BPF schedulers:
https://lore.kernel.org/r/20261130075303.1261456-1-arighi@xxxxxxxxxx
Masoud, can you take a look and see if it makes sense?
Thanks!
-Andrea