]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
bpf: Test_verifier, add alu32 bounds tracking tests
authorJohn Fastabend <john.fastabend@gmail.com>
Thu, 5 Aug 2021 15:53:41 +0000 (18:53 +0300)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sun, 8 Aug 2021 07:04:09 +0000 (09:04 +0200)
commit 41f70fe0649dddf02046315dc566e06da5a2dc91 upstream

Its possible to have divergent ALU32 and ALU64 bounds when using JMP32
instructins and ALU64 arithmatic operations. Sometimes the clang will
even generate this code. Because the case is a bit tricky lets add
a specific test for it.

Here is  pseudocode asm version to illustrate the idea,

 1 r0 = 0xffffffff00000001;
 2 if w0 > 1 goto %l[fail];
 3 r0 += 1
 5 if w0 > 2 goto %l[fail]
 6 exit

The intent here is the verifier will fail the load if the 32bit bounds
are not tracked correctly through ALU64 op. Similarly we can check the
64bit bounds are correctly zero extended after ALU32 ops.

 1 r0 = 0xffffffff00000001;
 2 w0 += 1
 2 if r0 > 3 goto %l[fail];
 6 exit

The above will fail if we do not correctly zero extend 64bit bounds
after 32bit op.

Signed-off-by: John Fastabend <john.fastabend@gmail.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/158560430155.10843.514209255758200922.stgit@john-Precision-5820-Tower
Signed-off-by: Ovidiu Panait <ovidiu.panait@windriver.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
tools/testing/selftests/bpf/verifier/bounds.c

index d55f476f2237d816194d8f29c7425ff8a215a0a0..d8e5388c9ba793a73ae26a7205240c9f30748839 100644 (file)
        .errstr = "map_value pointer and 1000000000000",
        .result = REJECT
 },
+{
+       "bounds check mixed 32bit and 64bit arithmatic. test1",
+       .insns = {
+       BPF_MOV64_IMM(BPF_REG_0, 0),
+       BPF_MOV64_IMM(BPF_REG_1, -1),
+       BPF_ALU64_IMM(BPF_LSH, BPF_REG_1, 32),
+       BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 1),
+       /* r1 = 0xffffFFFF00000001 */
+       BPF_JMP32_IMM(BPF_JGT, BPF_REG_1, 1, 3),
+       /* check ALU64 op keeps 32bit bounds */
+       BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 1),
+       BPF_JMP32_IMM(BPF_JGT, BPF_REG_1, 2, 1),
+       BPF_JMP_A(1),
+       /* invalid ldx if bounds are lost above */
+       BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, -1),
+       BPF_EXIT_INSN(),
+       },
+       .result = ACCEPT
+},
+{
+       "bounds check mixed 32bit and 64bit arithmatic. test2",
+       .insns = {
+       BPF_MOV64_IMM(BPF_REG_0, 0),
+       BPF_MOV64_IMM(BPF_REG_1, -1),
+       BPF_ALU64_IMM(BPF_LSH, BPF_REG_1, 32),
+       BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 1),
+       /* r1 = 0xffffFFFF00000001 */
+       BPF_MOV64_IMM(BPF_REG_2, 3),
+       /* r1 = 0x2 */
+       BPF_ALU32_IMM(BPF_ADD, BPF_REG_1, 1),
+       /* check ALU32 op zero extends 64bit bounds */
+       BPF_JMP_REG(BPF_JGT, BPF_REG_1, BPF_REG_2, 1),
+       BPF_JMP_A(1),
+       /* invalid ldx if bounds are lost above */
+       BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, -1),
+       BPF_EXIT_INSN(),
+       },
+       .result = ACCEPT
+},