Skip to contentSkip to conversation

Move to BPF selftests

PATCH In review Daniel Borkmann 3 participants 6 messages Oct 17, 2016 – Oct 18, 2016

Subsystems (5)
lore
Share & tools
lore

Daniel Borkmann

author maintainer
#0 lore raw

cover Move to BPF selftests

This set improves the test_verifier and test_maps suite and moves it over to a new BPF selftest directory, so we can keep improving it under kernel selftest umbrella. This also integrates a test script for checking test_bpf.ko under various JIT options.

Thanks!

Daniel Borkmann (2):
bpf: add various tests around spill/fill of regs
bpf: add initial suite for selftests

 MAINTAINERS                                 |    3 +-
 samples/bpf/Makefile                        |    3 -
 samples/bpf/test_maps.c                     |  503 -----
 samples/bpf/test_verifier.c                 | 2529 ------------------------
 tools/include/linux/filter.h                |   24 +
 tools/testing/selftests/Makefile            |    3 +-
 tools/testing/selftests/bpf/.gitignore      |    2 +
 tools/testing/selftests/bpf/Makefile        |   13 +
 tools/testing/selftests/bpf/bpf_sys.h       |  108 ++
 tools/testing/selftests/bpf/config          |    5 +
 tools/testing/selftests/bpf/test_kmod.sh    |   39 +
 tools/testing/selftests/bpf/test_maps.c     |  525 +++++
 tools/testing/selftests/bpf/test_verifier.c | 2764 +++++++++++++++++++++++++++
 13 files changed, 3484 insertions(+), 3037 deletions(-)
 delete mode 100644 samples/bpf/test_maps.c
 delete mode 100644 samples/bpf/test_verifier.c
 create mode 100644 tools/testing/selftests/bpf/.gitignore
 create mode 100644 tools/testing/selftests/bpf/Makefile
 create mode 100644 tools/testing/selftests/bpf/bpf_sys.h
 create mode 100644 tools/testing/selftests/bpf/config
 create mode 100755 tools/testing/selftests/bpf/test_kmod.sh
 create mode 100644 tools/testing/selftests/bpf/test_maps.c
 create mode 100644 tools/testing/selftests/bpf/test_verifier.c
Signature · 1 line
1.9.3
author maintainer
#1 lore raw

PATCH 1/2 bpf: add various tests around spill/fill of regs

Add several spill/fill tests. Besides others, one that performs xadd on the spilled register, one ldx/stx test where different types are spilled from two branches and read out from common path. Verfier does handle all correctly.

---
 samples/bpf/test_verifier.c | 116 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 116 insertions(+)
samples/bpf/test_verifier.c +116 -0
index 369ffaa..45cf740 100644
@@ -14,6 +14,7 @@
 #include <linux/unistd.h>
 #include <string.h>
 #include <linux/filter.h>
+#include <linux/bpf_perf_event.h>
 #include <stddef.h>
 #include <stdbool.h>
 #include <sys/resource.h>
