Skip to contentSkip to conversation

bpf: enable verifier to add 0 to packet ptr

PATCH In review William Tu 3 participants 7 messages Feb 3, 2017

Subsystems (6)
lore
Share & tools
lore

William Tu

author
#0 lore raw

PATCH v2 1/1 bpf: enable verifier to add 0 to packet ptr

The patch fixes the case when adding a zero value to the packet pointer. The verifer reports the following error:
[...]

    R0=imm0,min_value=0,max_value=0
    R1=pkt(id=0,off=0,r=4)
    R2=pkt_end R3=fp-12
    R4=imm4,min_value=4,max_value=4
    R5=pkt(id=0,off=4,r=4)

269: (bf) r2 = r0 // r2 becomes imm0
270: (77) r2 >>= 3
271: (bf) r4 = r1 // r4 becomes pkt ptr
272: (0f) r4 += r2 // r4 += 0
addition of negative constant to packet pointer is not allowed

---
 kernel/bpf/verifier.c                       |  2 +-
 tools/testing/selftests/bpf/test_verifier.c | 15 +++++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +1 -1
index fb3513b..1a754e5 100644
@@ -1397,7 +1397,7 @@ static int check_packet_ptr_add(struct bpf_verifier_env *env,
 		imm = insn->imm;
 
 add_imm:
-		if (imm <= 0) {
+		if (imm < 0) {
 			verbose("addition of negative constant to packet pointer is not allowed\n");
 			return -EACCES;
 		}
tools/testing/selftests/bpf/test_verifier.c +15 -0
index 0d0912c..a2b5c7e 100644
@@ -2404,6 +2404,21 @@ static struct bpf_test tests[] = {
 		.prog_type = BPF_PROG_TYPE_SCHED_CLS,
 	},
 	{
+		"direct packet access: test14 (pkt_ptr += 0, good access)",
+		.insns = {
+			BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
+				    offsetof(struct __sk_buff, data)),
+			BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
+				    offsetof(struct __sk_buff, data_end)),
+			BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
+			BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 0),
+			BPF_MOV64_IMM(BPF_REG_0, 1),
+			BPF_EXIT_INSN(),
+		},
+		.result = ACCEPT,
+		.prog_type = BPF_PROG_TYPE_SCHED_CLS,
+	},
+	{
 		"helper access to packet: test1, valid packet_ptr range",
 		.insns = {
 			BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
Signature · 1 line
2.7.4
maintainer Acked-by on v2 1/1
#1 lore raw

[ please keep us Cc'ed ;) ]

Quoted 14 lines · William Tu

The patch fixes the case when adding a zero value to the packet pointer. The verifer reports the following error:

   [...]
     R0=imm0,min_value=0,max_value=0
     R1=pkt(id=0,off=0,r=4)
     R2=pkt_end R3=fp-12
     R4=imm4,min_value=4,max_value=4
     R5=pkt(id=0,off=4,r=4)
   269: (bf) r2 = r0	// r2 becomes imm0
   270: (77) r2 >>= 3
   271: (bf) r4 = r1	// r4 becomes pkt ptr
   272: (0f) r4 += r2	// r4 += 0
   addition of negative constant to packet pointer is not allowed
Quoted 2 lines

Thanks!

#2 lore raw
In reply to William Tu
Quoted 40 lines · William Tu

The patch fixes the case when adding a zero value to the packet pointer. The verifer reports the following error:
[...]

    R0=imm0,min_value=0,max_value=0
    R1=pkt(id=0,off=0,r=4)
    R2=pkt_end R3=fp-12
    R4=imm4,min_value=4,max_value=4
    R5=pkt(id=0,off=4,r=4)

269: (bf) r2 = r0 // r2 becomes imm0
270: (77) r2 >>= 3
271: (bf) r4 = r1 // r4 becomes pkt ptr
272: (0f) r4 += r2 // r4 += 0
addition of negative constant to packet pointer is not allowed

---
 kernel/bpf/verifier.c                       |  2 +-
 tools/testing/selftests/bpf/test_verifier.c | 15 +++++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +1 -1
index fb3513b..1a754e5 100644
@@ -1397,7 +1397,7 @@ static int check_packet_ptr_add(struct bpf_verifier_env *env,
 		imm = insn->imm;
 
 add_imm:
-		if (imm <= 0) {
+		if (imm < 0) {
 			verbose("addition of negative constant to packet pointer is not allowed\n");
 			return -EACCES;
 		}
tools/testing/selftests/bpf/test_verifier.c +0 -0
index 0d0912c..a2b5c7e 100644
@@ -2404,6 +2404,21 @@ static struct bpf_test tests[] = {
 		.prog_type = BPF_PROG_TYPE_SCHED_CLS,

tools/testing/selftests/bpf/test_verifier.c static struct bpf_test tests[] = { · line 2414

Quoted 10 lines
tools/testing/selftests/bpf/test_verifier.c +8 -0
 	},
 	{
+		"direct packet access: test14 (pkt_ptr += 0, good access)",
+		.insns = {
+			BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
+				    offsetof(struct __sk_buff, data)),
+			BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
+				    offsetof(struct __sk_buff, data_end)),
+			BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
+			BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 0),

wait. the test is bogus.
please write the proper test for the feature
and check that it fails before the patch and passes afterwards.

Quoted 13 lines
tools/testing/selftests/bpf/test_verifier.c +7 -0
+			BPF_MOV64_IMM(BPF_REG_0, 1),
+			BPF_EXIT_INSN(),
+		},
+		.result = ACCEPT,
+		.prog_type = BPF_PROG_TYPE_SCHED_CLS,
+	},
+	{
 		"helper access to packet: test1, valid packet_ptr range",
 		.insns = {
 			BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
Signature · 1 line
2.7.4
author
#3 lore raw

Hi Alexei,

why it is bogus? on my system, it fails without the patch applied.

--William

Quoted 69 lines · Alexei Starovoitov
Quoted 40 lines · William Tu

The patch fixes the case when adding a zero value to the packet pointer. The verifer reports the following error:
[...]

    R0=imm0,min_value=0,max_value=0
    R1=pkt(id=0,off=0,r=4)
    R2=pkt_end R3=fp-12
    R4=imm4,min_value=4,max_value=4
    R5=pkt(id=0,off=4,r=4)

269: (bf) r2 = r0 // r2 becomes imm0
270: (77) r2 >>= 3
271: (bf) r4 = r1 // r4 becomes pkt ptr
272: (0f) r4 += r2 // r4 += 0
addition of negative constant to packet pointer is not allowed

---
 kernel/bpf/verifier.c                       |  2 +-
 tools/testing/selftests/bpf/test_verifier.c | 15 +++++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +1 -1
index fb3513b..1a754e5 100644
@@ -1397,7 +1397,7 @@ static int check_packet_ptr_add(struct bpf_verifier_env *env,
              imm = insn->imm;
 
 add_imm:
-             if (imm <= 0) {
+             if (imm < 0) {
                      verbose("addition of negative constant to packet pointer is not allowed\n");
                      return -EACCES;
              }
tools/testing/selftests/bpf/test_verifier.c +0 -0
index 0d0912c..a2b5c7e 100644
@@ -2404,6 +2404,21 @@ static struct bpf_test tests[] = {
              .prog_type = BPF_PROG_TYPE_SCHED_CLS,

tools/testing/selftests/bpf/test_verifier.c static struct bpf_test tests[] = { · line 2414

Quoted 10 lines
tools/testing/selftests/bpf/test_verifier.c +8 -0
      },
      {
+             "direct packet access: test14 (pkt_ptr += 0, good access)",
+             .insns = {
+                     BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
+                                 offsetof(struct __sk_buff, data)),
+                     BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
+                                 offsetof(struct __sk_buff, data_end)),
+                     BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
+                     BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 0),

wait. the test is bogus.
please write the proper test for the feature
and check that it fails before the patch and passes afterwards.

Quoted 13 lines
tools/testing/selftests/bpf/test_verifier.c +7 -1
+                     BPF_MOV64_IMM(BPF_REG_0, 1),
+                     BPF_EXIT_INSN(),
+             },
+             .result = ACCEPT,
+             .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+     },
+     {
              "helper access to packet: test1, valid packet_ptr range",
              .insns = {
                      BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
--

2.7.4

maintainer
#4 lore raw
Quoted 25 lines · William Tu

Hi Alexei,

why it is bogus? on my system, it fails without the patch applied.

--William

Quoted 17 lines · Alexei Starovoitov
Quoted 16 lines · William Tu

The patch fixes the case when adding a zero value to the packet pointer. The verifer reports the following error:

   [...]
     R0=imm0,min_value=0,max_value=0
     R1=pkt(id=0,off=0,r=4)
     R2=pkt_end R3=fp-12
     R4=imm4,min_value=4,max_value=4
     R5=pkt(id=0,off=4,r=4)
   269: (bf) r2 = r0   // r2 becomes imm0
   270: (77) r2 >>= 3
   271: (bf) r4 = r1   // r4 becomes pkt ptr
   272: (0f) r4 += r2  // r4 += 0
   addition of negative constant to packet pointer is not allowed

[...]

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
       {
tools/testing/selftests/bpf/test_verifier.c +8 -0
+             "direct packet access: test14 (pkt_ptr += 0, good access)",
+             .insns = {
+                     BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
+                                 offsetof(struct __sk_buff, data)),
+                     BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
+                                 offsetof(struct __sk_buff, data_end)),
+                     BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
+                     BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 0),
Quoted 3 lines
Quoted 3 lines

wait. the test is bogus.
please write the proper test for the feature
and check that it fails before the patch and passes afterwards.

But still same code path that is executed in verifier as BPF_K and CONST_IMM tracked reg both share the same path under add_imm label in check_packet_ptr_add(), no? So it becomes r2=pkt(id=0,off=0,r=0); r0 = r2; r0 += 0 here in this test. Probably okay as well, though there could be risk that in future both don't share the same path for some reason. I guess you were referring to either adding tests for BPF_K /and/ CONST_IMM reg or just the latter, right?

#5 lore raw
Quoted 41 lines · Daniel Borkmann
Quoted 25 lines · William Tu

Hi Alexei,

why it is bogus? on my system, it fails without the patch applied.

--William

Quoted 17 lines · Alexei Starovoitov
Quoted 16 lines · William Tu

The patch fixes the case when adding a zero value to the packet pointer. The verifer reports the following error:
[...]

    R0=imm0,min_value=0,max_value=0
    R1=pkt(id=0,off=0,r=4)
    R2=pkt_end R3=fp-12
    R4=imm4,min_value=4,max_value=4
    R5=pkt(id=0,off=4,r=4)

269: (bf) r2 = r0 // r2 becomes imm0
270: (77) r2 >>= 3
271: (bf) r4 = r1 // r4 becomes pkt ptr
272: (0f) r4 += r2 // r4 += 0
addition of negative constant to packet pointer is not allowed

[...]

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
      {
tools/testing/selftests/bpf/test_verifier.c +8 -0
+             "direct packet access: test14 (pkt_ptr += 0, good access)",
+             .insns = {
+                     BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
+                                 offsetof(struct __sk_buff, data)),
+                     BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
+                                 offsetof(struct __sk_buff, data_end)),
+                     BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
+                     BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 0),
Quoted 3 lines
Quoted 3 lines

wait. the test is bogus.
please write the proper test for the feature
and check that it fails before the patch and passes afterwards.

Quoted 7 lines

But still same code path that is executed in verifier as BPF_K and CONST_IMM tracked reg both share the same path under add_imm label in check_packet_ptr_add(), no? So it becomes r2=pkt(id=0,off=0,r=0); r0 = r2; r0 += 0 here in this test. Probably okay as well, though there could be risk that in future both don't share the same path for some reason. I guess you were referring to either adding tests for BPF_K /and/ CONST_IMM reg or just the latter, right?

yes. Sorry I wasn't clear.
imo the 'r0 += 0' is not something that verifier should recognize, since such nop insns shouldn't be generated by the compiler. It happened that the code path in verifier covers that case as well, but I think we really need to test 'rX += rY' case where rY is recognized as imm0, since that what the original use case was about.

author
#6 lore raw

Thanks. I got it. I will resubmit v3 patch!

Quoted 59 lines · Alexei Starovoitov
Quoted 41 lines · Daniel Borkmann
Quoted 25 lines · William Tu

Hi Alexei,

why it is bogus? on my system, it fails without the patch applied.

--William

Quoted 17 lines · Alexei Starovoitov
Quoted 16 lines · William Tu

The patch fixes the case when adding a zero value to the packet pointer. The verifer reports the following error:
[...]

    R0=imm0,min_value=0,max_value=0
    R1=pkt(id=0,off=0,r=4)
    R2=pkt_end R3=fp-12
    R4=imm4,min_value=4,max_value=4
    R5=pkt(id=0,off=4,r=4)

269: (bf) r2 = r0 // r2 becomes imm0
270: (77) r2 >>= 3
271: (bf) r4 = r1 // r4 becomes pkt ptr
272: (0f) r4 += r2 // r4 += 0
addition of negative constant to packet pointer is not allowed

[...]

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
      {
tools/testing/selftests/bpf/test_verifier.c +8 -0
+             "direct packet access: test14 (pkt_ptr += 0, good access)",
+             .insns = {
+                     BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
+                                 offsetof(struct __sk_buff, data)),
+                     BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
+                                 offsetof(struct __sk_buff, data_end)),
+                     BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
+                     BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 0),
Quoted 3 lines
Quoted 3 lines

wait. the test is bogus.
please write the proper test for the feature
and check that it fails before the patch and passes afterwards.

Quoted 7 lines

But still same code path that is executed in verifier as BPF_K and CONST_IMM tracked reg both share the same path under add_imm label in check_packet_ptr_add(), no? So it becomes r2=pkt(id=0,off=0,r=0); r0 = r2; r0 += 0 here in this test. Probably okay as well, though there could be risk that in future both don't share the same path for some reason. I guess you were referring to either adding tests for BPF_K /and/ CONST_IMM reg or just the latter, right?

yes. Sorry I wasn't clear.
imo the 'r0 += 0' is not something that verifier should recognize, since such nop insns shouldn't be generated by the compiler. It happened that the code path in verifier covers that case as well, but I think we really need to test 'rX += rY' case where rY is recognized as imm0, since that what the original use case was about.