From: Yonghong Song <yonghong.song@linux.dev>
To: Eduard Zingerman <eddyz87@gmail.com>, bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
kernel-team@fb.com, Martin KaFai Lau <martin.lau@kernel.org>
Subject: Re: [PATCH bpf-next v3 1/2] bpf: Do not include stack ptr register in precision backtracking bookkeeping
Date: Wed, 21 May 2025 13:34:50 -0700 [thread overview]
Message-ID: <2c0fa9ee-f9dd-4cde-b4fb-6f28ebefc619@linux.dev> (raw)
In-Reply-To: <45e399c6-74ad-4e58-bfda-06b392d1d28d@gmail.com>
On 5/21/25 11:55 AM, Eduard Zingerman wrote:
> [...]
>
>> diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
>> index 78c97e12ea4e..e73a910e4ece 100644
>> --- a/include/linux/bpf_verifier.h
>> +++ b/include/linux/bpf_verifier.h
>> @@ -357,6 +357,10 @@ enum {
>> INSN_F_SPI_SHIFT = 3, /* shifted 3 bits to the left */
>> INSN_F_STACK_ACCESS = BIT(9), /* we need 10 bits total */
>> +
>> + INSN_F_DST_REG_STACK = BIT(10), /* dst_reg is PTR_TO_STACK */
>> + INSN_F_SRC_REG_STACK = BIT(11), /* src_reg is PTR_TO_STACK */
>
> INSN_F_STACK_ACCESS can be inferred from INSN_F_DST_REG_STACK
> and INSN_F_SRC_REG_STACK if insn_stack_access_flags() is adjusted
> to track these flags instead. So, can be one less flag/bit.
You are correct, we could have BIT(9) for both INSN_F_STACK_ACCESS and INSN_F_DST_REG_STACK,
and BIT(10) for INSN_F_SRC_REG_STACK. But it makes code a little bit
complicated. I am okay with this if Andrii also thinks it is
worthwhile to do this.
>
>> + /* total 12 bits are used now. */
>> };
>> static_assert(INSN_F_FRAMENO_MASK + 1 >= MAX_CALL_FRAMES);
>
> [...]
>
>> @@ -4402,6 +4418,8 @@ static int backtrack_insn(struct
>> bpf_verifier_env *env, int idx, int subseq_idx,
>> */
>> return 0;
>> } else if (BPF_SRC(insn->code) == BPF_X) {
>> + bool dreg_precise, sreg_precise;
>> +
>> if (!bt_is_reg_set(bt, dreg) && !bt_is_reg_set(bt, sreg))
>> return 0;
>> /* dreg <cond> sreg
>> @@ -4410,8 +4428,16 @@ static int backtrack_insn(struct
>> bpf_verifier_env *env, int idx, int subseq_idx,
>> * before it would be equally necessary to
>> * propagate it to dreg.
>> */
>> - bt_set_reg(bt, dreg);
>> - bt_set_reg(bt, sreg);
>> + if (!hist)
>> + return 0;
>> + dreg_precise = !insn_dreg_stack_ptr(hist->flags);
>> + sreg_precise = !insn_sreg_stack_ptr(hist->flags);
>> + if (!dreg_precise && !sreg_precise)
>> + return 0;
>> + if (dreg_precise)
>> + bt_set_reg(bt, dreg);
>> + if (sreg_precise)
>> + bt_set_reg(bt, sreg);
>
> This check can be done in a generic way at backtrack_insn() callsite:
> check which register is pointer to stack and remove it from set
> registers.
Looks like other cases in backtrack_insn() has been handled properly,
so it may still be worthwhile to put the code here.
>
>> } else if (BPF_SRC(insn->code) == BPF_K) {
>> /* dreg <cond> K
>> * Only dreg still needs precision before
>> @@ -16397,6 +16423,29 @@ static void sync_linked_regs(struct
>> bpf_verifier_state *vstate, struct bpf_reg_s
>> }
>> }
>> +static int push_cond_jmp_history(struct bpf_verifier_env *env,
>> struct bpf_verifier_state *state,
>> + struct bpf_reg_state *dst_reg, struct bpf_reg_state
>> *src_reg,
>> + u64 linked_regs)
>> +{
>> + bool dreg_stack_ptr, sreg_stack_ptr;
>> + int insn_flags;
>> +
>> + if (!src_reg) {
>> + if (linked_regs)
>> + return push_insn_history(env, state, 0, linked_regs);
>> + return 0;
>> + }
>
> Nit: this 'if' is not needed, src_reg is always set (it might point to
> a fake register,
> but in that case it is a scalar without id).
>
Here, there is a bug here. Thanks for pointing this out. I need to check
BPF_SRC(insn->code) != BPF_X instead of "!src_reg". Basically passing one
more parameter (e.g., faked_sreg) to decide whether src_reg is faked or not.
>
>> +
>> + dreg_stack_ptr = dst_reg->type == PTR_TO_STACK;
>> + sreg_stack_ptr = src_reg->type == PTR_TO_STACK;
>> +
>> + if (!dreg_stack_ptr && !sreg_stack_ptr && !linked_regs)
>> + return 0;
>> +
>> + insn_flags = insn_reg_access_flags(dreg_stack_ptr, sreg_stack_ptr);
>> + return push_insn_history(env, state, insn_flags, linked_regs);
>> +}
>> +
>
> [...]
>
next prev parent reply other threads:[~2025-05-21 20:34 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-21 17:04 [PATCH bpf-next v3 1/2] bpf: Do not include stack ptr register in precision backtracking bookkeeping Yonghong Song
2025-05-21 17:04 ` [PATCH bpf-next v3 2/2] selftests/bpf: Add tests with stack ptr register in conditional jmp Yonghong Song
2025-05-21 19:02 ` Eduard Zingerman
2025-05-21 20:57 ` Yonghong Song
2025-05-21 21:03 ` Eduard Zingerman
2025-05-21 18:55 ` [PATCH bpf-next v3 1/2] bpf: Do not include stack ptr register in precision backtracking bookkeeping Eduard Zingerman
2025-05-21 20:34 ` Yonghong Song [this message]
2025-05-21 20:58 ` Eduard Zingerman
2025-05-21 21:35 ` Yonghong Song
2025-05-21 21:59 ` Eduard Zingerman
2025-05-21 22:04 ` Eduard Zingerman
2025-05-21 22:38 ` Andrii Nakryiko
2025-05-21 22:49 ` Eduard Zingerman
2025-05-22 2:53 ` Yonghong Song
2025-05-22 20:05 ` Andrii Nakryiko
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=2c0fa9ee-f9dd-4cde-b4fb-6f28ebefc619@linux.dev \
--to=yonghong.song@linux.dev \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=kernel-team@fb.com \
--cc=martin.lau@kernel.org \
/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