@@ -1128,6 +1129,108 @@ struct other_val {
 		.result = REJECT,
 	},
 	{
+		"unpriv: spill/fill of ctx",
+		.insns = {
+			BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_10),
+			BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, -8),
+			BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, 0),
+			BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_6, 0),
+			BPF_MOV64_IMM(BPF_REG_0, 0),
+			BPF_EXIT_INSN(),
+		},
+		.result = ACCEPT,
+	},
+	{
+		"unpriv: spill/fill of ctx 2",
+		.insns = {
+			BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_10),
+			BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, -8),
+			BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, 0),
+			BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_6, 0),
+			BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_get_hash_recalc),
+			BPF_EXIT_INSN(),
+		},
+		.result = ACCEPT,
+		.prog_type = BPF_PROG_TYPE_SCHED_CLS,
+	},
+	{
+		"unpriv: spill/fill of ctx 3",
+		.insns = {
+			BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_10),
+			BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, -8),
+			BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, 0),
+			BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_10, 0),
+			BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_6, 0),
+			BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_get_hash_recalc),
+			BPF_EXIT_INSN(),
+		},
+		.result = REJECT,
+		.errstr = "R1 type=fp expected=ctx",
+		.prog_type = BPF_PROG_TYPE_SCHED_CLS,
+	},
+	{
+		"unpriv: spill/fill of ctx 4",
+		.insns = {
+			BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_10),
+			BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, -8),
+			BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, 0),
+			BPF_MOV64_IMM(BPF_REG_0, 1),
+			BPF_RAW_INSN(BPF_STX | BPF_XADD | BPF_DW, BPF_REG_10, BPF_REG_0, -8, 0),
+			BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_6, 0),
+			BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_get_hash_recalc),
+			BPF_EXIT_INSN(),
+		},
+		.result = REJECT,
+		.errstr = "R1 type=inv expected=ctx",
+		.prog_type = BPF_PROG_TYPE_SCHED_CLS,
+	},
+	{
+		"unpriv: spill/fill of different pointers stx",
+		.insns = {
+			BPF_MOV64_IMM(BPF_REG_3, 42),
+			BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_10),
+			BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, -8),
+			BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 3),
+			BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
+			BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -16),
+			BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_2, 0),
+			BPF_JMP_IMM(BPF_JNE, BPF_REG_1, 0, 1),
+			BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, 0),
+			BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_6, 0),
+			BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_3,
+				    offsetof(struct __sk_buff, mark)),
+			BPF_MOV64_IMM(BPF_REG_0, 0),
+			BPF_EXIT_INSN(),
+		},
+		.result = REJECT,
+		.errstr = "same insn cannot be used with different pointers",
+		.prog_type = BPF_PROG_TYPE_SCHED_CLS,
+	},
+	{
+		"unpriv: spill/fill of different pointers ldx",
+		.insns = {
+			BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_10),
+			BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, -8),
+			BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 3),
+			BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
+			BPF_ALU64_IMM(BPF_ADD, BPF_REG_2,
+				      -(__s32)offsetof(struct bpf_perf_event_data,
+						       sample_period) - 8),
+			BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_2, 0),
+			BPF_JMP_IMM(BPF_JNE, BPF_REG_1, 0, 1),
+			BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, 0),
+			BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_6, 0),
+			BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_1,
+				    offsetof(struct bpf_perf_event_data,
+					     sample_period)),
+			BPF_MOV64_IMM(BPF_REG_0, 0),
+			BPF_EXIT_INSN(),
+		},
+		.result = REJECT,
+		.errstr = "same insn cannot be used with different pointers",
+		.prog_type = BPF_PROG_TYPE_PERF_EVENT,
+	},
+	{
 		"unpriv: write pointer into map elem value",
 		.insns = {
 			BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
@@ -1194,6 +1297,19 @@ struct other_val {
 		.result = REJECT,
 	},
 	{
+		"unpriv: spill/fill frame pointer",
+		.insns = {
+			BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_10),
+			BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, -8),
+			BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_10, 0),
+			BPF_LDX_MEM(BPF_DW, BPF_REG_10, BPF_REG_6, 0),
+			BPF_MOV64_IMM(BPF_REG_0, 0),
+			BPF_EXIT_INSN(),
+		},
+		.errstr = "frame pointer is read only",
+		.result = REJECT,
+	},
+	{
 		"unpriv: cmp of frame pointer",
 		.insns = {
 			BPF_JMP_IMM(BPF_JEQ, BPF_REG_10, 0, 0),
Signature · 1 line
1.9.3
Acked-by on v1 1/2
#2 lore raw

Re: [PATCH net-next 1/2] bpf: add various tests around spill/fill of regs

Quoted 6 lines · Daniel Borkmann

Add several spill/fill tests. Besides others, one that performs xadd on the spilled register, one ldx/stx test where different types are spilled from two branches and read out from common path. Verfier does handle all correctly.

author maintainer
#3 lore raw

PATCH 2/2 bpf: add initial suite for selftests

In reply to Daniel Borkmann

Add a start of a test suite for kernel selftests. This moves test_verifier and test_maps over to tools/testing/selftests/bpf/ along with various code improvements and also adds a script for invoking test_bpf module. The test suite can simply be run via selftest framework, f.e.:

  # cd tools/testing/selftests/bpf/
  # make
  # make run_tests

Both test_verifier and test_maps were kind of misplaced in samples/bpf/ directory and we were looking into adding them to selftests for a while now, so it can be picked up by kbuild bot et al and hopefully also get more exposure and thus new test case additions.

---
 MAINTAINERS                                 |    3 +-
 samples/bpf/Makefile                        |    3 -
 samples/bpf/test_maps.c                     |  503 -----
 samples/bpf/test_verifier.c                 | 2645 -------------------------
 tools/include/linux/filter.h                |   24 +
 tools/testing/selftests/Makefile            |    3 +-
 tools/testing/selftests/bpf/.gitignore      |    2 +
 tools/testing/selftests/bpf/Makefile        |   13 +
 tools/testing/selftests/bpf/bpf_sys.h       |  108 ++
 tools/testing/selftests/bpf/config          |    5 +
 tools/testing/selftests/bpf/test_kmod.sh    |   39 +
 tools/testing/selftests/bpf/test_maps.c     |  525 +++++
 tools/testing/selftests/bpf/test_verifier.c | 2764 +++++++++++++++++++++++++++
 13 files changed, 3484 insertions(+), 3153 deletions(-)
 delete mode 100644 samples/bpf/test_maps.c
 delete mode 100644 samples/bpf/test_verifier.c
 create mode 100644 tools/testing/selftests/bpf/.gitignore
 create mode 100644 tools/testing/selftests/bpf/Makefile
 create mode 100644 tools/testing/selftests/bpf/bpf_sys.h
 create mode 100644 tools/testing/selftests/bpf/config
 create mode 100755 tools/testing/selftests/bpf/test_kmod.sh
 create mode 100644 tools/testing/selftests/bpf/test_maps.c
 create mode 100644 tools/testing/selftests/bpf/test_verifier.c
MAINTAINERS +2 -1
index 1fc66f0a..f18b546 100644
@@ -2521,6 +2521,8 @@ L:	[email protected]
 L:	[email protected]
 S:	Supported
 F:	kernel/bpf/
+F:	tools/testing/selftests/bpf/
+F:	lib/test_bpf.c
 
 BROADCOM B44 10/100 ETHERNET DRIVER
 M:	Michael Chan <[email protected]>
@@ -8413,7 +8415,6 @@ F:	include/uapi/linux/net_namespace.h
 F:	tools/net/
 F:	tools/testing/selftests/net/
 F:	lib/random32.c
-F:	lib/test_bpf.c
 
 NETWORKING [IPv4/IPv6]
 M:	"David S. Miller" <[email protected]>
samples/bpf/Makefile +0 -3
index 12b7304..5c53fdb 100644
@@ -2,7 +2,6 @@
 obj- := dummy.o
 
 # List of programs to build
-hostprogs-y := test_verifier test_maps
 hostprogs-y += sock_example
 hostprogs-y += fds_example
 hostprogs-y += sockex1
@@ -28,8 +27,6 @@ hostprogs-y += test_current_task_under_cgroup
 hostprogs-y += trace_event
 hostprogs-y += sampleip
 
-test_verifier-objs := test_verifier.o libbpf.o
-test_maps-objs := test_maps.o libbpf.o
 sock_example-objs := sock_example.o libbpf.o
 fds_example-objs := bpf_load.o libbpf.o fds_example.o
 sockex1-objs := bpf_load.o libbpf.o sockex1_user.o
samples/bpf/test_maps.c deleted +0 -503

503 lines not on this page · raw message

samples/bpf/test_verifier.c deleted +0 -2645

2,467 lines not on this page · raw message

tools/include/linux/filter.h +24 -0
index 3276625..122153b 100644

Lines of this file are not shown. View the raw message.

tools/testing/selftests/Makefile +2 -1
index f770dba..a3144a3 100644

Lines of this file are not shown. View the raw message.

tools/testing/selftests/bpf/.gitignore new file +2 -0
new file mode 100644
index 0000000..3c59f96e

Lines of this file are not shown. View the raw message.

tools/testing/selftests/bpf/Makefile new file +13 -0
new file mode 100644
index 0000000..4761e2d6

Lines of this file are not shown. View the raw message.

tools/testing/selftests/bpf/bpf_sys.h new file +108 -0

108 lines not on this page · raw message

tools/testing/selftests/bpf/config new file +5 -0
new file mode 100644
index 0000000..52d53ed

Lines of this file are not shown. View the raw message.

tools/testing/selftests/bpf/test_kmod.sh new file +39 -0
new file mode 100755
index 0000000..92e627a

Lines of this file are not shown. View the raw message.

tools/testing/selftests/bpf/test_maps.c new file +525 -0

525 lines not on this page · raw message

tools/testing/selftests/bpf/test_verifier.c new file +2764 -0

2,764 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
1.9.3

Part of the diff is not shown here. View the raw message.

Acked-by on v1 2/2
#4 lore raw

Re: [PATCH net-next 2/2] bpf: add initial suite for selftests

Quoted 9 lines · Daniel Borkmann

Add a start of a test suite for kernel selftests. This moves test_verifier and test_maps over to tools/testing/selftests/bpf/ along with various code improvements and also adds a script for invoking test_bpf module. The test suite can simply be run via selftest framework, f.e.:

  # cd tools/testing/selftests/bpf/
  # make
  # make run_tests
Quoted 6 lines

Both test_verifier and test_maps were kind of misplaced in samples/bpf/ directory and we were looking into adding them to selftests for a while now, so it can be picked up by kbuild bot et al and hopefully also get more exposure and thus new test case additions.

maintainer
#5 lore raw
In reply to Daniel Borkmann

From: Daniel Borkmann <[email protected]>
Date: Mon, 17 Oct 2016 14:28:34 +0200

Quoted 4 lines

This set improves the test_verifier and test_maps suite and moves it over to a new BPF selftest directory, so we can keep improving it under kernel selftest umbrella. This also integrates a test script for checking test_bpf.ko under various JIT options.

Series applied, thanks Daniel.