selftests/bpf: check if BPF_ST with variable offset preserves STACK_ZERO
authorEduard Zingerman <eddyz87@gmail.com>
Tue, 14 Feb 2023 23:20:30 +0000 (01:20 +0200)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 15 Feb 2023 19:48:48 +0000 (11:48 -0800)
A test case to verify that variable offset BPF_ST instruction
preserves STACK_ZERO marks when writes zeros, e.g. in the following
situation:

  *(u64*)(r10 - 8) = 0   ; STACK_ZERO marks for fp[-8]
  r0 = random(-7, -1)    ; some random number in range of [-7, -1]
  r0 += r10              ; r0 is now variable offset pointer to stack
  *(u8*)(r0) = 0         ; BPF_ST writing zero, STACK_ZERO mark for
                         ; fp[-8] should be preserved.

Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/r/20230214232030.1502829-5-eddyz87@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/verifier/bpf_st_mem.c

index 932903f..3af2501 100644 (file)
        .expected_attach_type = BPF_SK_LOOKUP,
        .runs = -1,
 },
+{
+       "BPF_ST_MEM stack imm zero, variable offset",
+       .insns = {
+       /* set fp[-16], fp[-24] to zeros */
+       BPF_ST_MEM(BPF_DW, BPF_REG_10, -16, 0),
+       BPF_ST_MEM(BPF_DW, BPF_REG_10, -24, 0),
+       /* r0 = random value in range [-32, -15] */
+       BPF_EMIT_CALL(BPF_FUNC_get_prandom_u32),
+       BPF_JMP_IMM(BPF_JLE, BPF_REG_0, 16, 2),
+       BPF_MOV64_IMM(BPF_REG_0, 0),
+       BPF_EXIT_INSN(),
+       BPF_ALU64_IMM(BPF_SUB, BPF_REG_0, 32),
+       /* fp[r0] = 0, make a variable offset write of zero,
+        *             this should preserve zero marks on stack.
+        */
+       BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_10),
+       BPF_ST_MEM(BPF_B, BPF_REG_0, 0, 0),
+       /* r0 = fp[-20], if variable offset write was tracked correctly
+        *               r0 would be a known zero.
+        */
+       BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_10, -20),
+       /* Would fail return code verification if r0 range is not tracked correctly. */
+       BPF_EXIT_INSN(),
+       },
+       .result = ACCEPT,
+       /* Use prog type that requires return value in range [0, 1] */
+       .prog_type = BPF_PROG_TYPE_SK_LOOKUP,
+       .expected_attach_type = BPF_SK_LOOKUP,
+       .runs = -1,
+},