bpf: add various test cases for verifier selftest
authorDaniel Borkmann <daniel@iogearbox.net>
Sun, 2 Jul 2017 00:13:31 +0000 (02:13 +0200)
committerDavid S. Miller <davem@davemloft.net>
Mon, 3 Jul 2017 09:22:52 +0000 (02:22 -0700)
Add couple of verifier test cases for x|imm += pkt_ptr, including the
imm += x extension.

Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
tools/testing/selftests/bpf/test_verifier.c

index 26f1eefdc782f48007d7ab9c2c407fe8d61dfda0..404aec5208128101812f6c9189ed046d1ec6eb24 100644 (file)
@@ -2657,6 +2657,171 @@ static struct bpf_test tests[] = {
                .prog_type = BPF_PROG_TYPE_SCHED_CLS,
                .flags = F_LOAD_WITH_STRICT_ALIGNMENT,
        },
+       {
+               "direct packet access: test18 (imm += pkt_ptr, 1)",
+               .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_IMM(BPF_REG_0, 8),
+                       BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_2),
+                       BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 1),
+                       BPF_STX_MEM(BPF_B, BPF_REG_2, BPF_REG_2, 0),
+                       BPF_MOV64_IMM(BPF_REG_0, 0),
+                       BPF_EXIT_INSN(),
+               },
+               .result = ACCEPT,
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+       },
+       {
+               "direct packet access: test19 (imm += pkt_ptr, 2)",
+               .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, 8),
+                       BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 3),
+                       BPF_MOV64_IMM(BPF_REG_4, 4),
+                       BPF_ALU64_REG(BPF_ADD, BPF_REG_4, BPF_REG_2),
+                       BPF_STX_MEM(BPF_B, BPF_REG_4, BPF_REG_4, 0),
+                       BPF_MOV64_IMM(BPF_REG_0, 0),
+                       BPF_EXIT_INSN(),
+               },
+               .result = ACCEPT,
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+       },
+       {
+               "direct packet access: test20 (x += pkt_ptr, 1)",
+               .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_IMM(BPF_REG_0, 0xffffffff),
+                       BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -8),
+                       BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_10, -8),
+                       BPF_ALU64_IMM(BPF_AND, BPF_REG_0, 0xffff),
+                       BPF_MOV64_REG(BPF_REG_4, BPF_REG_0),
+                       BPF_ALU64_REG(BPF_ADD, BPF_REG_4, BPF_REG_2),
+                       BPF_MOV64_REG(BPF_REG_5, BPF_REG_4),
+                       BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, 0xffff - 1),
+                       BPF_JMP_REG(BPF_JGT, BPF_REG_4, BPF_REG_3, 1),
+                       BPF_STX_MEM(BPF_DW, BPF_REG_5, BPF_REG_4, 0),
+                       BPF_MOV64_IMM(BPF_REG_0, 0),
+                       BPF_EXIT_INSN(),
+               },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .result = ACCEPT,
+       },
+       {
+               "direct packet access: test21 (x += pkt_ptr, 2)",
+               .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, 8),
+                       BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 9),
+                       BPF_MOV64_IMM(BPF_REG_4, 0xffffffff),
+                       BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_4, -8),
+                       BPF_LDX_MEM(BPF_DW, BPF_REG_4, BPF_REG_10, -8),
+                       BPF_ALU64_IMM(BPF_AND, BPF_REG_4, 0xffff),
+                       BPF_ALU64_REG(BPF_ADD, BPF_REG_4, BPF_REG_2),
+                       BPF_MOV64_REG(BPF_REG_5, BPF_REG_4),
+                       BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, 0xffff - 1),
+                       BPF_JMP_REG(BPF_JGT, BPF_REG_4, BPF_REG_3, 1),
+                       BPF_STX_MEM(BPF_DW, BPF_REG_5, BPF_REG_4, 0),
+                       BPF_MOV64_IMM(BPF_REG_0, 0),
+                       BPF_EXIT_INSN(),
+               },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .result = ACCEPT,
+       },
+       {
+               "direct packet access: test22 (x += pkt_ptr, 3)",
+               .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, 8),
+                       BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -8),
+                       BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_3, -16),
+                       BPF_LDX_MEM(BPF_DW, BPF_REG_3, BPF_REG_10, -16),
+                       BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 11),
+                       BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_10, -8),
+                       BPF_MOV64_IMM(BPF_REG_4, 0xffffffff),
+                       BPF_STX_XADD(BPF_DW, BPF_REG_10, BPF_REG_4, -8),
+                       BPF_LDX_MEM(BPF_DW, BPF_REG_4, BPF_REG_10, -8),
+                       BPF_ALU64_IMM(BPF_RSH, BPF_REG_4, 48),
+                       BPF_ALU64_REG(BPF_ADD, BPF_REG_4, BPF_REG_2),
+                       BPF_MOV64_REG(BPF_REG_0, BPF_REG_4),
+                       BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 2),
+                       BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 2),
+                       BPF_MOV64_IMM(BPF_REG_2, 1),
+                       BPF_STX_MEM(BPF_H, BPF_REG_4, BPF_REG_2, 0),
+                       BPF_MOV64_IMM(BPF_REG_0, 0),
+                       BPF_EXIT_INSN(),
+               },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .result = ACCEPT,
+       },
+       {
+               "direct packet access: test23 (x += pkt_ptr, 4)",
+               .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_IMM(BPF_REG_0, 0xffffffff),
+                       BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -8),
+                       BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_10, -8),
+                       BPF_ALU64_IMM(BPF_AND, BPF_REG_0, 0xffff),
+                       BPF_MOV64_REG(BPF_REG_4, BPF_REG_0),
+                       BPF_MOV64_IMM(BPF_REG_0, 31),
+                       BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_4),
+                       BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_2),
+                       BPF_MOV64_REG(BPF_REG_5, BPF_REG_0),
+                       BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 0xffff - 1),
+                       BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 1),
+                       BPF_STX_MEM(BPF_DW, BPF_REG_5, BPF_REG_0, 0),
+                       BPF_MOV64_IMM(BPF_REG_0, 0),
+                       BPF_EXIT_INSN(),
+               },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .result = REJECT,
+               .errstr = "cannot add integer value with 47 upper zero bits to ptr_to_packet",
+       },
+       {
+               "direct packet access: test24 (x += pkt_ptr, 5)",
+               .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_IMM(BPF_REG_0, 0xffffffff),
+                       BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_0, -8),
+                       BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_10, -8),
+                       BPF_ALU64_IMM(BPF_AND, BPF_REG_0, 0xff),
+                       BPF_MOV64_REG(BPF_REG_4, BPF_REG_0),
+                       BPF_MOV64_IMM(BPF_REG_0, 64),
+                       BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_4),
+                       BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_2),
+                       BPF_MOV64_REG(BPF_REG_5, BPF_REG_0),
+                       BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 0xffff - 1),
+                       BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 1),
+                       BPF_STX_MEM(BPF_DW, BPF_REG_5, BPF_REG_0, 0),
+                       BPF_MOV64_IMM(BPF_REG_0, 0),
+                       BPF_EXIT_INSN(),
+               },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .result = ACCEPT,
+       },
        {
                "helper access to packet: test1, valid packet_ptr range",
                .insns = {