From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=178.21.23.139 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796017858; b=IfthfPoGz9yv5THO1o5TQ747rJnvbrzVKuEp4V8du6V9cnSO0uKusJAD8K7ypYBLMtXC a3Tv0jXffSCC0LPkaDWAfWDYMmq87ENbZznYfeFIRXKc+9TsqPho8958UYBABuuWuGYiV vS4QgeL1TV1juF4Cn5CFdkm/SkLJIPuumVP/mZhU52f6w5W7PDVO87Cvr48651vXQb4V9 RgWPowFGAMXn7zacEp9If9MuDlEQ1l7ahJMSAVQ4Cs66o76R7vVGUn2Tach2Tqp4dxw2t cCFo0C7eXNByk4TzupVg4ca0U9lLaRNdMBQRSwm02VGh3DR+nO8cM054gFHrR+TAIvw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796017858; h=DMARC-Filter:Received:X-Virus-Scanned:Received:DKIM-Signature:From: To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Transfer-Encoding; bh=whZmcPqGqXziFFhezrUIRZ/+Kau7Fb4JQIku2KWPRCQ=; b=AHe42lNCEu2iS3gcChIozJzYnKXSHLe+Stuc945L8x/yQNpcqEuqF6BpruK+bcoRNWRI kX0ZZyWkfnywswSMaLQlWBssYLqLIshRRMJLQOiCl9lyMquncIAnTsOUGeQvkQbLE8O1E /e3rllcyeMZkRCAHFWjQecZxtvOdzgYb1wbrdHvLrKbjSRVqqubHd6qa9Vzjp6w8z7a4K +i48fdN0dn2nUxkejfZmd0iTC1aVYPJXdLTwDfHV8/jAxFlViJuHRK66sIczR0ZffmVNw /ekYYag0qtWnd9OcTeWMi9HYUMEzPAmRJ3VgciHTGCf0Y43gjLbo6QEnL98uzS23zLA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=disroot.org; spf=pass smtp.mailfrom=disroot.org; dkim=pass header.d=disroot.org header.i=@disroot.org header.a=rsa-sha256 header.s=mail header.b=PuvYV9VP; arc=none smtp.remote-ip=178.21.23.139 Received: from layka.disroot.org (layka.disroot.org [178.21.23.139]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4C94D606A31C for ; Mon, 30 Nov 2026 05:50:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4C94D606A31C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=disroot.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=disroot.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=disroot.org header.i=@disroot.org header.a=rsa-sha256 header.s=mail header.b=PuvYV9VP Received: from mail01.layka.lan (localhost [127.0.0.1]) by disroot.org (Postfix) with ESMTP id 39826810E5; Mon, 30 Nov 2026 06:50:55 +0100 (CET) X-Virus-Scanned: SPAM Filter at disroot.org Received: from layka.disroot.org ([127.0.0.1]) by localhost (disroot.org [127.0.0.1]) (amavis, port 10024) with ESMTP id vrNwyDAg4fTM; Mon, 30 Nov 2026 06:50:54 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=disroot.org; s=mail; t=1796017854; bh=dD6vLNXPzUGcKCyGvi4pyUuEdZlFnXYsQYCBnERNEQQ=; h=From:To:Cc:Subject:Date; b=PuvYV9VPULLSt4/NNwtQJbLUn1Hu+iR0Z06SgUKghDCZNoOsS2LLUAKzko2Tka8i8 7UBqoyeUUOcNni/UUgUUSuOi++DEgkTBIUBGUIYjLIFR2JOnLTVXD2XrH9BHttd9YF zjzJFyD1s2zWNYwRsZeiulxqB5VfL+BKynJANP+mqXtkIyzJI0z19jQ5/Oezt9zy/k T33Owtyth918gMiiDVKb8JzINJNh4//rJhsG3Vcw3gbtebd2DOaSi8cBMuipvk/ske wuM6AsejoMOnikP1yQ56x5lbS+A9VIZO1cRNnHQra9cEO9zr6PtycV/+jLFNcTr9Bz 84KQi0ZuhfD1A== From: Masoud Aghasi To: sched-ext@lists.linux.dev Cc: tj@kernel.org, void@manifault.com, arighi@nvidia.com, changwoo@igalia.com, linux-kernel@vger.kernel.org, Masoud Aghasi Subject: [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 05:49:57 +0000 Message-ID: <20261130054957.570822-1-maghasi@disroot.org> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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) { @@ -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; \ /* \ * Note that __param[] must have at least one \ * element to keep the verifier happy. \ -- 2.47.3