selftests/bpf: Add test for signed 32-bit bound check bug
authorJean-Philippe Brucker <jean-philippe@linaro.org>
Tue, 8 Dec 2020 18:01:52 +0000 (19:01 +0100)
committerAlexei Starovoitov <ast@kernel.org>
Thu, 10 Dec 2020 21:11:30 +0000 (13:11 -0800)
After a 32-bit load followed by a branch, the verifier would reduce the
maximum bound of the register to 0x7fffffff, allowing a user to bypass
bound checks. Ensure such a program is rejected.

In the second test, the 64-bit compare should not sufficient to
determine whether the signed 32-bit lower bound is 0, so the verifier
should reject the second branch.

Signed-off-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/verifier/bounds.c

index dac40de..57ed67b 100644 (file)
        .fixup_map_hash_8b = { 3 },
        .result = ACCEPT,
 },
+{
+       "bounds checks after 32-bit truncation. test 1",
+       .insns = {
+       BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
+       BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
+       BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
+       BPF_LD_MAP_FD(BPF_REG_1, 0),
+       BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
+       BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4),
+       BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_0, 0),
+       /* This used to reduce the max bound to 0x7fffffff */
+       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 1),
+       BPF_JMP_IMM(BPF_JGT, BPF_REG_1, 0x7fffffff, 1),
+       BPF_MOV64_IMM(BPF_REG_0, 0),
+       BPF_EXIT_INSN(),
+       },
+       .fixup_map_hash_8b = { 3 },
+       .errstr_unpriv = "R0 leaks addr",
+       .result_unpriv = REJECT,
+       .result = ACCEPT,
+},
+{
+       "bounds checks after 32-bit truncation. test 2",
+       .insns = {
+       BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
+       BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
+       BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
+       BPF_LD_MAP_FD(BPF_REG_1, 0),
+       BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
+       BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4),
+       BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_0, 0),
+       BPF_JMP_IMM(BPF_JSLT, BPF_REG_1, 1, 1),
+       BPF_JMP32_IMM(BPF_JSLT, BPF_REG_1, 0, 1),
+       BPF_MOV64_IMM(BPF_REG_0, 0),
+       BPF_EXIT_INSN(),
+       },
+       .fixup_map_hash_8b = { 3 },
+       .errstr_unpriv = "R0 leaks addr",
+       .result_unpriv = REJECT,
+       .result = ACCEPT,
+},