]> git.baikalelectronics.ru Git - kernel.git/commitdiff
bpf: selftest: Add verifier tests for <8-byte scalar spill and refill
authorMartin KaFai Lau <kafai@fb.com>
Wed, 22 Sep 2021 00:49:53 +0000 (17:49 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Sun, 26 Sep 2021 20:07:28 +0000 (13:07 -0700)
This patch adds a few verifier tests for <8-byte spill and refill.

Signed-off-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20210922004953.627183-1-kafai@fb.com
tools/testing/selftests/bpf/verifier/spill_fill.c

index 0b943897aaf6c136a8fea40a3804042b28c44e37..c9991c3f3bd274275fe3c524a55ba1cc471e04df 100644 (file)
        .result = ACCEPT,
        .retval = POINTER_VALUE,
 },
+{
+       "Spill and refill a u32 const scalar.  Offset to skb->data",
+       .insns = {
+       BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
+                   offsetof(struct __sk_buff, data)),
+       BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
+                   offsetof(struct __sk_buff, data_end)),
+       /* r4 = 20 */
+       BPF_MOV32_IMM(BPF_REG_4, 20),
+       /* *(u32 *)(r10 -8) = r4 */
+       BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_4, -8),
+       /* r4 = *(u32 *)(r10 -8) */
+       BPF_LDX_MEM(BPF_W, BPF_REG_4, BPF_REG_10, -8),
+       /* r0 = r2 */
+       BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
+       /* r0 += r4 R0=pkt R2=pkt R3=pkt_end R4=inv20 */
+       BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_4),
+       /* if (r0 > r3) R0=pkt,off=20 R2=pkt R3=pkt_end R4=inv20 */
+       BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 1),
+       /* r0 = *(u32 *)r2 R0=pkt,off=20,r=20 R2=pkt,r=20 R3=pkt_end R4=inv20 */
+       BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_2, 0),
+       BPF_MOV64_IMM(BPF_REG_0, 0),
+       BPF_EXIT_INSN(),
+       },
+       .result = ACCEPT,
+       .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+},
+{
+       "Spill a u32 const, refill from another half of the uninit u32 from the stack",
+       .insns = {
+       /* r4 = 20 */
+       BPF_MOV32_IMM(BPF_REG_4, 20),
+       /* *(u32 *)(r10 -8) = r4 */
+       BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_4, -8),
+       /* r4 = *(u32 *)(r10 -4) fp-8=????rrrr*/
+       BPF_LDX_MEM(BPF_W, BPF_REG_4, BPF_REG_10, -4),
+       BPF_MOV64_IMM(BPF_REG_0, 0),
+       BPF_EXIT_INSN(),
+       },
+       .result = REJECT,
+       .errstr = "invalid read from stack off -4+0 size 4",
+       .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+},
+{
+       "Spill a u32 const scalar.  Refill as u16.  Offset to skb->data",
+       .insns = {
+       BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
+                   offsetof(struct __sk_buff, data)),
+       BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
+                   offsetof(struct __sk_buff, data_end)),
+       /* r4 = 20 */
+       BPF_MOV32_IMM(BPF_REG_4, 20),
+       /* *(u32 *)(r10 -8) = r4 */
+       BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_4, -8),
+       /* r4 = *(u16 *)(r10 -8) */
+       BPF_LDX_MEM(BPF_H, BPF_REG_4, BPF_REG_10, -8),
+       /* r0 = r2 */
+       BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
+       /* r0 += r4 R0=pkt R2=pkt R3=pkt_end R4=inv,umax=65535 */
+       BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_4),
+       /* if (r0 > r3) R0=pkt,umax=65535 R2=pkt R3=pkt_end R4=inv,umax=65535 */
+       BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 1),
+       /* r0 = *(u32 *)r2 R0=pkt,umax=65535 R2=pkt R3=pkt_end R4=inv20 */
+       BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_2, 0),
+       BPF_MOV64_IMM(BPF_REG_0, 0),
+       BPF_EXIT_INSN(),
+       },
+       .result = REJECT,
+       .errstr = "invalid access to packet",
+       .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+},
+{
+       "Spill a u32 const scalar.  Refill as u16 from fp-6.  Offset to skb->data",
+       .insns = {
+       BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
+                   offsetof(struct __sk_buff, data)),
+       BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
+                   offsetof(struct __sk_buff, data_end)),
+       /* r4 = 20 */
+       BPF_MOV32_IMM(BPF_REG_4, 20),
+       /* *(u32 *)(r10 -8) = r4 */
+       BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_4, -8),
+       /* r4 = *(u16 *)(r10 -6) */
+       BPF_LDX_MEM(BPF_H, BPF_REG_4, BPF_REG_10, -6),
+       /* r0 = r2 */
+       BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
+       /* r0 += r4 R0=pkt R2=pkt R3=pkt_end R4=inv,umax=65535 */
+       BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_4),
+       /* if (r0 > r3) R0=pkt,umax=65535 R2=pkt R3=pkt_end R4=inv,umax=65535 */
+       BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 1),
+       /* r0 = *(u32 *)r2 R0=pkt,umax=65535 R2=pkt R3=pkt_end R4=inv20 */
+       BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_2, 0),
+       BPF_MOV64_IMM(BPF_REG_0, 0),
+       BPF_EXIT_INSN(),
+       },
+       .result = REJECT,
+       .errstr = "invalid access to packet",
+       .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+},
+{
+       "Spill and refill a u32 const scalar at non 8byte aligned stack addr.  Offset to skb->data",
+       .insns = {
+       BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
+                   offsetof(struct __sk_buff, data)),
+       BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
+                   offsetof(struct __sk_buff, data_end)),
+       /* r4 = 20 */
+       BPF_MOV32_IMM(BPF_REG_4, 20),
+       /* *(u32 *)(r10 -8) = r4 */
+       BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_4, -8),
+       /* *(u32 *)(r10 -4) = r4 */
+       BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_4, -4),
+       /* r4 = *(u32 *)(r10 -4),  */
+       BPF_LDX_MEM(BPF_W, BPF_REG_4, BPF_REG_10, -4),
+       /* r0 = r2 */
+       BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
+       /* r0 += r4 R0=pkt R2=pkt R3=pkt_end R4=inv,umax=U32_MAX */
+       BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_4),
+       /* if (r0 > r3) R0=pkt,umax=U32_MAX R2=pkt R3=pkt_end R4=inv */
+       BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 1),
+       /* r0 = *(u32 *)r2 R0=pkt,umax=U32_MAX R2=pkt R3=pkt_end R4=inv */
+       BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_2, 0),
+       BPF_MOV64_IMM(BPF_REG_0, 0),
+       BPF_EXIT_INSN(),
+       },
+       .result = REJECT,
+       .errstr = "invalid access to packet",
+       .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+},
+{
+       "Spill and refill a umax=40 bounded scalar.  Offset to skb->data",
+       .insns = {
+       BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
+                   offsetof(struct __sk_buff, data)),
+       BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
+                   offsetof(struct __sk_buff, data_end)),
+       BPF_LDX_MEM(BPF_DW, BPF_REG_4, BPF_REG_1,
+                   offsetof(struct __sk_buff, tstamp)),
+       BPF_JMP_IMM(BPF_JLE, BPF_REG_4, 40, 2),
+       BPF_MOV64_IMM(BPF_REG_0, 0),
+       BPF_EXIT_INSN(),
+       /* *(u32 *)(r10 -8) = r4 R4=inv,umax=40 */
+       BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_4, -8),
+       /* r4 = (*u32 *)(r10 - 8) */
+       BPF_LDX_MEM(BPF_W, BPF_REG_4, BPF_REG_10, -8),
+       /* r2 += r4 R2=pkt R4=inv,umax=40 */
+       BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_4),
+       /* r0 = r2 R2=pkt,umax=40 R4=inv,umax=40 */
+       BPF_MOV64_REG(BPF_REG_0, BPF_REG_2),
+       /* r2 += 20 R0=pkt,umax=40 R2=pkt,umax=40 */
+       BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 20),
+       /* if (r2 > r3) R0=pkt,umax=40 R2=pkt,off=20,umax=40 */
+       BPF_JMP_REG(BPF_JGT, BPF_REG_2, BPF_REG_3, 1),
+       /* r0 = *(u32 *)r0 R0=pkt,r=20,umax=40 R2=pkt,off=20,r=20,umax=40 */
+       BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_0, 0),
+       BPF_MOV64_IMM(BPF_REG_0, 0),
+       BPF_EXIT_INSN(),
+       },
+       .result = ACCEPT,
+       .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+},