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] sched_ext: Fix kernel OOB memory read in scx_bpf_x_bstr kfuncs
Date: Sun, 29 Nov 2026 15:11:36 +0100 [thread overview]
Message-ID: <awwymLdpQ105Xw5O@gpd4> (raw)
In-Reply-To: <20261129093555.3890002-1-maghasi@disroot.org>
Hi Masoud,
On Sun, Nov 29, 2026 at 09:35:55AM +0000, 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.
Actually I think the OOB doesn't require a string longer than INT_MAX,
the verifier only checks one byte of fmt, so an unterminated buffer can
also overrun in the initial strnchr(), before reaching the loop. We
should probably clarify this in the description.
And, as reported by Sashiko, ___fmt needs to be const, otherwise the
existing wrappers fail verification. Please also fix the kernel-doc
parameter names.
Thanks,
-Andrea
>
> This patch fixes the bug by adding the __str suffix to the fmt
> parameter definition so the verifier could reject invalid strings.
>
> Fixes: f0e1a0643a59 ("sched_ext: Implement BPF extensible scheduler class")
> Signed-off-by: Masoud Aghasi <maghasi@disroot.org>
> ---
> kernel/sched/ext/ext.c | 20 ++++++++++----------
> kernel/sched/ext/internal.h | 2 +-
> kernel/sched/ext/sub.c | 6 +++---
> tools/sched_ext/include/scx/common.bpf.h | 8 ++++----
> 4 files changed, 18 insertions(+), 18 deletions(-)
>
> diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
> index a4a93ed97f7b..e1e0e73b6c1b 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;
> @@ -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)
> {
> @@ -11451,7 +11451,7 @@ __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);
> }
>
> @@ -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,7 +11475,7 @@ __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);
> }
>
> @@ -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..0deef348c176 100644
> --- a/kernel/sched/ext/sub.c
> +++ b/kernel/sched/ext/sub.c
> @@ -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..648188ce6606 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
> --
> 2.47.3
>
next prev parent reply other threads:[~2026-11-29 14:11 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
2026-11-29 14:11 ` Andrea Righi [this message]
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=awwymLdpQ105Xw5O@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®