selftests/bpf: Add test case to assert precise scalar path pruning
authorDaniel Borkmann <daniel@iogearbox.net>
Tue, 4 Apr 2023 14:05:58 +0000 (14:05 +0000)
committerDaniel Borkmann <daniel@iogearbox.net>
Thu, 27 Apr 2023 08:48:23 +0000 (10:48 +0200)
Add a test case to check for precision marking of safe paths. Ensure
that the verifier will not prematurely prune scalars contributing to
registers needing precision.

Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
tools/testing/selftests/bpf/verifier/precise.c

index 6c03a7d805f9d1708e6382b31902a5087193f968..8f0340eed696452af9f61035a81497eb98c157f7 100644 (file)
        .errstr = "invalid access to memory, mem_size=1 off=42 size=8",
        .result = REJECT,
 },
+{
+       "precise: program doesn't prematurely prune branches",
+       .insns = {
+               BPF_ALU64_IMM(BPF_MOV, BPF_REG_6, 0x400),
+               BPF_ALU64_IMM(BPF_MOV, BPF_REG_7, 0),
+               BPF_ALU64_IMM(BPF_MOV, BPF_REG_8, 0),
+               BPF_ALU64_IMM(BPF_MOV, BPF_REG_9, 0x80000000),
+               BPF_ALU64_IMM(BPF_MOD, BPF_REG_6, 0x401),
+               BPF_JMP_IMM(BPF_JA, 0, 0, 0),
+               BPF_JMP_REG(BPF_JLE, BPF_REG_6, BPF_REG_9, 2),
+               BPF_ALU64_IMM(BPF_MOD, BPF_REG_6, 1),
+               BPF_ALU64_IMM(BPF_MOV, BPF_REG_9, 0),
+               BPF_JMP_REG(BPF_JLE, BPF_REG_6, BPF_REG_9, 1),
+               BPF_ALU64_IMM(BPF_MOV, BPF_REG_6, 0),
+               BPF_ALU64_IMM(BPF_MOV, BPF_REG_0, 0),
+               BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_0, -4),
+               BPF_LD_MAP_FD(BPF_REG_4, 0),
+               BPF_ALU64_REG(BPF_MOV, BPF_REG_1, BPF_REG_4),
+               BPF_ALU64_REG(BPF_MOV, BPF_REG_2, BPF_REG_10),
+               BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4),
+               BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
+               BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
+               BPF_EXIT_INSN(),
+               BPF_ALU64_IMM(BPF_RSH, BPF_REG_6, 10),
+               BPF_ALU64_IMM(BPF_MUL, BPF_REG_6, 8192),
+               BPF_ALU64_REG(BPF_MOV, BPF_REG_1, BPF_REG_0),
+               BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_6),
+               BPF_LDX_MEM(BPF_DW, BPF_REG_3, BPF_REG_0, 0),
+               BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_3, 0),
+               BPF_EXIT_INSN(),
+       },
+       .fixup_map_array_48b = { 13 },
+       .prog_type = BPF_PROG_TYPE_XDP,
+       .result = REJECT,
+       .errstr = "register with unbounded min value is not allowed",
+},