Sched_ext development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Andrea Righi" <arighi@nvidia.com>
Cc: sched-ext@lists.linux.dev
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 08:14:28 +0000	[thread overview]
Message-ID: <sashiko-outbox-240675@kernel.org> (raw)
In-Reply-To: <20261130075303.1261456-1-arighi@nvidia.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] sched_ext: same-CPU re-entrancy in scx_bpf_dump_bstr()
  corrupts buf->fmt
--

commit 327433557b482c491e440670458830800354c885
Author: Andrea Righi <arighi@nvidia.com>

sched_ext: Fix unbounded format string reads in the bstr kfuncs

This commit copies the BPF-supplied format string into a bounded kernel
buffer (scx_exit_info->fmt or scx_bstr_buf->fmt) before running
bpf_bprintf_prepare() and bstr_printf().

Link: https://lore.kernel.org/r/20261130054957.570822-1-maghasi@disroot.org

> diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
> index a9e36b5bb0386..06f1fbd93e961 100644
> --- a/kernel/sched/ext/ext.c
> +++ b/kernel/sched/ext/ext.c

[ ... ]

> @@ -11283,14 +11285,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;
> +}
> +
> +static s32 __bstr_format(struct scx_sched *sch, u64 *data_buf, char *fmt_buf,
> +			 char *line_buf, size_t line_size, const char *fmt,
> +			 unsigned long long *data, u32 data__sz)
>  {
>  	struct bpf_bprintf_data bprintf_data = { .get_bin_args = true };
>  	s32 ret;
>  
> +	ret = scx_snapshot_fmt(fmt_buf, SCX_BSTR_FMT_LEN, fmt);
> +	if (ret < 0) {
> +		scx_error(sch, "failed to read format string (%d)", ret);
> +		return ret;
> +	}
> +
>  	if (data__sz % 8 || data__sz > MAX_BPRINTF_VARARGS * 8 ||
>  	    (data__sz && !data)) {
>  		scx_error(sch, "invalid data=%p and data__sz=%u", (void *)data, data__sz);
> @@ -11303,18 +11326,17 @@ static s32 __bstr_format(struct scx_sched *sch, u64 *data_buf, char *line_buf,
>  		return ret;
>  	}
>  
> -	ret = bpf_bprintf_prepare(fmt, UINT_MAX, data_buf, data__sz / 8,
> -				  &bprintf_data);
> +	ret = bpf_bprintf_prepare(fmt_buf, SCX_BSTR_FMT_LEN, data_buf,
> +				  data__sz / 8, &bprintf_data);
>  	if (ret < 0) {
>  		scx_error(sch, "format preparation failed (%d)", ret);
>  		return ret;
>  	}
>  
> -	ret = bstr_printf(line_buf, line_size, fmt,
> -			  bprintf_data.bin_args);
> +	ret = bstr_printf(line_buf, line_size, fmt_buf, bprintf_data.bin_args);
>  	bpf_bprintf_cleanup(&bprintf_data);
>  	if (ret < 0) {
> -		scx_error(sch, "(\"%s\", %p, %u) failed to format", fmt, data, data__sz);
> +		scx_error(sch, "(\"%s\", %p, %u) failed to format", fmt_buf, data, data__sz);
>  		return ret;
>  	}
>  

[ ... ]

> @@ -11441,11 +11463,11 @@ __bpf_kfunc void scx_bpf_dump_bstr(char *fmt, unsigned long long *data,
>  	}
>  
>  	/* append the formatted string to the line buf */
> -	ret = __bstr_format(sch, buf->data, buf->line + dd->cursor,
> +	ret = __bstr_format(sch, buf->data, buf->fmt, buf->line + dd->cursor,
>  			    sizeof(buf->line) - dd->cursor, fmt, data, data__sz);
>  	if (ret < 0) {
>  		scx_dump_line(dd->s, "%s[!] (\"%s\", %p, %u) failed to format (%d)",
> -			      dd->prefix, fmt, data, data__sz, ret);
> +			      dd->prefix, buf->fmt, data, data__sz, ret);
>  		return;
>  	}
>  

[Severity: Medium]
Can same-CPU re-entrancy into scx_bpf_dump_bstr() overwrite buf->fmt
between bpf_bprintf_prepare() and bstr_printf()?

scx_bpf_dump_bstr() is registered in scx_kfunc_ids_any and is not excluded
by scx_kfunc_ids_no_tracing, so a BPF_PROG_TYPE_TRACING program can invoke
it.  Because scx_bpf_dump_bstr() only checks raw_smp_processor_id() !=
dd->cpu, a tracing program firing on dd->cpu during a dump (for example on
bpf_bprintf_prepare() or from an NMI) passes that check and shares
scx_dump_data.buf.fmt:

scx_bpf_dump_bstr() [outer, fmt="hello", data__sz=0]
  __bstr_format()
    scx_snapshot_fmt(buf->fmt, ..., "hello")
    bpf_bprintf_prepare(buf->fmt, ...)
      -> bprintf_data.bin_args = NULL
    <fentry/NMI tracing prog on dd->cpu>
      scx_bpf_dump_bstr() [inner, fmt="%s", data__sz=8]
        __bstr_format()
          scx_snapshot_fmt(buf->fmt, ..., "%s")  <-- overwrites buf->fmt
    bstr_printf(..., buf->fmt, NULL)  <-- NULL pointer dereference

When the outer call resumes in bstr_printf(), it parses the inner call's
format string in buf->fmt against the outer bprintf_data.bin_args, causing
a NULL pointer dereference (when the outer data__sz is 0) or reading
unvalidated arguments from bin_args.

Adding scx_bpf_dump_bstr to scx_kfunc_ids_no_tracing (or guarding dd with
a busy flag on dd->cpu) would prevent this re-entrancy.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130075303.1261456-1-arighi@nvidia.com?part=1

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

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  7:53 [PATCH sched_ext/for-7.4-fixes] sched_ext: Fix unbounded format string reads in the bstr kfuncs Andrea Righi
2026-11-30  8:14 ` sashiko-bot [this message]
2026-11-30 10:49 ` Masoud Aghasi
2026-11-30 11:03   ` Andrea Righi
2026-11-30 15:36 ` Tejun Heo
2026-11-30 16:28   ` Andrea Righi

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=sashiko-outbox-240675@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=arighi@nvidia.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=sched-ext@lists.linux.dev \
    /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