Skip to contentSkip to conversation

bpf: add test cases for non-pointer sanitiation logic

PATCH In review Daniel Borkmann 2 participants 5 messages Mar 1, 2019 – Mar 2, 2019

Subsystems (5)
lore
Share & tools
lore

Daniel Borkmann

author maintainer
#0 lore raw

PATCH 1/1 bpf: add test cases for non-pointer sanitiation logic

Add two additional tests for further asserting the BPF_ALU_NON_POINTER logic with cases that were missed previously.

Cc Marek Majkowski <[email protected]>
Cc Arthur Fabre <[email protected]>
---
 .../selftests/bpf/verifier/value_ptr_arith.c  | 44 ++++++++++++++++++-
 1 file changed, 43 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/verifier/value_ptr_arith.c +43 -1
index 4b721a77bebb..c3de1a2c9dc5 100644
@@ -173,7 +173,7 @@
 	.retval = 0,
 },
 {
-	"sanitation: alu with different scalars",
+	"sanitation: alu with different scalars 1",
 	.insns = {
 	BPF_MOV64_IMM(BPF_REG_0, 1),
 	BPF_LD_MAP_FD(BPF_REG_ARG1, 0),
@@ -198,6 +198,48 @@
 	.result = ACCEPT,
 	.retval = 0x100000,
 },
+{
+	"sanitation: alu with different scalars 2",
+	.insns = {
+	BPF_MOV64_IMM(BPF_REG_0, 1),
+	BPF_LD_MAP_FD(BPF_REG_1, 0),
+	BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
+	BPF_MOV64_REG(BPF_REG_2, BPF_REG_FP),
+	BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -16),
+	BPF_ST_MEM(BPF_DW, BPF_REG_FP, -16, 0),
+	BPF_EMIT_CALL(BPF_FUNC_map_delete_elem),
+	BPF_MOV64_REG(BPF_REG_7, BPF_REG_0),
+	BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
+	BPF_MOV64_REG(BPF_REG_2, BPF_REG_FP),
+	BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -16),
+	BPF_EMIT_CALL(BPF_FUNC_map_delete_elem),
+	BPF_MOV64_REG(BPF_REG_6, BPF_REG_0),
+	BPF_MOV64_REG(BPF_REG_8, BPF_REG_6),
+	BPF_ALU64_REG(BPF_ADD, BPF_REG_8, BPF_REG_7),
+	BPF_MOV64_REG(BPF_REG_0, BPF_REG_8),
+	BPF_EXIT_INSN(),
+	},
+	.fixup_map_array_48b = { 1 },
+	.result = ACCEPT,
+	.retval = -EINVAL * 2,
+},
+{
+	"sanitation: alu with different scalars 3",
+	.insns = {
+	BPF_MOV64_IMM(BPF_REG_0, EINVAL),
+	BPF_ALU64_IMM(BPF_MUL, BPF_REG_0, -1),
+	BPF_MOV64_REG(BPF_REG_7, BPF_REG_0),
+	BPF_MOV64_IMM(BPF_REG_0, EINVAL),
+	BPF_ALU64_IMM(BPF_MUL, BPF_REG_0, -1),
+	BPF_MOV64_REG(BPF_REG_6, BPF_REG_0),
+	BPF_MOV64_REG(BPF_REG_8, BPF_REG_6),
+	BPF_ALU64_REG(BPF_ADD, BPF_REG_8, BPF_REG_7),
+	BPF_MOV64_REG(BPF_REG_0, BPF_REG_8),
+	BPF_EXIT_INSN(),
+	},
+	.result = ACCEPT,
+	.retval = -EINVAL * 2,
+},
 {
 	"map access: value_ptr += known scalar, upper oob arith, test 1",
 	.insns = {
Signature · 1 line
2.17.1
#1 lore raw
Quoted 43 lines · Daniel Borkmann

Add two additional tests for further asserting the BPF_ALU_NON_POINTER logic with cases that were missed previously.

Cc Marek Majkowski <[email protected]>
Cc Arthur Fabre <[email protected]>
---
 .../selftests/bpf/verifier/value_ptr_arith.c  | 44 ++++++++++++++++++-
 1 file changed, 43 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/verifier/value_ptr_arith.c +15 -1
index 4b721a77bebb..c3de1a2c9dc5 100644
@@ -173,7 +173,7 @@
        .retval = 0,
 },
 {
-       "sanitation: alu with different scalars",
+       "sanitation: alu with different scalars 1",
        .insns = {
        BPF_MOV64_IMM(BPF_REG_0, 1),
        BPF_LD_MAP_FD(BPF_REG_ARG1, 0),
@@ -198,6 +198,48 @@
        .result = ACCEPT,
        .retval = 0x100000,
 },
+{
+       "sanitation: alu with different scalars 2",
+       .insns = {
+       BPF_MOV64_IMM(BPF_REG_0, 1),
+       BPF_LD_MAP_FD(BPF_REG_1, 0),
+       BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
+       BPF_MOV64_REG(BPF_REG_2, BPF_REG_FP),
+       BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -16),
+       BPF_ST_MEM(BPF_DW, BPF_REG_FP, -16, 0),
+       BPF_EMIT_CALL(BPF_FUNC_map_delete_elem),
+       BPF_MOV64_REG(BPF_REG_7, BPF_REG_0),
+       BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
+       BPF_MOV64_REG(BPF_REG_2, BPF_REG_FP),
+       BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -16),

tools/testing/selftests/bpf/verifier/value_ptr_arith.c line 224

Quoted 10 lines
tools/testing/selftests/bpf/verifier/value_ptr_arith.c +10 -0
+       BPF_EMIT_CALL(BPF_FUNC_map_delete_elem),
+       BPF_MOV64_REG(BPF_REG_6, BPF_REG_0),
+       BPF_MOV64_REG(BPF_REG_8, BPF_REG_6),
+       BPF_ALU64_REG(BPF_ADD, BPF_REG_8, BPF_REG_7),
+       BPF_MOV64_REG(BPF_REG_0, BPF_REG_8),
+       BPF_EXIT_INSN(),
+       },
+       .fixup_map_array_48b = { 1 },
+       .result = ACCEPT,
+       .retval = -EINVAL * 2,

Why "-EINVAL * 2" here?

Quoted 24 lines
tools/testing/selftests/bpf/verifier/value_ptr_arith.c +18 -1
+},
+{
+       "sanitation: alu with different scalars 3",
+       .insns = {
+       BPF_MOV64_IMM(BPF_REG_0, EINVAL),
+       BPF_ALU64_IMM(BPF_MUL, BPF_REG_0, -1),
+       BPF_MOV64_REG(BPF_REG_7, BPF_REG_0),
+       BPF_MOV64_IMM(BPF_REG_0, EINVAL),
+       BPF_ALU64_IMM(BPF_MUL, BPF_REG_0, -1),
+       BPF_MOV64_REG(BPF_REG_6, BPF_REG_0),
+       BPF_MOV64_REG(BPF_REG_8, BPF_REG_6),
+       BPF_ALU64_REG(BPF_ADD, BPF_REG_8, BPF_REG_7),
+       BPF_MOV64_REG(BPF_REG_0, BPF_REG_8),
+       BPF_EXIT_INSN(),
+       },
+       .result = ACCEPT,
+       .retval = -EINVAL * 2,
+},
 {
        "map access: value_ptr += known scalar, upper oob arith, test 1",
        .insns = {
--

2.17.1

author maintainer
#2 lore raw
Quoted 48 lines · Song Liu
Quoted 47 lines · Daniel Borkmann

Add two additional tests for further asserting the BPF_ALU_NON_POINTER logic with cases that were missed previously.

Cc Marek Majkowski <[email protected]>
Cc Arthur Fabre <[email protected]>
---
 .../selftests/bpf/verifier/value_ptr_arith.c  | 44 ++++++++++++++++++-
 1 file changed, 43 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/verifier/value_ptr_arith.c +19 -1
index 4b721a77bebb..c3de1a2c9dc5 100644
@@ -173,7 +173,7 @@
        .retval = 0,
 },
 {
-       "sanitation: alu with different scalars",
+       "sanitation: alu with different scalars 1",
        .insns = {
        BPF_MOV64_IMM(BPF_REG_0, 1),
        BPF_LD_MAP_FD(BPF_REG_ARG1, 0),
@@ -198,6 +198,48 @@
        .result = ACCEPT,
        .retval = 0x100000,
 },
+{
+       "sanitation: alu with different scalars 2",
+       .insns = {
+       BPF_MOV64_IMM(BPF_REG_0, 1),
+       BPF_LD_MAP_FD(BPF_REG_1, 0),
+       BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
+       BPF_MOV64_REG(BPF_REG_2, BPF_REG_FP),
+       BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -16),
+       BPF_ST_MEM(BPF_DW, BPF_REG_FP, -16, 0),
+       BPF_EMIT_CALL(BPF_FUNC_map_delete_elem),
+       BPF_MOV64_REG(BPF_REG_7, BPF_REG_0),
+       BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
+       BPF_MOV64_REG(BPF_REG_2, BPF_REG_FP),
+       BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -16),
+       BPF_EMIT_CALL(BPF_FUNC_map_delete_elem),
+       BPF_MOV64_REG(BPF_REG_6, BPF_REG_0),
+       BPF_MOV64_REG(BPF_REG_8, BPF_REG_6),
+       BPF_ALU64_REG(BPF_ADD, BPF_REG_8, BPF_REG_7),
Quoted 8 lines

tools/testing/selftests/bpf/verifier/value_ptr_arith.c line 224

Quoted 6 lines
tools/testing/selftests/bpf/verifier/value_ptr_arith.c +6 -0
+       BPF_MOV64_REG(BPF_REG_0, BPF_REG_8),
+       BPF_EXIT_INSN(),
+       },
+       .fixup_map_array_48b = { 1 },
+       .result = ACCEPT,
+       .retval = -EINVAL * 2,

Why "-EINVAL * 2" here?

This is array map so deletion will return -EINVAL, basically I wanted to get a test case under socket filters where helper returns a stable value which is then unknown scalar to do some alu64 op with where sanitation logic triggers. So map_delete_elem() came in handy for that test.

Thanks,
Daniel

Acked-by
#3 lore raw
Quoted 59 lines · Daniel Borkmann
Quoted 48 lines · Song Liu
Quoted 47 lines · Daniel Borkmann

Add two additional tests for further asserting the BPF_ALU_NON_POINTER logic with cases that were missed previously.

Cc Marek Majkowski <[email protected]>
Cc Arthur Fabre <[email protected]>
---
 .../selftests/bpf/verifier/value_ptr_arith.c  | 44 ++++++++++++++++++-
 1 file changed, 43 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/verifier/value_ptr_arith.c +19 -1
index 4b721a77bebb..c3de1a2c9dc5 100644
@@ -173,7 +173,7 @@
        .retval = 0,
 },
 {
-       "sanitation: alu with different scalars",
+       "sanitation: alu with different scalars 1",
        .insns = {
        BPF_MOV64_IMM(BPF_REG_0, 1),
        BPF_LD_MAP_FD(BPF_REG_ARG1, 0),
@@ -198,6 +198,48 @@
        .result = ACCEPT,
        .retval = 0x100000,
 },
+{
+       "sanitation: alu with different scalars 2",
+       .insns = {
+       BPF_MOV64_IMM(BPF_REG_0, 1),
+       BPF_LD_MAP_FD(BPF_REG_1, 0),
+       BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
+       BPF_MOV64_REG(BPF_REG_2, BPF_REG_FP),
+       BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -16),
+       BPF_ST_MEM(BPF_DW, BPF_REG_FP, -16, 0),
+       BPF_EMIT_CALL(BPF_FUNC_map_delete_elem),
+       BPF_MOV64_REG(BPF_REG_7, BPF_REG_0),
+       BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
+       BPF_MOV64_REG(BPF_REG_2, BPF_REG_FP),
+       BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -16),
+       BPF_EMIT_CALL(BPF_FUNC_map_delete_elem),
+       BPF_MOV64_REG(BPF_REG_6, BPF_REG_0),
+       BPF_MOV64_REG(BPF_REG_8, BPF_REG_6),
+       BPF_ALU64_REG(BPF_ADD, BPF_REG_8, BPF_REG_7),
Quoted 8 lines

tools/testing/selftests/bpf/verifier/value_ptr_arith.c line 224

Quoted 6 lines
tools/testing/selftests/bpf/verifier/value_ptr_arith.c +6 -0
+       BPF_MOV64_REG(BPF_REG_0, BPF_REG_8),
+       BPF_EXIT_INSN(),
+       },
+       .fixup_map_array_48b = { 1 },
+       .result = ACCEPT,
+       .retval = -EINVAL * 2,

Why "-EINVAL * 2" here?

Quoted 5 lines

This is array map so deletion will return -EINVAL, basically I wanted to get a test case under socket filters where helper returns a stable value which is then unknown scalar to do some alu64 op with where sanitation logic triggers. So map_delete_elem() came in handy for that test.

I see, so the *2 comes from

BPF_ALU64_REG(BPF_ADD, BPF_REG_8, BPF_REG_7)

Thanks for the explanation.

author maintainer
#4 lore raw
Quoted 3 lines · Song Liu
Quoted 2 lines · Daniel Borkmann
Quoted 1 line · Song Liu

On Fri, Mar 1, 2019 at 1:10 PM Daniel Borkmann <[email protected]> wrote:

[...]

Quoted 4 lines

I see, so the *2 comes from

BPF_ALU64_REG(BPF_ADD, BPF_REG_8, BPF_REG_7)

Yes, exactly the place we need to test for here.

Quoted 1 line

Thanks for the explanation.

np

Thanks,
Daniel