From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796026469; b=fG6ORwueRSmS/BLbLcI5T7d3VhdpeE45X9XdJNfSkKexVJa+uP9MR7ciihV1elXdlFfD OgbCoe7+BctLL7NET8Jt6hOZBrSDfGjHk9SjcCOCbs82yjJcxJlSTbe2kDWFghfFHylAb RyxwTo0oK2KdAkHDPf/zGhtSrGydR3qwjAe2W1rlzKj28D4r5bSRwuHbWUN/Ct78nFaoz r8hTS0f8k7jQEsvvAjsYaXZKIqiTPZVjQAErlSbXqRHX+/qZnND7WUGimcEzJcD1aiDjJ K7uLfmVJEBIWnod2rhJFLa9UEZnHKCCDHxZl0yFuI6fdYI20HFHxbf1RwH3VNh2YBjg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796026469; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=Oc8bzqmvI+goxVwnbVr4N2UnlfmYPP68W6/1rCtzlVQ=; b=cUGkc2CYxy8VhbWlUW3xpKEUCIsVgSvXZpcEXGN/n9symopXUfLy7mwGGYSskgUmHeKH hosbUcYN4RenNVR62AQCqa59XQ8lhDZWdN/S3+vHbK6oViQJdj8nNsqQ2an+M6sB3zQHN VBqNbjK2f0GqoremnYSKHPZRDzs14gUNOXk0DW9F2pfYyf+8g19UJfkQ3FiFoBNSzoNtb heYI4JE9/zMPnFECbZTH8jj89THp9DHhBkKauAqdl2uRctOHYMyOHuwMql8dmQLMsv17f Abu1HZ2fY6HPV1/69l4gk8Cp+Kd8umzQfa8FwYqhv8APZ304FBRRU3nckr9lxuj/JXA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=X9Zptmav; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9E229606A31C for ; Mon, 30 Nov 2026 08:14:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=X9Zptmav Received: by smtp.kernel.org (Postfix) with ESMTPSA id 278241F0092A; Mon, 30 Nov 2026 08:14:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796026469; bh=Oc8bzqmvI+goxVwnbVr4N2UnlfmYPP68W6/1rCtzlVQ=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=X9ZptmavT+Yg7liVPpXkn3Oov4hTnBj4fi+LI0tyqw+PxVWmuGQ8F8fdEOAkOtvni GGILE48t5FJFbwFAeuP1zigaYB7ud4ErFOo1z3dr2t93TfMBxBrPi8Mcy1rEND/cjH 43sHGXWTt9EDQNxqjwa1Ti6LdrospLhxnqwqLmN//35hoD26C4vp4FeuMl891uTpeC XcMS1GTQmQctpoFJLWCqGmXw35csJB+Nsqb8k7aVOtZXRnWzdH0h9RrUJLMC9osRGq M53/q4Lw4L877axlj0RD4aNgZ4gi/jqx+7v6c+k5GJAjXlkjW4/2xVbhN7OjaSDogc t/Jq6Ro3+2txw== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH sched_ext/for-7.4-fixes] sched_ext: Fix unbounded format string reads in the bstr kfuncs Reply-To: sashiko-reviews@lists.linux.dev To: "Andrea Righi" Cc: sched-ext@lists.linux.dev In-Reply-To: <20261130075303.1261456-1-arighi@nvidia.com> References: <20261130075303.1261456-1-arighi@nvidia.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Mon, 30 Nov 2026 08:14:28 +0000 X-Mailing-List: sched-ext@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 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(c= onst struct bpf_prog_aux *aux) > =20 > __bpf_kfunc_end_defs(); > =20 > -__printf(5, 0) > -static s32 __bstr_format(struct scx_sched *sch, u64 *data_buf, char *lin= e_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 co= py in > + * @dst to prepare and format the string. > + */ > +static s32 scx_snapshot_fmt(char *dst, size_t size, const char *fmt) > +{ > + long ret =3D 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 =3D=3D 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 =3D { .get_bin_args =3D true }; > s32 ret; > =20 > + ret =3D 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=3D%p and data__sz=3D%u", (void *)data, da= ta__sz); > @@ -11303,18 +11326,17 @@ static s32 __bstr_format(struct scx_sched *sch,= u64 *data_buf, char *line_buf, > return ret; > } > =20 > - ret =3D bpf_bprintf_prepare(fmt, UINT_MAX, data_buf, data__sz / 8, > - &bprintf_data); > + ret =3D 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; > } > =20 > - ret =3D bstr_printf(line_buf, line_size, fmt, > - bprintf_data.bin_args); > + ret =3D 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__s= z); > + scx_error(sch, "(\"%s\", %p, %u) failed to format", fmt_buf, data, dat= a__sz); > return ret; > } > =20 [ ... ] > @@ -11441,11 +11463,11 @@ __bpf_kfunc void scx_bpf_dump_bstr(char *fmt, u= nsigned long long *data, > } > =20 > /* append the formatted string to the line buf */ > - ret =3D __bstr_format(sch, buf->data, buf->line + dd->cursor, > + ret =3D __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; > } > =20 [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() !=3D 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=3D"hello", data__sz=3D0] __bstr_format() scx_snapshot_fmt(buf->fmt, ..., "hello") bpf_bprintf_prepare(buf->fmt, ...) -> bprintf_data.bin_args =3D NULL cpu> scx_bpf_dump_bstr() [inner, fmt=3D"%s", data__sz=3D8] __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. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261130075303.1261= 456-1-arighi@nvidia.com?part=3D1