mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andrea Righi <arighi@nvidia.com>
To: Masoud Aghasi <maghasi@disroot.org>
Cc: Tejun Heo <tj@kernel.org>, David Vernet <void@manifault.com>,
	Changwoo Min <changwoo@igalia.com>,
	sched-ext@lists.linux.dev, linux-kernel@vger.kernel.org
Subject: Re: [PATCH sched_ext/for-7.4-fixes] sched_ext: Fix unbounded format string reads in the bstr kfuncs
Date: Mon, 30 Nov 2026 12:03:29 +0100	[thread overview]
Message-ID: <aw1YAdyZNHuvYW1m@gpd4> (raw)
In-Reply-To: <ee0ac918-ff18-486b-91de-2b8770cbe4dd@disroot.org>

Hi Masoud,

On Mon, Nov 30, 2026 at 10:49:04AM +0000, Masoud Aghasi wrote:
> Hi Andrea,
> 
> On 30/11/2026 07:53, Andrea Righi wrote:
> > The format string arguments of the sched_ext bstr kfuncs
> > (scx_bpf_exit_bstr(), scx_bpf_error_bstr(), scx_bpf_dump_bstr() and
> > scx_bpf_sub_kill_bstr()) are passed to bpf_bprintf_prepare() with a
> > UINT_MAX length. The verifier only checks that the pointer is readable
> > for a single byte, so an unterminated format makes strnchr() scan past
> > the end of the BPF object, possibly into unmapped memory. If the scan
> > covers more than INT_MAX bytes before finding a NUL, it can also
> > overflow the int index of the parsing loop.
> > 
> > Moreover, the format usually lives in a writable .data map, so it can be
> > modified by the BPF program between bpf_bprintf_prepare() and
> > bstr_printf(), making the latter interpret arguments that have not been
> > validated and prepared.
> > 
> > Fix this by taking a snapshot of the format into a private kernel buffer
> > using a bounded, fault-safe read and using only the snapshot for both
> > preparation and formatting. Formats of SCX_BSTR_FMT_LEN (1024) bytes or
> > more, including the NUL, are rejected with -E2BIG, which doesn't affect
> > any realistic format, as messages are already limited to
> > SCX_EXIT_MSG_LEN.
> > 
> > The snapshot is too large for the stack (these kfuncs can run in NMI),
> > so store it next to the existing output buffers, which already have a
> > single user at any time: scx_exit_info (per scheduler, written only by
> > the winner of scx_claim_exit()) for the exit path, and scx_bstr_buf
> > (serialized by scx_dump_lock) for the dump path.
> > 
> > Note that the bounded read may still access memory beyond the BPF object
> > up to SCX_BSTR_FMT_LEN bytes. This is not a concern, since loading a
> > struct_ops program requires CAP_PERFMON, which already allows reading
> > arbitrary kernel memory via bpf_probe_read_kernel_str().
> > 
> > The kfunc prototypes and the BPF-side wrappers are unchanged, so
> > existing BPF schedulers keep working on fixed kernels and newly built
> > ones keep working on older kernels.
> > 
> > Fixes: f0e1a0643a59 ("sched_ext: Implement BPF extensible scheduler class")
> > Cc: stable@vger.kernel.org # v6.12+
> > Reported-by: Masoud Aghasi <maghasi@disroot.org>
> > Link: https://lore.kernel.org/r/20261130054957.570822-1-maghasi@disroot.org
> > Signed-off-by: Andrea Righi <arighi@nvidia.com>
> > ---
> >  kernel/sched/ext/ext.c      | 50 ++++++++++++++++++++++++++-----------
> >  kernel/sched/ext/internal.h |  4 +++
> >  kernel/sched/ext/types.h    |  1 +
> >  3 files changed, 41 insertions(+), 14 deletions(-)
> > 
> > diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
> > index a4a93ed97f7b6..e06bd464dbd28 100644
> > --- a/kernel/sched/ext/ext.c
> > +++ b/kernel/sched/ext/ext.c
> > @@ -7389,6 +7389,7 @@ void scx_bypass(struct scx_sched *sch, bool bypass)
> >  static void free_exit_info(struct scx_exit_info *ei)
> >  {
> >  	kvfree(ei->dump);
> > +	kfree(ei->fmt);
> >  	kfree(ei->msg);
> >  	kfree(ei->bt);
> >  	kfree(ei);
> > @@ -7405,9 +7406,10 @@ static struct scx_exit_info *alloc_exit_info(size_t exit_dump_len)
> >  	ei->exit_cpu = -1;
> >  	ei->bt = kzalloc_objs(ei->bt[0], SCX_EXIT_BT_LEN);
> >  	ei->msg = kzalloc(SCX_EXIT_MSG_LEN, GFP_KERNEL);
> > +	ei->fmt = kzalloc(SCX_BSTR_FMT_LEN, GFP_KERNEL);
> >  	ei->dump = kvzalloc(exit_dump_len, GFP_KERNEL);
> >  
> > -	if (!ei->bt || !ei->msg || !ei->dump) {
> > +	if (!ei->bt || !ei->msg || !ei->fmt || !ei->dump) {
> >  		free_exit_info(ei);
> >  		return NULL;
> >  	}
> > @@ -11355,14 +11357,35 @@ __bpf_kfunc void scx_bpf_reenqueue_local___v2(const struct bpf_prog_aux *aux)
> >  
> >  __bpf_kfunc_end_defs();
> >  
> > -__printf(5, 0)
> > -static s32 __bstr_format(struct scx_sched *sch, u64 *data_buf, char *line_buf,
> > -			 size_t line_size, char *fmt, unsigned long long *data,
> > -			 u32 data__sz)
> > +/*
> > + * @fmt may be unterminated and writable by BPF, use only the bounded copy in
> > + * @dst to prepare and format the string.
> > + */
> > +static s32 scx_snapshot_fmt(char *dst, size_t size, const char *fmt)
> > +{
> > +	long ret = strncpy_from_kernel_nofault(dst, fmt, size);
> > +
> > +	if (ret < 0)
> > +		return ret;
> > +	/* @size means truncated or NUL in the last byte, reject both */
> > +	if (ret == size)
> > +		return -E2BIG;
> > +	return 0;
> > +}
> > +
> 
> Thanks for the fix. I see 2 potential limitations with this approach:
> 1- as you mentioned, the bounded nofault read does not establish
>    the actual readable object boundary.

Right, the kernel doesn't know where the object containing the format
ends, so an unterminated format can make the copy read past it.

However, the formats generated by scx_bpf_exit/error/dump() are static
variables in .data, which libbpf creates as a BPF_F_MMAPABLE array map.
Such maps are backed by a dedicated, zeroed, page-aligned vmalloc area,
followed by a vmalloc guard page. So, if the format is not terminated,
the copy either hits the zero padding after the variables (finding a
NUL), or the guard page, which strncpy_from_kernel_nofault() handles by
returning -EFAULT. In both cases it never reads outside the map.

Only a format passed explicitly from other memory could read into a
neighbouring object, but the read is still bounded to SCX_BSTR_FMT_LEN,
can't fault, and the program has CAP_PERFMON, which already allows
reading arbitrary kernel memory via bpf_probe_read_kernel_str().

> 
> 2- couldn't the source be modified concurrently while the snapshot is
>    being copied? (e.g. through updating the writable array value
>    from userspace)

Yes, the snapshot isn't atomic, but it doesn't need to be: what matters
is that bpf_bprintf_prepare() and bstr_printf() see exactly the same
bytes. Once copied, the snapshot is private and always NUL-terminated,
so a concurrent writer can only produce a garbled message (or one
rejected by bpf_bprintf_prepare()), but never a string that is formatted
without being validated first. The data arguments already behave the
same way.

And the issue pointed out by Sashiko is also valid:
https://lore.kernel.org/r/sashiko-outbox-240675@kernel.org

However, calling scx_bpf_dump() from a tracing program doesn't really
make sense IMHO: it's only meaningful from ops.dump() and friends, and
called from any other CPU it just aborts the scheduler.

I'll send a separate follow-up patch that adds scx_bpf_dump_bstr() to
scx_kfunc_ids_no_tracing, rejecting such programs at load time.

Thanks for reviewing this!
-Andrea

      reply	other threads:[~2026-11-30 11:03 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  7:53 Andrea Righi
2026-11-30 10:49 ` Masoud Aghasi
2026-11-30 11:03   ` Andrea Righi [this message]

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=aw1YAdyZNHuvYW1m@gpd4 \
    --to=arighi@nvidia.com \
    --cc=changwoo@igalia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maghasi@disroot.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®