]> asedeno.scripts.mit.edu Git - linux.git/commitdiff
selftests/bpf: Test variable offset stack access
authorAndrey Ignatov <rdna@fb.com>
Fri, 29 Mar 2019 01:01:58 +0000 (18:01 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Fri, 29 Mar 2019 19:05:35 +0000 (12:05 -0700)
Test different scenarios of indirect variable-offset stack access: out of
bound access (>0), min_off below initialized part of the stack,
max_off+size above initialized part of the stack, initialized stack.

Example of output:
  ...
  #856/p indirect variable-offset stack access, out of bound OK
  #857/p indirect variable-offset stack access, max_off+size > max_initialized OK
  #858/p indirect variable-offset stack access, min_off < min_initialized OK
  #859/p indirect variable-offset stack access, ok OK
  ...

Signed-off-by: Andrey Ignatov <rdna@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/verifier/var_off.c

index 1e536ff121a5c0199a0316b9aec76ed1d0563313..c4ebd0bb07814f6c1095ea153b7c0bb065e19ed0 100644 (file)
@@ -40,7 +40,7 @@
        .prog_type = BPF_PROG_TYPE_LWT_IN,
 },
 {
-       "indirect variable-offset stack access",
+       "indirect variable-offset stack access, out of bound",
        .insns = {
        /* Fill the top 8 bytes of the stack */
        BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
        BPF_EXIT_INSN(),
        },
        .fixup_map_hash_8b = { 5 },
-       .errstr = "variable stack read R2",
+       .errstr = "invalid stack type R2 var_off",
        .result = REJECT,
        .prog_type = BPF_PROG_TYPE_LWT_IN,
 },
+{
+       "indirect variable-offset stack access, max_off+size > max_initialized",
+       .insns = {
+       /* Fill only the second from top 8 bytes of the stack. */
+       BPF_ST_MEM(BPF_DW, BPF_REG_10, -16, 0),
+       /* Get an unknown value. */
+       BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1, 0),
+       /* Make it small and 4-byte aligned. */
+       BPF_ALU64_IMM(BPF_AND, BPF_REG_2, 4),
+       BPF_ALU64_IMM(BPF_SUB, BPF_REG_2, 16),
+       /* Add it to fp.  We now have either fp-12 or fp-16, but we don't know
+        * which. fp-12 size 8 is partially uninitialized stack.
+        */
+       BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_10),
+       /* Dereference it indirectly. */
+       BPF_LD_MAP_FD(BPF_REG_1, 0),
+       BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
+       BPF_MOV64_IMM(BPF_REG_0, 0),
+       BPF_EXIT_INSN(),
+       },
+       .fixup_map_hash_8b = { 5 },
+       .errstr = "invalid indirect read from stack var_off",
+       .result = REJECT,
+       .prog_type = BPF_PROG_TYPE_LWT_IN,
+},
+{
+       "indirect variable-offset stack access, min_off < min_initialized",
+       .insns = {
+       /* Fill only the top 8 bytes of the stack. */
+       BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
+       /* Get an unknown value */
+       BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1, 0),
+       /* Make it small and 4-byte aligned. */
+       BPF_ALU64_IMM(BPF_AND, BPF_REG_2, 4),
+       BPF_ALU64_IMM(BPF_SUB, BPF_REG_2, 16),
+       /* Add it to fp.  We now have either fp-12 or fp-16, but we don't know
+        * which. fp-16 size 8 is partially uninitialized stack.
+        */
+       BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_10),
+       /* Dereference it indirectly. */
+       BPF_LD_MAP_FD(BPF_REG_1, 0),
+       BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
+       BPF_MOV64_IMM(BPF_REG_0, 0),
+       BPF_EXIT_INSN(),
+       },
+       .fixup_map_hash_8b = { 5 },
+       .errstr = "invalid indirect read from stack var_off",
+       .result = REJECT,
+       .prog_type = BPF_PROG_TYPE_LWT_IN,
+},
+{
+       "indirect variable-offset stack access, ok",
+       .insns = {
+       /* Fill the top 16 bytes of the stack. */
+       BPF_ST_MEM(BPF_DW, BPF_REG_10, -16, 0),
+       BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
+       /* Get an unknown value. */
+       BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1, 0),
+       /* Make it small and 4-byte aligned. */
+       BPF_ALU64_IMM(BPF_AND, BPF_REG_2, 4),
+       BPF_ALU64_IMM(BPF_SUB, BPF_REG_2, 16),
+       /* Add it to fp.  We now have either fp-12 or fp-16, we don't know
+        * which, but either way it points to initialized stack.
+        */
+       BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_10),
+       /* Dereference it indirectly. */
+       BPF_LD_MAP_FD(BPF_REG_1, 0),
+       BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
+       BPF_MOV64_IMM(BPF_REG_0, 0),
+       BPF_EXIT_INSN(),
+       },
+       .fixup_map_hash_8b = { 6 },
+       .result = ACCEPT,
+       .prog_type = BPF_PROG_TYPE_LWT_IN,
+},