Skip to contentSkip to conversation

bpf: Pointers beyond packet end.

PATCH Applied Alexei Starovoitov 5 participants 14 messages Nov 11, 2020 – Jan 20, 2021

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

v2 cover bpf: Pointers beyond packet end.

From: Alexei Starovoitov <[email protected]>

v1->v2:
- removed set-but-unused variable.
- added Jiri's Tested-by.

In some cases LLVM uses the knowledge that branch is taken to optimze the code which causes the verifier to reject valid programs.
Teach the verifier to recognize that
r1 = skb->data;
r1 += 10;
r2 = skb->data_end;
if (r1 > r2) {
here r1 points beyond packet_end and subsequent
if (r1 > r2) // always evaluates to "true".
}

Alexei Starovoitov (3):
bpf: Support for pointers beyond pkt_end. selftests/bpf: Add skb_pkt_end test selftests/bpf: Add asm tests for pkt vs pkt_end comparison.

 include/linux/bpf_verifier.h                  |   2 +-
 kernel/bpf/verifier.c                         | 129 +++++++++++++++---
 .../bpf/prog_tests/test_skb_pkt_end.c         |  41 ++++++
 .../testing/selftests/bpf/progs/skb_pkt_end.c |  54 ++++++++
 .../testing/selftests/bpf/verifier/ctx_skb.c  |  42 ++++++
 5 files changed, 245 insertions(+), 23 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/test_skb_pkt_end.c
 create mode 100644 tools/testing/selftests/bpf/progs/skb_pkt_end.c
Signature · 1 line
2.24.1
#1 lore raw

PATCH v2 1/3 bpf: Support for pointers beyond pkt_end.

From: Alexei Starovoitov <[email protected]>

This patch adds the verifier support to recognize inlined branch conditions. The LLVM knows that the branch evaluates to the same value, but the verifier couldn't track it. Hence causing valid programs to be rejected. The potential LLVM workaround: https://reviews.llvm.org/D87428
can have undesired side effects, since LLVM doesn't know that skb->data/data_end are being compared. LLVM has to introduce extra boolean variable and use inline_asm trick to force easier for the verifier assembly.

Instead teach the verifier to recognize that
r1 = skb->data;
r1 += 10;
r2 = skb->data_end;
if (r1 > r2) {
here r1 points beyond packet_end and
subsequent
if (r1 > r2) // always evaluates to "true".
}

---
 include/linux/bpf_verifier.h |   2 +-
 kernel/bpf/verifier.c        | 129 +++++++++++++++++++++++++++++------
 2 files changed, 108 insertions(+), 23 deletions(-)
