From: Eduard Zingerman <eddyz87@gmail.com>
To: Yiyang Chen <chenyy23@mails.tsinghua.edu.cn>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>
Cc: John Fastabend <john.fastabend@gmail.com>,
Martin KaFai Lau <martin.lau@linux.dev>,
Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
Jiri Olsa <jolsa@kernel.org>, Shuah Khan <shuah@kernel.org>,
Emil Tsalapatis <emil@etsalapatis.com>,
bpf@vger.kernel.org, linux-kselftest@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH bpf-next 1/2] bpf: Preserve stack frame number for commuted arithmetic
Date: Mon, 20 Jul 2026 11:47:09 -0700 [thread overview]
Message-ID: <a469d5344126d38b5151b9bd044ce83320875517.camel@gmail.com> (raw)
In-Reply-To: <fee8d44c3c792e29b8898951a68c8313d9d2da53.1784563939.git.chenyy23@mails.tsinghua.edu.cn>
On Mon, 2026-07-20 at 16:37 +0000, Yiyang Chen wrote:
> When scalar += pointer is handled in adjust_ptr_min_max_vals(), the
> destination register inherits the pointer type and id from the source
> pointer. For PTR_TO_STACK, the inherited pointer state also has to carry
> the stack frame number.
>
> Without the frame number copy, a stack pointer derived inside a callee
> through scalar += fp can be recorded as pointing to frame 0. Stack reads
> and writes through that register can then update or consult the caller
> frame while the actual instruction uses the callee frame.
>
> Copy the frame number when PTR_TO_STACK state is inherited by the
> commuted arithmetic form.
Could you please infer a "Fixes" tag?
> Signed-off-by: Yiyang Chen <chenyy23@mails.tsinghua.edu.cn>
> ---
> kernel/bpf/verifier.c | 7 +++++--
> 1 file changed, 5 insertions(+), 2 deletions(-)
>
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index 52be0a118cce0..58017141d52b7 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -13796,11 +13796,14 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
> return -EACCES;
> }
>
> - /* In case of 'scalar += pointer', dst_reg inherits pointer type and id.
> - * The id may be overwritten later if we create a new variable offset.
> + /* In case of 'scalar += pointer', dst_reg inherits pointer type, id,
> + * and for stack pointers also the frame number. The id may be overwritten
> + * later if we create a new variable offset.
> */
> dst_reg->type = ptr_reg->type;
> dst_reg->id = ptr_reg->id;
> + if (base_type(ptr_reg->type) == PTR_TO_STACK)
> + dst_reg->frameno = ptr_reg->frameno;
This patch fixes a real issue and this a surprisingly buggy piece of
code. Looking at other fields defined in bpf_reg_state, it appears
that several additional modifications are necessary:
- dst_reg->delta = 0
- dst_reg->parent_id = ptr_reg->parent_id
Given such an error prone nature, I think it would be better to:
- stash the value of the off_reg in a temporary variable and adjust
off_reg pointer accordingly (see bpf_verifier_env for a collection
of similar temporaries).
- do *dst_reg = *ptr_reg instead of the fixup.
Wdyt?
>
> if (!check_reg_sane_offset_scalar(env, off_reg, ptr_reg->type) ||
> !check_reg_sane_offset_ptr(env, ptr_reg, ptr_reg->type))
next prev parent reply other threads:[~2026-07-20 18:47 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-20 16:37 [PATCH bpf-next 0/2] bpf: Preserve stack frame number for commuted stack arithmetic Yiyang Chen
2026-07-20 16:37 ` [PATCH bpf-next 1/2] bpf: Preserve stack frame number for commuted arithmetic Yiyang Chen
2026-07-20 18:47 ` Eduard Zingerman [this message]
2026-07-21 7:51 ` Yiyang Chen
2026-07-20 16:37 ` [PATCH bpf-next 2/2] selftests/bpf: Cover stack frame number after scalar plus fp Yiyang Chen
2026-07-20 17:18 ` Eduard Zingerman
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=a469d5344126d38b5151b9bd044ce83320875517.camel@gmail.com \
--to=eddyz87@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=chenyy23@mails.tsinghua.edu.cn \
--cc=daniel@iogearbox.net \
--cc=emil@etsalapatis.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=yonghong.song@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
all inboxes | Powered by JetHome®