From: Eduard Zingerman <eddyz87@gmail.com>
To: Yonghong Song <yonghong.song@linux.dev>,
bpf@vger.kernel.org, ast@kernel.org
Cc: andrii@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev,
kernel-team@fb.com, zenczykowski@gmail.com
Subject: Re: [PATCH bpf-next 2/3] bpf: infer packet range for 'if pkt ==/!= pkt_end' comparisons
Date: Wed, 10 Jan 2024 20:23:35 +0200 [thread overview]
Message-ID: <8e8fa80e093108b2de5956317cbc65b6612a3890.camel@gmail.com> (raw)
In-Reply-To: <0055d2a9f2fdbbcb524252e103440c387d3b5f3d.camel@gmail.com>
On Wed, 2024-01-10 at 03:07 +0200, Eduard Zingerman wrote:
> On Tue, 2024-01-09 at 09:26 -0800, Yonghong Song wrote:
> [...]
> >
> > What will happen if there are multiple BPF_JEQ/BPF_JNE? I made a change to one of tests
> > in patch 3:
> >
> > +SEC("tc")
> > +__success __log_level(2)
> > +__msg("if r3 != r2 goto pc+3 ; R2_w=pkt_end() R3_w=pkt(off=8,r=0xffffffffffffffff)")
> > +__naked void data_plus_const_neq_pkt_end(void)
> > +{
> > + asm volatile (" \
> > + r9 = r1; \
> > + r1 = *(u32*)(r9 + %[__sk_buff_data]); \
> > + r2 = *(u32*)(r9 + %[__sk_buff_data_end]); \
> > + r3 = r1; \
> > + r3 += 8; \
> > + if r3 != r2 goto 1f; \
> > + r3 += 8; \
> > + if r3 != r2 goto 1f; \
> > + r1 = *(u64 *)(r1 + 0); \
> > +1: \
> > + r0 = 0; \
> > + exit; \
> > +" :
> > + : __imm_const(__sk_buff_data, offsetof(struct __sk_buff, data)),
> > + __imm_const(__sk_buff_data_end, offsetof(struct __sk_buff, data_end))
> > + : __clobber_all);
> > +}
> >
> >
> > The verifier output:
> > func#0 @0
> > Global function data_plus_const_neq_pkt_end() doesn't return scalar. Only those are supported.
> > 0: R1=ctx() R10=fp0
> > ; asm volatile (" \
> > 0: (bf) r9 = r1 ; R1=ctx() R9_w=ctx()
> > 1: (61) r1 = *(u32 *)(r9 +76) ; R1_w=pkt(r=0) R9_w=ctx()
> > 2: (61) r2 = *(u32 *)(r9 +80) ; R2_w=pkt_end() R9_w=ctx()
> > 3: (bf) r3 = r1 ; R1_w=pkt(r=0) R3_w=pkt(r=0)
> > 4: (07) r3 += 8 ; R3_w=pkt(off=8,r=0)
> > 5: (5d) if r3 != r2 goto pc+3 ; R2_w=pkt_end() R3_w=pkt(off=8,r=0xffffffffffffffff)
> > 6: (07) r3 += 8 ; R3_w=pkt(off=16,r=0xffffffffffffffff)
> > 7: (5d) if r3 != r2 goto pc+1 ; R2_w=pkt_end() R3_w=pkt(off=16,r=0xffffffffffffffff)
> > 8: (79) r1 = *(u64 *)(r1 +0) ; R1=scalar()
> > 9: (b7) r0 = 0 ; R0_w=0
> > 10: (95) exit
> >
> > from 7 to 9: safe
> >
> > from 5 to 9: safe
> > processed 13 insns (limit 1000000) max_states_per_insn 0 total_states 1 peak_states 1 mark_read 0
> >
> > insn 5, for this_branch (straight one), r3 range will be 8 and assuming pkt_end is 8.
> > insn 7, r3 range becomes 18 and then we assume pkt_end is 16.
> >
> > I guess we should handle this case. For branch 5 and 7, it cannot be that both will be true.
>
> This is an interesting case.
> reg->range is set to AT_PKT_END or BEYOND_PKT_END only in
> try_match_pkt_pointers() (in mark_pkt_end() call).
> And this range mark appears not to be reset by += operation
> (which might add a negative number as well).
> So, once r3 is marked AT_PKT_END it would remain so
> even after r3 += 8, which is obviously not true.
> Not sure what to do yet.
Here is another example which is currently not handled correctly,
even w/o my patch:
SEC("tc")
__success
__naked void pkt_vs_pkt_end_with_bound_change(void)
{
asm volatile (" \
r9 = r1; \
r0 = 0; \
r1 = *(u32*)(r9 + %[__sk_buff_data]); \
r2 = *(u32*)(r9 + %[__sk_buff_data_end]); \
r3 = r1; \
r3 += 8; \
if r3 <= r2 goto 1f; \
r3 -= 8; \
if r3 >= r2 goto 1f; \
r4 = *(u64 *)(r1 + 0); \
1: exit; \
" :
: __imm_const(__sk_buff_data, offsetof(struct __sk_buff, data)),
__imm_const(__sk_buff_data_end, offsetof(struct __sk_buff, data_end))
: __clobber_all);
}
Verifier log:
0: R1=ctx() R10=fp0
; asm volatile (" \
0: (bf) r9 = r1 ; R1=ctx() R9_w=ctx()
1: (b7) r0 = 0 ; R0_w=0
2: (61) r1 = *(u32 *)(r9 +76) ; R1_w=pkt(r=0) R9_w=ctx()
3: (61) r2 = *(u32 *)(r9 +80) ; R2_w=pkt_end() R9_w=ctx()
4: (bf) r3 = r1 ; R1_w=pkt(r=0) R3_w=pkt(r=0)
5: (07) r3 += 8 ; R3_w=pkt(off=8,r=0)
6: (bd) if r3 <= r2 goto pc+3 ; R2_w=pkt_end() R3_w=pkt(off=8,r=0xfffffffffffffffe)
7: (17) r3 -= 8 ; R3_w=pkt(r=0xfffffffffffffffe)
8: (3d) if r3 >= r2 goto pc+1 ; R2_w=pkt_end() R3_w=pkt(r=0xfffffffffffffffe)
10: (95) exit
from 6 to 10: safe
processed 11 insns (limit 1000000) max_states_per_insn 0 total_states 1 peak_states 1 mark_read 1
At (6) for this_branch r3 is marked BEYOND_PKT_END,
packet range is known to be 8;
at (7) it is changed to point back to start of the packet;
at (8) is_pkt_ptr_branch_taken() incorrectly predicts that
r3 >= r2 (r3 - packet start, r2 - packet end).
next prev parent reply other threads:[~2024-01-10 18:23 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-08 13:27 [PATCH bpf-next 0/3] infer packet range for 'if pkt ==/!= pkt_end' instructions Eduard Zingerman
2024-01-08 13:28 ` [PATCH bpf-next 1/3] bpf: simplify try_match_pkt_pointers() Eduard Zingerman
2024-01-09 0:40 ` Andrii Nakryiko
2024-01-09 0:43 ` Andrii Nakryiko
2024-01-09 0:52 ` Eduard Zingerman
2024-01-09 18:22 ` Andrii Nakryiko
2024-01-08 13:28 ` [PATCH bpf-next 2/3] bpf: infer packet range for 'if pkt ==/!= pkt_end' comparisons Eduard Zingerman
2024-01-08 13:49 ` Maciej Żenczykowski
2024-01-08 13:57 ` Eduard Zingerman
2024-01-09 0:45 ` Andrii Nakryiko
2024-01-09 0:57 ` Eduard Zingerman
2024-01-09 18:32 ` Andrii Nakryiko
2024-01-09 17:26 ` Yonghong Song
2024-01-10 1:07 ` Eduard Zingerman
2024-01-10 18:23 ` Eduard Zingerman [this message]
2024-01-08 13:28 ` [PATCH bpf-next 3/3] selftests/bpf: test packet range inference for 'if pkt ==/!= pkt_end' 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=8e8fa80e093108b2de5956317cbc65b6612a3890.camel@gmail.com \
--to=eddyz87@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=kernel-team@fb.com \
--cc=martin.lau@linux.dev \
--cc=yonghong.song@linux.dev \
--cc=zenczykowski@gmail.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