The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Andrea Righi <arighi@nvidia.com>
To: Masoud Aghasi <maghasi@disroot.org>
Cc: sched-ext@lists.linux.dev, tj@kernel.org, void@manifault.com,
	changwoo@igalia.com, linux-kernel@vger.kernel.org
Subject: Re: [PATCH sched_ext/for-7.4-fixes v2] sched_ext: Fix kernel OOB memory read in scx_bpf_x_bstr kfuncs
Date: Mon, 30 Nov 2026 08:25:13 +0100	[thread overview]
Message-ID: <aw0k2SsiRs5NRtAv@gpd4> (raw)
In-Reply-To: <20261130054957.570822-1-maghasi@disroot.org>

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@disroot.org>

There's one issue I didn't consider in my previous review, this breaks
compatibility with existing BPF schedulers in both directions (see
below).

> ---
> v2:
> - Make the format string declaration in
>   scx_bpf_bstr_preamble() const-qualified to satisfy the
>   verifier's read-only string requirement.
> 
> - Update kernel-doc parameter names to reflect the fmt__str
>   annotations.
> 
> - Clarify the description of the out-of-bounds read bug.
> 
> v1: https://lore.kernel.org/sched-ext/20261129093555.3890002-1-maghasi@disroot.org/
> 
>  kernel/sched/ext/ext.c                   | 26 ++++++++++++------------
>  kernel/sched/ext/internal.h              |  2 +-
>  kernel/sched/ext/sub.c                   |  8 ++++----
>  tools/sched_ext/include/scx/common.bpf.h | 10 ++++-----
>  4 files changed, 23 insertions(+), 23 deletions(-)
> 
> diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
> index a4a93ed97f7b..adf2ed869827 100644
> --- a/kernel/sched/ext/ext.c
> +++ b/kernel/sched/ext/ext.c
> @@ -11357,7 +11357,7 @@ __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,
> +			 size_t line_size, const char *fmt, unsigned long long *data,
>  			 u32 data__sz)
>  {
>  	struct bpf_bprintf_data bprintf_data = { .get_bin_args = true };
> @@ -11375,7 +11375,7 @@ 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,
> +	ret = bpf_bprintf_prepare(fmt, INT_MAX, data_buf, data__sz / 8,
>  				  &bprintf_data);
>  	if (ret < 0) {
>  		scx_error(sch, "format preparation failed (%d)", ret);
> @@ -11405,7 +11405,7 @@ static s32 __bstr_format(struct scx_sched *sch, u64 *data_buf, char *line_buf,
>   */
>  __printf(5, 0)
>  bool scx_exit_bstr(struct scx_sched *sch, enum scx_exit_kind kind,
> -		   s64 exit_code, struct scx_sched *fmt_blame, char *fmt,
> +		   s64 exit_code, struct scx_sched *fmt_blame, const char *fmt,
>  		   unsigned long long *data, u32 data__sz)
>  {
>  	struct scx_exit_info *ei = sch->exit_info;
> @@ -11432,7 +11432,7 @@ __bpf_kfunc_start_defs();
>  /**
>   * scx_bpf_exit_bstr - Gracefully exit the BPF scheduler.
>   * @exit_code: Exit value to pass to user space via struct scx_exit_info.
> - * @fmt: error message format string
> + * @fmt__str: exit message format string
>   * @data: format string parameters packaged using ___bpf_fill() macro
>   * @data__sz: @data len, must end in '__sz' for the verifier
>   * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs
> @@ -11441,7 +11441,7 @@ __bpf_kfunc_start_defs();
>   * disabling.
>   */
>  __printf(2, 0)
> -__bpf_kfunc void scx_bpf_exit_bstr(s64 exit_code, char *fmt,
> +__bpf_kfunc void scx_bpf_exit_bstr(s64 exit_code, const char *fmt__str,
>  				   unsigned long long *data, u32 data__sz,
>  				   const struct bpf_prog_aux *aux)

__str requires the format to be a NUL-terminated string in a read-only
map. Existing schedulers built with the current common.bpf.h define the
format as static char ___fmt[], which ends up in writable .data, so they
would all fail to load on a kernel with this patch applied.

>  {
> @@ -11451,13 +11451,13 @@ __bpf_kfunc void scx_bpf_exit_bstr(s64 exit_code, char *fmt,
>  
>  	sch = scx_prog_sched(aux);
>  	if (likely(sch))
> -		scx_exit_bstr(sch, SCX_EXIT_UNREG_BPF, exit_code, sch, fmt,
> +		scx_exit_bstr(sch, SCX_EXIT_UNREG_BPF, exit_code, sch, fmt__str,
>  			      data, data__sz);
>  }
>  
>  /**
>   * scx_bpf_error_bstr - Indicate fatal error
> - * @fmt: error message format string
> + * @fmt__str: error message format string
>   * @data: format string parameters packaged using ___bpf_fill() macro
>   * @data__sz: @data len, must end in '__sz' for the verifier
>   * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs
> @@ -11466,7 +11466,7 @@ __bpf_kfunc void scx_bpf_exit_bstr(s64 exit_code, char *fmt,
>   * disabling.
>   */
>  __printf(1, 0)
> -__bpf_kfunc void scx_bpf_error_bstr(char *fmt, unsigned long long *data,
> +__bpf_kfunc void scx_bpf_error_bstr(const char *fmt__str, unsigned long long *data,
>  				    u32 data__sz, const struct bpf_prog_aux *aux)
>  {
>  	struct scx_sched *sch;
> @@ -11475,13 +11475,13 @@ __bpf_kfunc void scx_bpf_error_bstr(char *fmt, unsigned long long *data,
>  
>  	sch = scx_prog_sched(aux);
>  	if (likely(sch))
> -		scx_exit_bstr(sch, SCX_EXIT_ERROR_BPF, 0, sch, fmt, data,
> +		scx_exit_bstr(sch, SCX_EXIT_ERROR_BPF, 0, sch, fmt__str, data,
>  			      data__sz);
>  }
>  
>  /**
>   * scx_bpf_dump_bstr - Generate extra debug dump specific to the BPF scheduler
> - * @fmt: format string
> + * @fmt__str: format string
>   * @data: format string parameters packaged using ___bpf_fill() macro
>   * @data__sz: @data len, must end in '__sz' for the verifier
>   * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs
> @@ -11493,7 +11493,7 @@ __bpf_kfunc void scx_bpf_error_bstr(char *fmt, unsigned long long *data,
>   * multiple calls. The last line is automatically terminated.
>   */
>  __printf(1, 0)
> -__bpf_kfunc void scx_bpf_dump_bstr(char *fmt, unsigned long long *data,
> +__bpf_kfunc void scx_bpf_dump_bstr(const char *fmt__str, unsigned long long *data,
>  				   u32 data__sz, const struct bpf_prog_aux *aux)
>  {
>  	struct scx_sched *sch;
> @@ -11514,10 +11514,10 @@ __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,
> -			    sizeof(buf->line) - dd->cursor, fmt, data, data__sz);
> +			    sizeof(buf->line) - dd->cursor, fmt__str, 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, fmt__str, data, data__sz, ret);
>  		return;
>  	}
>  
> diff --git a/kernel/sched/ext/internal.h b/kernel/sched/ext/internal.h
> index c77f62cd7bc9..bdc968b71f1d 100644
> --- a/kernel/sched/ext/internal.h
> +++ b/kernel/sched/ext/internal.h
> @@ -2349,7 +2349,7 @@ int scx_sched_sysfs_add(struct scx_sched *sch);
>  bool scx_is_descendant(struct scx_sched *sch, struct scx_sched *ancestor);
>  __printf(5, 0) bool scx_exit_bstr(struct scx_sched *sch, enum scx_exit_kind kind,
>  				  s64 exit_code, struct scx_sched *fmt_blame,
> -				  char *fmt, unsigned long long *data, u32 data__sz);
> +				  const char *fmt, unsigned long long *data, u32 data__sz);
>  
>  extern raw_spinlock_t scx_sched_lock;
>  extern struct mutex scx_enable_mutex;
> diff --git a/kernel/sched/ext/sub.c b/kernel/sched/ext/sub.c
> index 7c069a289766..d10f85a31789 100644
> --- a/kernel/sched/ext/sub.c
> +++ b/kernel/sched/ext/sub.c
> @@ -3162,7 +3162,7 @@ __bpf_kfunc s32 scx_bpf_sub_caps(u64 cgroup_id, u64 caps, struct scx_cmask *out_
>  /**
>   * scx_bpf_sub_kill_bstr - Kill a direct child sub-scheduler
>   * @cgroup_id: cgroup id of the direct child to kill
> - * @fmt: reason message format string
> + * @fmt__str: reason message format string
>   * @data: format string parameters packaged using ___bpf_fill() macro
>   * @data__sz: @data len, must end in '__sz' for the verifier
>   * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs
> @@ -3180,7 +3180,7 @@ __bpf_kfunc s32 scx_bpf_sub_caps(u64 cgroup_id, u64 caps, struct scx_cmask *out_
>   * Naming a sched that exists but is not a direct child aborts the parent.
>   */
>  __printf(2, 0)
> -__bpf_kfunc s32 scx_bpf_sub_kill_bstr(u64 cgroup_id, char *fmt,
> +__bpf_kfunc s32 scx_bpf_sub_kill_bstr(u64 cgroup_id, const char *fmt__str,
>  				      unsigned long long *data, u32 data__sz,
>  				      const struct bpf_prog_aux *aux)
>  {
> @@ -3207,7 +3207,7 @@ __bpf_kfunc s32 scx_bpf_sub_kill_bstr(u64 cgroup_id, char *fmt,
>  		return -EINVAL;
>  	}
>  
> -	scx_exit_bstr(child, SCX_EXIT_PARENT_KILL, 0, parent, fmt, data, data__sz);
> +	scx_exit_bstr(child, SCX_EXIT_PARENT_KILL, 0, parent, fmt__str, data, data__sz);
>  	return 0;
>  }
>  
> @@ -3237,7 +3237,7 @@ __bpf_kfunc s32 scx_bpf_sub_caps(u64 cgroup_id, u64 caps, struct scx_cmask *out_
>  	return -EOPNOTSUPP;
>  }
>  
> -__bpf_kfunc s32 scx_bpf_sub_kill_bstr(u64 cgroup_id, char *fmt,
> +__bpf_kfunc s32 scx_bpf_sub_kill_bstr(u64 cgroup_id, const char *fmt__str,
>  				      unsigned long long *data, u32 data__sz,
>  				      const struct bpf_prog_aux *aux)
>  {
> diff --git a/tools/sched_ext/include/scx/common.bpf.h b/tools/sched_ext/include/scx/common.bpf.h
> index 6194484d4d9a..dbddcc833174 100644
> --- a/tools/sched_ext/include/scx/common.bpf.h
> +++ b/tools/sched_ext/include/scx/common.bpf.h
> @@ -74,9 +74,9 @@ int bpf_iter_scx_dsq_new(struct bpf_iter_scx_dsq *it, u64 dsq_id, u64 flags) __k
>  struct task_struct *bpf_iter_scx_dsq_next(struct bpf_iter_scx_dsq *it) __ksym __weak;
>  void bpf_iter_scx_dsq_destroy(struct bpf_iter_scx_dsq *it) __ksym __weak;
>  bool scx_bpf_dsq_reenq_task(struct bpf_iter_scx_dsq *it, struct task_struct *p) __ksym __weak;
> -void scx_bpf_exit_bstr(s64 exit_code, char *fmt, unsigned long long *data, u32 data__sz) __ksym __weak;
> -void scx_bpf_error_bstr(char *fmt, unsigned long long *data, u32 data_len) __ksym;
> -void scx_bpf_dump_bstr(char *fmt, unsigned long long *data, u32 data_len) __ksym __weak;
> +void scx_bpf_exit_bstr(s64 exit_code, const char *fmt__str, unsigned long long *data, u32 data__sz) __ksym __weak;
> +void scx_bpf_error_bstr(const char *fmt__str, unsigned long long *data, u32 data_len) __ksym;
> +void scx_bpf_dump_bstr(const char *fmt__str, unsigned long long *data, u32 data_len) __ksym __weak;
>  u32 scx_bpf_cpuperf_cap(s32 cpu) __ksym __weak;
>  u32 scx_bpf_cpuperf_cur(s32 cpu) __ksym __weak;
>  void scx_bpf_cpuperf_set(s32 cpu, u32 perf) __ksym __weak;
> @@ -123,7 +123,7 @@ u32 scx_bpf_cidperf_cur(s32 cid) __ksym __weak;
>  s32 scx_bpf_sub_grant(u64 cgroup_id, u64 caps, const struct scx_cmask __arena *cmask__arena, struct scx_cmask __arena *denied_out__arena__nullable) __ksym __weak;
>  void scx_bpf_sub_revoke(u64 cgroup_id, u64 caps, const struct scx_cmask __arena *cmask__arena) __ksym __weak;
>  s32 scx_bpf_sub_caps(u64 cgroup_id, u64 caps, struct scx_cmask __arena *out__arena) __ksym __weak;
> -s32 scx_bpf_sub_kill_bstr(u64 cgroup_id, char *fmt, unsigned long long *data, u32 data__sz) __ksym __weak;
> +s32 scx_bpf_sub_kill_bstr(u64 cgroup_id, const char *fmt__str, unsigned long long *data, u32 data__sz) __ksym __weak;
>  
>  /*
>   * Use the following as @it__iter when calling scx_bpf_dsq_move[_vtime]() from
> @@ -146,7 +146,7 @@ void ___scx_bpf_bstr_format_checker(const char *fmt, ...) {}
>   * refer to the initialized list of inputs to the bstr kfunc.
>   */
>  #define scx_bpf_bstr_preamble(fmt, args...)					\
> -	static char ___fmt[] = fmt;						\
> +	static const char ___fmt[] = fmt;						\

And this breaks the other direction: schedulers rebuilt with the new
header place the format in .rodata, while older kernels expect a
writable char *fmt, so they would fail to load on kernels without this
patch.

>  	/*									\
>  	 * Note that __param[] must have at least one				\
>  	 * element to keep the verifier happy.					\
> -- 
> 2.47.3
> 
> 

I think there may be a simpler way to fix this that keeps the kfunc
prototypes and the headers unchanged, so we don't break the existent BPF
schedulers. Let me run a few tests...

Thanks,
-Andrea

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

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  5:49 [PATCH sched_ext/for-7.4-fixes v2] sched_ext: Fix kernel OOB memory read in scx_bpf_x_bstr kfuncs Masoud Aghasi
2026-11-30  7:25 ` Andrea Righi [this message]
2026-11-30  7:57   ` 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=aw0k2SsiRs5NRtAv@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