bpf: fix div64 overflow tests to properly detect errors
authorNaveen N. Rao <naveen.n.rao@linux.vnet.ibm.com>
Wed, 12 Jun 2019 18:51:39 +0000 (00:21 +0530)
committerDaniel Borkmann <daniel@iogearbox.net>
Thu, 13 Jun 2019 21:07:00 +0000 (23:07 +0200)
If the result of the division is LLONG_MIN, current tests do not detect
the error since the return value is truncated to a 32-bit value and ends
up being 0.

Signed-off-by: Naveen N. Rao <naveen.n.rao@linux.vnet.ibm.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
tools/testing/selftests/bpf/verifier/div_overflow.c

index bd3f38dbe79646bd4a0bb557739eb60fda924322..acab4f00819f331a6bd58e24d373e88a97b6dce5 100644 (file)
        "DIV64 overflow, check 1",
        .insns = {
        BPF_MOV64_IMM(BPF_REG_1, -1),
-       BPF_LD_IMM64(BPF_REG_0, LLONG_MIN),
-       BPF_ALU64_REG(BPF_DIV, BPF_REG_0, BPF_REG_1),
+       BPF_LD_IMM64(BPF_REG_2, LLONG_MIN),
+       BPF_ALU64_REG(BPF_DIV, BPF_REG_2, BPF_REG_1),
+       BPF_MOV32_IMM(BPF_REG_0, 0),
+       BPF_JMP_REG(BPF_JEQ, BPF_REG_0, BPF_REG_2, 1),
+       BPF_MOV32_IMM(BPF_REG_0, 1),
        BPF_EXIT_INSN(),
        },
        .prog_type = BPF_PROG_TYPE_SCHED_CLS,
 {
        "DIV64 overflow, check 2",
        .insns = {
-       BPF_LD_IMM64(BPF_REG_0, LLONG_MIN),
-       BPF_ALU64_IMM(BPF_DIV, BPF_REG_0, -1),
+       BPF_LD_IMM64(BPF_REG_1, LLONG_MIN),
+       BPF_ALU64_IMM(BPF_DIV, BPF_REG_1, -1),
+       BPF_MOV32_IMM(BPF_REG_0, 0),
+       BPF_JMP_REG(BPF_JEQ, BPF_REG_0, BPF_REG_1, 1),
+       BPF_MOV32_IMM(BPF_REG_0, 1),
        BPF_EXIT_INSN(),
        },
        .prog_type = BPF_PROG_TYPE_SCHED_CLS,