include/linux/bpf_verifier.h +1 -1
index e83ef6f6bf43..306869d4743b 100644
@@ -45,7 +45,7 @@ struct bpf_reg_state {
 	enum bpf_reg_type type;
 	union {
 		/* valid when type == PTR_TO_PACKET */
-		u16 range;
+		int range;
 
 		/* valid when type == CONST_PTR_TO_MAP | PTR_TO_MAP_VALUE |
 		 *   PTR_TO_MAP_VALUE_OR_NULL
kernel/bpf/verifier.c +107 -22

225 lines not on this page · raw message

Signature · 1 line
2.24.1
reviewer Acked-by on v2 1/3
#2 lore raw

RE: [PATCH v2 bpf-next 1/3] bpf: Support for pointers beyond pkt_end.

Quoted 20 lines · Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

This patch adds the verifier support to recognize inlined branch conditions. The LLVM knows that the branch evaluates to the same value, but the verifier couldn't track it. Hence causing valid programs to be rejected. The potential LLVM workaround: https://reviews.llvm.org/D87428
can have undesired side effects, since LLVM doesn't know that skb->data/data_end are being compared. LLVM has to introduce extra boolean variable and use inline_asm trick to force easier for the verifier assembly.

Instead teach the verifier to recognize that
r1 = skb->data;
r1 += 10;
r2 = skb->data_end;
if (r1 > r2) {
here r1 points beyond packet_end and
subsequent
if (r1 > r2) // always evaluates to "true".
}

Quoted 7 lines
---
 include/linux/bpf_verifier.h |   2 +-
 kernel/bpf/verifier.c        | 129 +++++++++++++++++++++++++++++------
 2 files changed, 108 insertions(+), 23 deletions(-)

Thanks, we can remove another set of inline asm logic.

kernel/bpf/verifier.c static int check_cond_jmp_op(struct bpf_verifier_env *env, · line 7605

Quoted 8 lines
kernel/bpf/verifier.c +2 -1
 	if (pred >= 0) {
@@ -7517,7 +7601,8 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
 		 */
 		if (!__is_pointer_value(false, dst_reg))
 			err = mark_chain_precision(env, insn->dst_reg);
-		if (BPF_SRC(insn->code) == BPF_X && !err)
+		if (BPF_SRC(insn->code) == BPF_X && !err &&
+		    !__is_pointer_value(false, src_reg))

This could have been more specific with !type_is_pkt_pointer() correct? I think its fine as is though.

Quoted 6 lines
kernel/bpf/verifier.c +0 -0
 			err = mark_chain_precision(env, insn->src_reg);
 		if (err)
 			return err;
Signature · 1 line
2.24.1
maintainer
#3 lore raw

Re: [PATCH v2 bpf-next 1/3] bpf: Support for pointers beyond pkt_end.

Quoted 33 lines · John Fastabend
Quoted 20 lines · Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

This patch adds the verifier support to recognize inlined branch conditions. The LLVM knows that the branch evaluates to the same value, but the verifier couldn't track it. Hence causing valid programs to be rejected. The potential LLVM workaround: https://reviews.llvm.org/D87428
can have undesired side effects, since LLVM doesn't know that skb->data/data_end are being compared. LLVM has to introduce extra boolean variable and use inline_asm trick to force easier for the verifier assembly.

Instead teach the verifier to recognize that
r1 = skb->data;
r1 += 10;
r2 = skb->data_end;
if (r1 > r2) {
here r1 points beyond packet_end and
subsequent
if (r1 > r2) // always evaluates to "true".
}

Quoted 7 lines
---
  include/linux/bpf_verifier.h |   2 +-
  kernel/bpf/verifier.c        | 129 +++++++++++++++++++++++++++++------
  2 files changed, 108 insertions(+), 23 deletions(-)

Thanks, we can remove another set of inline asm logic.

Quoted 15 lines

kernel/bpf/verifier.c static int check_cond_jmp_op(struct bpf_verifier_env *env, · line 7605

Quoted 8 lines
  	if (pred >= 0) {
kernel/bpf/verifier.c +2 -1
@@ -7517,7 +7601,8 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
  		 */
  		if (!__is_pointer_value(false, dst_reg))
  			err = mark_chain_precision(env, insn->dst_reg);
-		if (BPF_SRC(insn->code) == BPF_X && !err)
+		if (BPF_SRC(insn->code) == BPF_X && !err &&
+		    !__is_pointer_value(false, src_reg))

This could have been more specific with !type_is_pkt_pointer() correct? I think its fine as is though.

Quoted 3 lines
kernel/bpf/verifier.c +0 -0
  			err = mark_chain_precision(env, insn->src_reg);
  		if (err)
  			return err;

Given the reg->range could now be negative, I wonder whether for the regsafe() pruning logic we should now better add a >=0 sanity check in there before we attempt to test on rold->range > rcur->range?

Thanks,
Daniel

#4 lore raw

Re: [PATCH v2 bpf-next 1/3] bpf: Support for pointers beyond pkt_end.

Quoted 45 lines · Daniel Borkmann
Quoted 44 lines · John Fastabend
Quoted 20 lines · Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

This patch adds the verifier support to recognize inlined branch conditions. The LLVM knows that the branch evaluates to the same value, but the verifier couldn't track it. Hence causing valid programs to be rejected. The potential LLVM workaround: https://reviews.llvm.org/D87428
can have undesired side effects, since LLVM doesn't know that skb->data/data_end are being compared. LLVM has to introduce extra boolean variable and use inline_asm trick to force easier for the verifier assembly.

Instead teach the verifier to recognize that
r1 = skb->data;
r1 += 10;
r2 = skb->data_end;
if (r1 > r2) {
here r1 points beyond packet_end and
subsequent
if (r1 > r2) // always evaluates to "true".
}

Quoted 7 lines
---
  include/linux/bpf_verifier.h |   2 +-
  kernel/bpf/verifier.c        | 129 +++++++++++++++++++++++++++++------
  2 files changed, 108 insertions(+), 23 deletions(-)

Thanks, we can remove another set of inline asm logic.

kernel/bpf/verifier.c static int check_cond_jmp_op(struct bpf_verifier_env *env, · line 7605

Quoted 8 lines
  	if (pred >= 0) {
kernel/bpf/verifier.c +2 -1
@@ -7517,7 +7601,8 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
  		 */
  		if (!__is_pointer_value(false, dst_reg))
  			err = mark_chain_precision(env, insn->dst_reg);
-		if (BPF_SRC(insn->code) == BPF_X && !err)
+		if (BPF_SRC(insn->code) == BPF_X && !err &&
+		    !__is_pointer_value(false, src_reg))

This could have been more specific with !type_is_pkt_pointer() correct? I think its fine as is though.

Quoted 7 lines
Quoted 3 lines

kernel/bpf/verifier.c static int check_cond_jmp_op(struct bpf_verifier_env *env, · line 7608

Quoted 3 lines
kernel/bpf/verifier.c +0 -0
  			err = mark_chain_precision(env, insn->src_reg);
  		if (err)
  			return err;

Given the reg->range could now be negative, I wonder whether for the regsafe() pruning logic we should now better add a >=0 sanity check in there before we attempt to test on rold->range > rcur->range?

I thought about it and specifically picked negative range value to keep regsafe() check as-is.
The check is this:

                if (rold->range > rcur->range)
                        return false;

rold is the one that was safe in the past.
If rold was positive and the current is negative we fail here which is ok. State pruning is conservative.

If rold was negative it means the previous state was safe even though that pointer was pointing beyond packet end. So it's ok for rcur->range to be anything. Whether rcur is positive or negative doesn't matter. Everything is still ok. If rold->range == -1 and rcur->range == -2 we fail here.
It's minor annoyance. State pruning is tiny bit more conservative than necessary.

So I think no extra checks in regsafe() are neeeded. Does it make sense?

maintainer
#5 lore raw

Re: [PATCH v2 bpf-next 1/3] bpf: Support for pointers beyond pkt_end.

Quoted 62 lines · Alexei Starovoitov
Quoted 44 lines · Daniel Borkmann
Quoted 43 lines · John Fastabend
Quoted 20 lines · Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

This patch adds the verifier support to recognize inlined branch conditions. The LLVM knows that the branch evaluates to the same value, but the verifier couldn't track it. Hence causing valid programs to be rejected. The potential LLVM workaround: https://reviews.llvm.org/D87428
can have undesired side effects, since LLVM doesn't know that skb->data/data_end are being compared. LLVM has to introduce extra boolean variable and use inline_asm trick to force easier for the verifier assembly.

Instead teach the verifier to recognize that
r1 = skb->data;
r1 += 10;
r2 = skb->data_end;
if (r1 > r2) {
here r1 points beyond packet_end and
subsequent
if (r1 > r2) // always evaluates to "true".
}

Quoted 6 lines
---
   include/linux/bpf_verifier.h |   2 +-
   kernel/bpf/verifier.c        | 129 +++++++++++++++++++++++++++++------
   2 files changed, 108 insertions(+), 23 deletions(-)

Thanks, we can remove another set of inline asm logic.

kernel/bpf/verifier.c static int check_cond_jmp_op(struct bpf_verifier_env *env, · line 7605

Quoted 8 lines
   	if (pred >= 0) {
kernel/bpf/verifier.c +2 -1
@@ -7517,7 +7601,8 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
   		 */
   		if (!__is_pointer_value(false, dst_reg))
   			err = mark_chain_precision(env, insn->dst_reg);
-		if (BPF_SRC(insn->code) == BPF_X && !err)
+		if (BPF_SRC(insn->code) == BPF_X && !err &&
+		    !__is_pointer_value(false, src_reg))

This could have been more specific with !type_is_pkt_pointer() correct? I think its fine as is though.

Quoted 7 lines
Quoted 3 lines

kernel/bpf/verifier.c static int check_cond_jmp_op(struct bpf_verifier_env *env, · line 7608

Quoted 3 lines
kernel/bpf/verifier.c +0 -0
   			err = mark_chain_precision(env, insn->src_reg);
   		if (err)
   			return err;

Given the reg->range could now be negative, I wonder whether for the regsafe() pruning logic we should now better add a >=0 sanity check in there before we attempt to test on rold->range > rcur->range?

I thought about it and specifically picked negative range value to keep regsafe() check as-is.
The check is this:

                 if (rold->range > rcur->range)
                         return false;

rold is the one that was safe in the past.
If rold was positive and the current is negative we fail here which is ok. State pruning is conservative.

Quoted 8 lines

If rold was negative it means the previous state was safe even though that pointer was pointing beyond packet end. So it's ok for rcur->range to be anything. Whether rcur is positive or negative doesn't matter. Everything is still ok. If rold->range == -1 and rcur->range == -2 we fail here.
It's minor annoyance. State pruning is tiny bit more conservative than necessary.

So I think no extra checks in regsafe() are neeeded. Does it make sense?

Yeah, same conclusion here. We still might want to add more BPF asm based tests on this in general, but either way logic lgtm, so applied, thanks.

#6 lore raw

Re: [PATCH v2 bpf-next 1/3] bpf: Support for pointers beyond pkt_end.

In reply to John Fastabend
Quoted 29 lines · John Fastabend
Quoted 20 lines · Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

This patch adds the verifier support to recognize inlined branch conditions. The LLVM knows that the branch evaluates to the same value, but the verifier couldn't track it. Hence causing valid programs to be rejected. The potential LLVM workaround: https://reviews.llvm.org/D87428
can have undesired side effects, since LLVM doesn't know that skb->data/data_end are being compared. LLVM has to introduce extra boolean variable and use inline_asm trick to force easier for the verifier assembly.

Instead teach the verifier to recognize that
r1 = skb->data;
r1 += 10;
r2 = skb->data_end;
if (r1 > r2) {
here r1 points beyond packet_end and
subsequent
if (r1 > r2) // always evaluates to "true".
}

Quoted 7 lines
---
 include/linux/bpf_verifier.h |   2 +-
 kernel/bpf/verifier.c        | 129 +++++++++++++++++++++++++++++------
 2 files changed, 108 insertions(+), 23 deletions(-)
Quoted 1 line

Thanks, we can remove another set of inline asm logic.

Awesome! Please contribute your C examples to selftests when possible.

Quoted 13 lines

kernel/bpf/verifier.c static int check_cond_jmp_op(struct bpf_verifier_env *env, · line 7605

Quoted 8 lines
kernel/bpf/verifier.c +2 -1
 	if (pred >= 0) {
@@ -7517,7 +7601,8 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
 		 */
 		if (!__is_pointer_value(false, dst_reg))
 			err = mark_chain_precision(env, insn->dst_reg);
-		if (BPF_SRC(insn->code) == BPF_X && !err)
+		if (BPF_SRC(insn->code) == BPF_X && !err &&
+		    !__is_pointer_value(false, src_reg))

This could have been more specific with !type_is_pkt_pointer() correct? I think its fine as is though.

I actually meant to use __is_pointer_value() here for two reasons:
1. to match dst_reg check just few lines above.
2. mark_chain_precision() is for scalars only. If in the future is_*_branch_taken() will support other kinds of pointers the more precise !type_is_pkt_pointer() check would need to be modified. That would be unnecessary code churn.

Thanks for the review!

#7 lore raw

Re: [PATCH v2 bpf-next 1/3] bpf: Support for pointers beyond pkt_end.

Quoted 30 lines · Alexei Starovoitov
Quoted 29 lines · John Fastabend
Quoted 27 lines · Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

This patch adds the verifier support to recognize inlined branch conditions. The LLVM knows that the branch evaluates to the same value, but the verifier couldn't track it. Hence causing valid programs to be rejected. The potential LLVM workaround: https://reviews.llvm.org/D87428
can have undesired side effects, since LLVM doesn't know that skb->data/data_end are being compared. LLVM has to introduce extra boolean variable and use inline_asm trick to force easier for the verifier assembly.

Instead teach the verifier to recognize that
r1 = skb->data;
r1 += 10;
r2 = skb->data_end;
if (r1 > r2) {
here r1 points beyond packet_end and
subsequent
if (r1 > r2) // always evaluates to "true".
}

---
 include/linux/bpf_verifier.h |   2 +-
 kernel/bpf/verifier.c        | 129 +++++++++++++++++++++++++++++------
 2 files changed, 108 insertions(+), 23 deletions(-)
Quoted 3 lines
Quoted 1 line

Thanks, we can remove another set of inline asm logic.

Awesome! Please contribute your C examples to selftests when possible.

Sure will do, its just some mundane header parsing iirc.

Quoted 15 lines
Quoted 14 lines

kernel/bpf/verifier.c static int check_cond_jmp_op(struct bpf_verifier_env *env, · line 7605

Quoted 8 lines
kernel/bpf/verifier.c +2 -1
 	if (pred >= 0) {
@@ -7517,7 +7601,8 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
 		 */
 		if (!__is_pointer_value(false, dst_reg))
 			err = mark_chain_precision(env, insn->dst_reg);
-		if (BPF_SRC(insn->code) == BPF_X && !err)
+		if (BPF_SRC(insn->code) == BPF_X && !err &&
+		    !__is_pointer_value(false, src_reg))

This could have been more specific with !type_is_pkt_pointer() correct? I think its fine as is though.

Quoted 2 lines

I actually meant to use __is_pointer_value() here for two reasons:
1. to match dst_reg check just few lines above.

Agree.

Quoted 4 lines

2. mark_chain_precision() is for scalars only. If in the future is_*_branch_taken() will support other kinds of pointers the more precise !type_is_pkt_pointer() check would need to be modified. That would be unnecessary code churn.

Agree.

#8 lore raw

PATCH v2 2/3 selftests/bpf: Add skb_pkt_end test

In reply to Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

Add a test that currently makes LLVM generate assembly code:

$ llvm-objdump -S skb_pkt_end.o
0000000000000000 <main_prog>:
; 	if (skb_shorter(skb, ETH_IPV4_TCP_SIZE))
       0:	61 12 50 00 00 00 00 00	r2 = *(u32 *)(r1 + 80)
       1:	61 14 4c 00 00 00 00 00	r4 = *(u32 *)(r1 + 76)
       2:	bf 43 00 00 00 00 00 00	r3 = r4
       3:	07 03 00 00 36 00 00 00	r3 += 54
       4:	b7 01 00 00 00 00 00 00	r1 = 0
       5:	2d 23 02 00 00 00 00 00	if r3 > r2 goto +2 <LBB0_2>
       6:	07 04 00 00 0e 00 00 00	r4 += 14
; 	if (skb_shorter(skb, ETH_IPV4_TCP_SIZE))
       7:	bf 41 00 00 00 00 00 00	r1 = r4
0000000000000040 <LBB0_2>:
       8:	b4 00 00 00 ff ff ff ff	w0 = -1
; 	if (!(ip = get_iphdr(skb)))
       9:	2d 23 05 00 00 00 00 00	if r3 > r2 goto +5 <LBB0_6>
; 	proto = ip->protocol;
      10:	71 12 09 00 00 00 00 00	r2 = *(u8 *)(r1 + 9)
; 	if (proto != IPPROTO_TCP)
      11:	56 02 03 00 06 00 00 00	if w2 != 6 goto +3 <LBB0_6>
; 	if (tcp->dest != 0)
      12:	69 12 16 00 00 00 00 00	r2 = *(u16 *)(r1 + 22)
      13:	56 02 01 00 00 00 00 00	if w2 != 0 goto +1 <LBB0_6>
; 	return tcp->urg_ptr;
      14:	69 10 26 00 00 00 00 00	r0 = *(u16 *)(r1 + 38)
0000000000000078 <LBB0_6>:
; }
      15:	95 00 00 00 00 00 00 00	exit
---
 .../bpf/prog_tests/test_skb_pkt_end.c         | 41 ++++++++++++++
 .../testing/selftests/bpf/progs/skb_pkt_end.c | 54 +++++++++++++++++++
 2 files changed, 95 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/test_skb_pkt_end.c
 create mode 100644 tools/testing/selftests/bpf/progs/skb_pkt_end.c
tools/testing/selftests/bpf/prog_tests/test_skb_pkt_end.c new file +41 -0
new file mode 100644
index 000000000000..cf1215531920
@@ -0,0 +1,41 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2020 Facebook */
+#include <test_progs.h>
+#include <network_helpers.h>
+#include "skb_pkt_end.skel.h"
+
+static int sanity_run(struct bpf_program *prog)
+{
+	__u32 duration, retval;
+	int err, prog_fd;
+
+	prog_fd = bpf_program__fd(prog);
+	err = bpf_prog_test_run(prog_fd, 1, &pkt_v4, sizeof(pkt_v4),
+				NULL, NULL, &retval, &duration);
+	if (CHECK(err || retval != 123, "test_run",
+		  "err %d errno %d retval %d duration %d\n",
+		  err, errno, retval, duration))
+		return -1;
+	return 0;
+}
+
+void test_test_skb_pkt_end(void)
+{
+	struct skb_pkt_end *skb_pkt_end_skel = NULL;
+	__u32 duration = 0;
+	int err;
+
+	skb_pkt_end_skel = skb_pkt_end__open_and_load();
+	if (CHECK(!skb_pkt_end_skel, "skb_pkt_end_skel_load", "skb_pkt_end skeleton failed\n"))
+		goto cleanup;
+
+	err = skb_pkt_end__attach(skb_pkt_end_skel);
+	if (CHECK(err, "skb_pkt_end_attach", "skb_pkt_end attach failed: %d\n", err))
+		goto cleanup;
+
+	if (sanity_run(skb_pkt_end_skel->progs.main_prog))
+		goto cleanup;
+
+cleanup:
+	skb_pkt_end__destroy(skb_pkt_end_skel);
+}
tools/testing/selftests/bpf/progs/skb_pkt_end.c new file +54 -0

54 lines not on this page · raw message

Signature · 1 line
2.24.1
reviewer Acked-by on v2 2/3
#9 lore raw

RE: [PATCH v2 bpf-next 2/3] selftests/bpf: Add skb_pkt_end test

Quoted 33 lines · Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

Add a test that currently makes LLVM generate assembly code:

$ llvm-objdump -S skb_pkt_end.o
0000000000000000 <main_prog>:
; 	if (skb_shorter(skb, ETH_IPV4_TCP_SIZE))
       0:	61 12 50 00 00 00 00 00	r2 = *(u32 *)(r1 + 80)
       1:	61 14 4c 00 00 00 00 00	r4 = *(u32 *)(r1 + 76)
       2:	bf 43 00 00 00 00 00 00	r3 = r4
       3:	07 03 00 00 36 00 00 00	r3 += 54
       4:	b7 01 00 00 00 00 00 00	r1 = 0
       5:	2d 23 02 00 00 00 00 00	if r3 > r2 goto +2 <LBB0_2>
       6:	07 04 00 00 0e 00 00 00	r4 += 14
; 	if (skb_shorter(skb, ETH_IPV4_TCP_SIZE))
       7:	bf 41 00 00 00 00 00 00	r1 = r4
0000000000000040 <LBB0_2>:
       8:	b4 00 00 00 ff ff ff ff	w0 = -1
; 	if (!(ip = get_iphdr(skb)))
       9:	2d 23 05 00 00 00 00 00	if r3 > r2 goto +5 <LBB0_6>
; 	proto = ip->protocol;
      10:	71 12 09 00 00 00 00 00	r2 = *(u8 *)(r1 + 9)
; 	if (proto != IPPROTO_TCP)
      11:	56 02 03 00 06 00 00 00	if w2 != 6 goto +3 <LBB0_6>
; 	if (tcp->dest != 0)
      12:	69 12 16 00 00 00 00 00	r2 = *(u16 *)(r1 + 22)
      13:	56 02 01 00 00 00 00 00	if w2 != 0 goto +1 <LBB0_6>
; 	return tcp->urg_ptr;
      14:	69 10 26 00 00 00 00 00	r0 = *(u16 *)(r1 + 38)
0000000000000078 <LBB0_6>:
; }
      15:	95 00 00 00 00 00 00 00	exit
Quoted 2 lines

---

#10 lore raw

PATCH v2 3/3 selftests/bpf: Add asm tests for pkt vs pkt_end comparison.

In reply to Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

Add few assembly tests for packet comparison.

---
 .../testing/selftests/bpf/verifier/ctx_skb.c  | 42 +++++++++++++++++++
 1 file changed, 42 insertions(+)
tools/testing/selftests/bpf/verifier/ctx_skb.c +42 -0
index 2e16b8e268f2..2022c0f2cd75 100644
@@ -1089,3 +1089,45 @@
 	.errstr_unpriv = "R1 leaks addr",
 	.result = REJECT,
 },
+{
+       "pkt > pkt_end taken check",
+       .insns = {
+       BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,                //  0. r2 = *(u32 *)(r1 + data_end)
+                   offsetof(struct __sk_buff, data_end)),
+       BPF_LDX_MEM(BPF_W, BPF_REG_4, BPF_REG_1,                //  1. r4 = *(u32 *)(r1 + data)
+                   offsetof(struct __sk_buff, data)),
+       BPF_MOV64_REG(BPF_REG_3, BPF_REG_4),                    //  2. r3 = r4
+       BPF_ALU64_IMM(BPF_ADD, BPF_REG_3, 42),                  //  3. r3 += 42
+       BPF_MOV64_IMM(BPF_REG_1, 0),                            //  4. r1 = 0
+       BPF_JMP_REG(BPF_JGT, BPF_REG_3, BPF_REG_2, 2),          //  5. if r3 > r2 goto 8
+       BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, 14),                  //  6. r4 += 14
+       BPF_MOV64_REG(BPF_REG_1, BPF_REG_4),                    //  7. r1 = r4
+       BPF_JMP_REG(BPF_JGT, BPF_REG_3, BPF_REG_2, 1),          //  8. if r3 > r2 goto 10
+       BPF_LDX_MEM(BPF_H, BPF_REG_2, BPF_REG_1, 9),            //  9. r2 = *(u8 *)(r1 + 9)
+       BPF_MOV64_IMM(BPF_REG_0, 0),                            // 10. r0 = 0
+       BPF_EXIT_INSN(),                                        // 11. exit
+       },
+       .result = ACCEPT,
+       .prog_type = BPF_PROG_TYPE_SK_SKB,
+},
+{
+       "pkt_end < pkt taken check",
+       .insns = {
+       BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,                //  0. r2 = *(u32 *)(r1 + data_end)
+                   offsetof(struct __sk_buff, data_end)),
+       BPF_LDX_MEM(BPF_W, BPF_REG_4, BPF_REG_1,                //  1. r4 = *(u32 *)(r1 + data)
+                   offsetof(struct __sk_buff, data)),
+       BPF_MOV64_REG(BPF_REG_3, BPF_REG_4),                    //  2. r3 = r4
+       BPF_ALU64_IMM(BPF_ADD, BPF_REG_3, 42),                  //  3. r3 += 42
+       BPF_MOV64_IMM(BPF_REG_1, 0),                            //  4. r1 = 0
+       BPF_JMP_REG(BPF_JGT, BPF_REG_3, BPF_REG_2, 2),          //  5. if r3 > r2 goto 8
+       BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, 14),                  //  6. r4 += 14
+       BPF_MOV64_REG(BPF_REG_1, BPF_REG_4),                    //  7. r1 = r4
+       BPF_JMP_REG(BPF_JLT, BPF_REG_2, BPF_REG_3, 1),          //  8. if r2 < r3 goto 10
+       BPF_LDX_MEM(BPF_H, BPF_REG_2, BPF_REG_1, 9),            //  9. r2 = *(u8 *)(r1 + 9)
+       BPF_MOV64_IMM(BPF_REG_0, 0),                            // 10. r0 = 0
+       BPF_EXIT_INSN(),                                        // 11. exit
+       },
+       .result = ACCEPT,
+       .prog_type = BPF_PROG_TYPE_SK_SKB,
+},
Signature · 1 line
2.24.1
reviewer Acked-by on v2 3/3
#11 lore raw

RE: [PATCH v2 bpf-next 3/3] selftests/bpf: Add asm tests for pkt vs pkt_end comparison.

Quoted 9 lines · Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

Add few assembly tests for packet comparison.

---
 .../testing/selftests/bpf/verifier/ctx_skb.c  | 42 +++++++++++++++++++
 1 file changed, 42 insertions(+)
#12 lore raw

Re: [PATCH v2 bpf-next 3/3] selftests/bpf: Add asm tests for pkt vs pkt_end comparison.

In reply to Alexei Starovoitov
Quoted 6 lines · Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

Add few assembly tests for packet comparison.

hi,
I'm now getting error when running this test:

#347/p pkt_end < pkt taken check Did not run the program (not supported) OK
Summary: 1 PASSED, 0 SKIPPED, 0 FAILED

it looks like my kernel does not have prog->aux->ops->test_run defined for BPF_PROG_TYPE_SK_SKB for some reason

do I miss some config option? I recall running this back in November, so I'm confused ;-)

thanks,
jirka

Quoted 57 lines
---
 .../testing/selftests/bpf/verifier/ctx_skb.c  | 42 +++++++++++++++++++
 1 file changed, 42 insertions(+)
tools/testing/selftests/bpf/verifier/ctx_skb.c +42 -0
index 2e16b8e268f2..2022c0f2cd75 100644
@@ -1089,3 +1089,45 @@
 	.errstr_unpriv = "R1 leaks addr",
 	.result = REJECT,
 },
+{
+       "pkt > pkt_end taken check",
+       .insns = {
+       BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,                //  0. r2 = *(u32 *)(r1 + data_end)
+                   offsetof(struct __sk_buff, data_end)),
+       BPF_LDX_MEM(BPF_W, BPF_REG_4, BPF_REG_1,                //  1. r4 = *(u32 *)(r1 + data)
+                   offsetof(struct __sk_buff, data)),
+       BPF_MOV64_REG(BPF_REG_3, BPF_REG_4),                    //  2. r3 = r4
+       BPF_ALU64_IMM(BPF_ADD, BPF_REG_3, 42),                  //  3. r3 += 42
+       BPF_MOV64_IMM(BPF_REG_1, 0),                            //  4. r1 = 0
+       BPF_JMP_REG(BPF_JGT, BPF_REG_3, BPF_REG_2, 2),          //  5. if r3 > r2 goto 8
+       BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, 14),                  //  6. r4 += 14
+       BPF_MOV64_REG(BPF_REG_1, BPF_REG_4),                    //  7. r1 = r4
+       BPF_JMP_REG(BPF_JGT, BPF_REG_3, BPF_REG_2, 1),          //  8. if r3 > r2 goto 10
+       BPF_LDX_MEM(BPF_H, BPF_REG_2, BPF_REG_1, 9),            //  9. r2 = *(u8 *)(r1 + 9)
+       BPF_MOV64_IMM(BPF_REG_0, 0),                            // 10. r0 = 0
+       BPF_EXIT_INSN(),                                        // 11. exit
+       },
+       .result = ACCEPT,
+       .prog_type = BPF_PROG_TYPE_SK_SKB,
+},
+{
+       "pkt_end < pkt taken check",
+       .insns = {
+       BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,                //  0. r2 = *(u32 *)(r1 + data_end)
+                   offsetof(struct __sk_buff, data_end)),
+       BPF_LDX_MEM(BPF_W, BPF_REG_4, BPF_REG_1,                //  1. r4 = *(u32 *)(r1 + data)
+                   offsetof(struct __sk_buff, data)),
+       BPF_MOV64_REG(BPF_REG_3, BPF_REG_4),                    //  2. r3 = r4
+       BPF_ALU64_IMM(BPF_ADD, BPF_REG_3, 42),                  //  3. r3 += 42
+       BPF_MOV64_IMM(BPF_REG_1, 0),                            //  4. r1 = 0
+       BPF_JMP_REG(BPF_JGT, BPF_REG_3, BPF_REG_2, 2),          //  5. if r3 > r2 goto 8
+       BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, 14),                  //  6. r4 += 14
+       BPF_MOV64_REG(BPF_REG_1, BPF_REG_4),                    //  7. r1 = r4
+       BPF_JMP_REG(BPF_JLT, BPF_REG_2, BPF_REG_3, 1),          //  8. if r2 < r3 goto 10
+       BPF_LDX_MEM(BPF_H, BPF_REG_2, BPF_REG_1, 9),            //  9. r2 = *(u8 *)(r1 + 9)
+       BPF_MOV64_IMM(BPF_REG_0, 0),                            // 10. r0 = 0
+       BPF_EXIT_INSN(),                                        // 11. exit
+       },
+       .result = ACCEPT,
+       .prog_type = BPF_PROG_TYPE_SK_SKB,
+},
Signature · 1 line
2.24.1
#13 lore raw
In reply to Alexei Starovoitov
This series was applied to bpf/bpf-next.git (refs/heads/master):

Hello:

This series was applied to bpf/bpf-next.git (refs/heads/master):

Quoted 17 lines · On Tue, 10 Nov 2020 19:12:10 -0800 you wrote:

From: Alexei Starovoitov <[email protected]>

v1->v2:
- removed set-but-unused variable.
- added Jiri's Tested-by.

In some cases LLVM uses the knowledge that branch is taken to optimze the code which causes the verifier to reject valid programs.
Teach the verifier to recognize that
r1 = skb->data;
r1 += 10;
r2 = skb->data_end;
if (r1 > r2) {
here r1 points beyond packet_end and subsequent
if (r1 > r2) // always evaluates to "true".
}

Quoted 1 line

[...]

Here is the summary with links:
- [v2,bpf-next,1/3] bpf: Support for pointers beyond pkt_end.

    https://git.kernel.org/bpf/bpf-next/c/6d94e741a8ff
  - [v2,bpf-next,2/3] selftests/bpf: Add skb_pkt_end test
    https://git.kernel.org/bpf/bpf-next/c/9cc873e85800
  - [v2,bpf-next,3/3] selftests/bpf: Add asm tests for pkt vs pkt_end comparison.
    https://git.kernel.org/bpf/bpf-next/c/cb62d34019d9

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html