reference tracking: leak potential reference
reference tracking: leak potential reference on stack
reference tracking: leak potential reference on stack 2
reference tracking: zero potential reference
reference tracking: copy and zero potential references
reference tracking: release reference without check
reference tracking: release reference
reference tracking: release reference twice
reference tracking: release reference twice inside branch
reference tracking: alloc, check, free in one subbranch
reference tracking: alloc, check, free in both subbranches
reference tracking in call: free reference in subprog
reference tracking in call: free reference in subprog and outside
reference tracking in call: alloc & leak reference in subprog
reference tracking in call: alloc in subprog, release outside
reference tracking in call: sk_ptr leak into caller stack
reference tracking in call: sk_ptr spill into caller stack
reference tracking: allow LD_ABS
reference tracking: forbid LD_ABS while holding reference
reference tracking: allow LD_IND
reference tracking: forbid LD_IND while holding reference
reference tracking: check reference or tail call
reference tracking: release reference then tail call
reference tracking: leak possible reference over tail call
reference tracking: leak checked reference over tail call
reference tracking: mangle and release sock_or_null
reference tracking: mangle and release sock
reference tracking: access member
reference tracking: write to member
reference tracking: invalid 64-bit access of member
reference tracking: access after release
reference tracking: direct access for lookup
unpriv: spill/fill of different pointers stx - ctx and sock
unpriv: spill/fill of different pointers stx - leak sock
unpriv: spill/fill of different pointers stx - sock and ctx (read)
unpriv: spill/fill of different pointers stx - sock and ctx (write)

Signed-off-by: Joe Stringer <j...@wand.net.nz>

---
v3: Rebase against bpf_sk_release() flags argument removal.
    Removed Alexei's ack since there are many new tests:
    * "reference tracking: allow LD_ABS",
    * "reference tracking: forbid LD_ABS while holding reference",
    * "reference tracking: allow LD_IND",
    * "reference tracking: forbid LD_IND while holding reference",
    * "reference tracking: check reference or tail call",
    * "reference tracking: release reference then tail call",
    * "reference tracking: leak possible reference over tail call",
    * "reference tracking: leak checked reference over tail call",
    * "reference tracking: mangle and release sock_or_null",
    * "reference tracking: mangle and release sock",
    * "reference tracking: access member",
    * "reference tracking: write to member",
    * "reference tracking: invalid 64-bit access of member",
    * "reference tracking: access after release",
    * "reference tracking: direct access for lookup",
v4: New tests:
    * unpriv: spill/fill of different pointers stx - ctx and sock
    * unpriv: spill/fill of different pointers stx - leak sock
    * unpriv: spill/fill of different pointers stx - sock and ctx (read)
    * unpriv: spill/fill of different pointers stx - sock and ctx (write)
---
 tools/testing/selftests/bpf/test_verifier.c | 759 ++++++++++++++++++++
 1 file changed, 759 insertions(+)

diff --git a/tools/testing/selftests/bpf/test_verifier.c 
b/tools/testing/selftests/bpf/test_verifier.c
index 163fd1c0062c..bc9cd8537467 100644
--- a/tools/testing/selftests/bpf/test_verifier.c
+++ b/tools/testing/selftests/bpf/test_verifier.c
@@ -3,6 +3,7 @@
  *
  * Copyright (c) 2014 PLUMgrid, http://plumgrid.com
  * Copyright (c) 2017 Facebook
+ * Copyright (c) 2018 Covalent IO, Inc. http://covalent.io
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of version 2 of the GNU General Public
@@ -178,6 +179,24 @@ static void bpf_fill_rand_ld_dw(struct bpf_test *self)
        self->retval = (uint32_t)res;
 }
 
+/* BPF_SK_LOOKUP contains 13 instructions, if you need to fix up maps */
+#define BPF_SK_LOOKUP                                                  \
+       /* struct bpf_sock_tuple tuple = {} */                          \
+       BPF_MOV64_IMM(BPF_REG_2, 0),                                    \
+       BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_2, -8),                  \
+       BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -16),                \
+       BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -24),                \
+       BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -32),                \
+       BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -40),                \
+       BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_2, -48),                \
+       /* sk = sk_lookup_tcp(ctx, &tuple, sizeof tuple, 0, 0) */       \
+       BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),                           \
+       BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -48),                         \
+       BPF_MOV64_IMM(BPF_REG_3, sizeof(struct bpf_sock_tuple)),        \
+       BPF_MOV64_IMM(BPF_REG_4, 0),                                    \
+       BPF_MOV64_IMM(BPF_REG_5, 0),                                    \
+       BPF_EMIT_CALL(BPF_FUNC_sk_lookup_tcp)
+
 static struct bpf_test tests[] = {
        {
                "add+sub+mul",
@@ -2707,6 +2726,137 @@ static struct bpf_test tests[] = {
                .errstr = "same insn cannot be used with different pointers",
                .prog_type = BPF_PROG_TYPE_SCHED_CLS,
        },
+       {
+               "unpriv: spill/fill of different pointers stx - ctx and sock",
+               .insns = {
+                       BPF_MOV64_REG(BPF_REG_8, BPF_REG_1),
+                       /* struct bpf_sock *sock = bpf_sock_lookup(...); */
+                       BPF_SK_LOOKUP,
+                       BPF_MOV64_REG(BPF_REG_2, BPF_REG_0),
+                       /* u64 foo; */
+                       /* void *target = &foo; */
+                       BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_10),
+                       BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, -8),
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_8),
+                       /* if (skb == NULL) *target = sock; */
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 1),
+                               BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_2, 0),
+                       /* else *target = skb; */
+                       BPF_JMP_IMM(BPF_JNE, BPF_REG_1, 0, 1),
+                               BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, 0),
+                       /* struct __sk_buff *skb = *target; */
+                       BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_6, 0),
+                       /* skb->mark = 42; */
+                       BPF_MOV64_IMM(BPF_REG_3, 42),
+                       BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_3,
+                                   offsetof(struct __sk_buff, mark)),
+                       /* if (sk) bpf_sk_release(sk) */
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 1),
+                               BPF_EMIT_CALL(BPF_FUNC_sk_release),
+                       BPF_MOV64_IMM(BPF_REG_0, 0),
+                       BPF_EXIT_INSN(),
+               },
+               .result = REJECT,
+               .errstr = "type=ctx expected=sock",
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+       },
+       {
+               "unpriv: spill/fill of different pointers stx - leak sock",
+               .insns = {
+                       BPF_MOV64_REG(BPF_REG_8, BPF_REG_1),
+                       /* struct bpf_sock *sock = bpf_sock_lookup(...); */
+                       BPF_SK_LOOKUP,
+                       BPF_MOV64_REG(BPF_REG_2, BPF_REG_0),
+                       /* u64 foo; */
+                       /* void *target = &foo; */
+                       BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_10),
+                       BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, -8),
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_8),
+                       /* if (skb == NULL) *target = sock; */
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 1),
+                               BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_2, 0),
+                       /* else *target = skb; */
+                       BPF_JMP_IMM(BPF_JNE, BPF_REG_1, 0, 1),
+                               BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, 0),
+                       /* struct __sk_buff *skb = *target; */
+                       BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_6, 0),
+                       /* skb->mark = 42; */
+                       BPF_MOV64_IMM(BPF_REG_3, 42),
+                       BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_3,
+                                   offsetof(struct __sk_buff, mark)),
+                       BPF_EXIT_INSN(),
+               },
+               .result = REJECT,
+               //.errstr = "same insn cannot be used with different pointers",
+               .errstr = "Unreleased reference",
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+       },
+       {
+               "unpriv: spill/fill of different pointers stx - sock and ctx 
(read)",
+               .insns = {
+                       BPF_MOV64_REG(BPF_REG_8, BPF_REG_1),
+                       /* struct bpf_sock *sock = bpf_sock_lookup(...); */
+                       BPF_SK_LOOKUP,
+                       BPF_MOV64_REG(BPF_REG_2, BPF_REG_0),
+                       /* u64 foo; */
+                       /* void *target = &foo; */
+                       BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_10),
+                       BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, -8),
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_8),
+                       /* if (skb) *target = skb */
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 1),
+                               BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, 0),
+                       /* else *target = sock */
+                       BPF_JMP_IMM(BPF_JNE, BPF_REG_1, 0, 1),
+                               BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_2, 0),
+                       /* struct bpf_sock *sk = *target; */
+                       BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_6, 0),
+                       /* if (sk) u32 foo = sk->mark; bpf_sk_release(sk); */
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 2),
+                               BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
+                                           offsetof(struct bpf_sock, mark)),
+                               BPF_EMIT_CALL(BPF_FUNC_sk_release),
+                       BPF_MOV64_IMM(BPF_REG_0, 0),
+                       BPF_EXIT_INSN(),
+               },
+               .result = REJECT,
+               .errstr = "same insn cannot be used with different pointers",
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+       },
+       {
+               "unpriv: spill/fill of different pointers stx - sock and ctx 
(write)",
+               .insns = {
+                       BPF_MOV64_REG(BPF_REG_8, BPF_REG_1),
+                       /* struct bpf_sock *sock = bpf_sock_lookup(...); */
+                       BPF_SK_LOOKUP,
+                       BPF_MOV64_REG(BPF_REG_2, BPF_REG_0),
+                       /* u64 foo; */
+                       /* void *target = &foo; */
+                       BPF_ALU64_REG(BPF_MOV, BPF_REG_6, BPF_REG_10),
+                       BPF_ALU64_IMM(BPF_ADD, BPF_REG_6, -8),
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_8),
+                       /* if (skb) *target = skb */
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 1),
+                               BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, 0),
+                       /* else *target = sock */
+                       BPF_JMP_IMM(BPF_JNE, BPF_REG_1, 0, 1),
+                               BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_2, 0),
+                       /* struct bpf_sock *sk = *target; */
+                       BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_6, 0),
+                       /* if (sk) sk->mark = 42; bpf_sk_release(sk); */
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 3),
+                               BPF_MOV64_IMM(BPF_REG_3, 42),
+                               BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_3,
+                                           offsetof(struct bpf_sock, mark)),
+                               BPF_EMIT_CALL(BPF_FUNC_sk_release),
+                       BPF_MOV64_IMM(BPF_REG_0, 0),
+                       BPF_EXIT_INSN(),
+               },
+               .result = REJECT,
+               //.errstr = "same insn cannot be used with different pointers",
+               .errstr = "cannot write into socket",
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+       },
        {
                "unpriv: spill/fill of different pointers ldx",
                .insns = {
@@ -12557,6 +12707,214 @@ static struct bpf_test tests[] = {
                .prog_type = BPF_PROG_TYPE_SCHED_CLS,
                .result = ACCEPT,
        },
+       {
+               "reference tracking: leak potential reference",
+               .insns = {
+                       BPF_SK_LOOKUP,
+                       BPF_MOV64_REG(BPF_REG_6, BPF_REG_0), /* leak reference 
*/
+                       BPF_EXIT_INSN(),
+               },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .errstr = "Unreleased reference",
+               .result = REJECT,
+       },
+       {
+               "reference tracking: leak potential reference on stack",
+               .insns = {
+                       BPF_SK_LOOKUP,
+                       BPF_MOV64_REG(BPF_REG_4, BPF_REG_10),
+                       BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, -8),
+                       BPF_STX_MEM(BPF_DW, BPF_REG_4, BPF_REG_0, 0),
+                       BPF_MOV64_IMM(BPF_REG_0, 0),
+                       BPF_EXIT_INSN(),
+               },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .errstr = "Unreleased reference",
+               .result = REJECT,
+       },
+       {
+               "reference tracking: leak potential reference on stack 2",
+               .insns = {
+                       BPF_SK_LOOKUP,
+                       BPF_MOV64_REG(BPF_REG_4, BPF_REG_10),
+                       BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, -8),
+                       BPF_STX_MEM(BPF_DW, BPF_REG_4, BPF_REG_0, 0),
+                       BPF_MOV64_IMM(BPF_REG_0, 0),
+                       BPF_ST_MEM(BPF_DW, BPF_REG_4, 0, 0),
+                       BPF_EXIT_INSN(),
+               },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .errstr = "Unreleased reference",
+               .result = REJECT,
+       },
+       {
+               "reference tracking: zero potential reference",
+               .insns = {
+                       BPF_SK_LOOKUP,
+                       BPF_MOV64_IMM(BPF_REG_0, 0), /* leak reference */
+                       BPF_EXIT_INSN(),
+               },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .errstr = "Unreleased reference",
+               .result = REJECT,
+       },
+       {
+               "reference tracking: copy and zero potential references",
+               .insns = {
+                       BPF_SK_LOOKUP,
+                       BPF_MOV64_REG(BPF_REG_7, BPF_REG_0),
+                       BPF_MOV64_IMM(BPF_REG_0, 0),
+                       BPF_MOV64_IMM(BPF_REG_7, 0), /* leak reference */
+                       BPF_EXIT_INSN(),
+               },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .errstr = "Unreleased reference",
+               .result = REJECT,
+       },
+       {
+               "reference tracking: release reference without check",
+               .insns = {
+                       BPF_SK_LOOKUP,
+                       /* reference in r0 may be NULL */
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
+                       BPF_MOV64_IMM(BPF_REG_2, 0),
+                       BPF_EMIT_CALL(BPF_FUNC_sk_release),
+                       BPF_EXIT_INSN(),
+               },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .errstr = "type=sock_or_null expected=sock",
+               .result = REJECT,
+       },
+       {
+               "reference tracking: release reference",
+               .insns = {
+                       BPF_SK_LOOKUP,
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 1),
+                       BPF_EMIT_CALL(BPF_FUNC_sk_release),
+                       BPF_EXIT_INSN(),
+               },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .result = ACCEPT,
+       },
+       {
+               "reference tracking: release reference 2",
+               .insns = {
+                       BPF_SK_LOOKUP,
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
+                       BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
+                       BPF_EXIT_INSN(),
+                       BPF_EMIT_CALL(BPF_FUNC_sk_release),
+                       BPF_EXIT_INSN(),
+               },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .result = ACCEPT,
+       },
+       {
+               "reference tracking: release reference twice",
+               .insns = {
+                       BPF_SK_LOOKUP,
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
+                       BPF_MOV64_REG(BPF_REG_6, BPF_REG_0),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 1),
+                       BPF_EMIT_CALL(BPF_FUNC_sk_release),
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
+                       BPF_EMIT_CALL(BPF_FUNC_sk_release),
+                       BPF_EXIT_INSN(),
+               },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .errstr = "type=inv expected=sock",
+               .result = REJECT,
+       },
+       {
+               "reference tracking: release reference twice inside branch",
+               .insns = {
+                       BPF_SK_LOOKUP,
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
+                       BPF_MOV64_REG(BPF_REG_6, BPF_REG_0),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 3), /* goto end */
+                       BPF_EMIT_CALL(BPF_FUNC_sk_release),
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
+                       BPF_EMIT_CALL(BPF_FUNC_sk_release),
+                       BPF_EXIT_INSN(),
+               },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .errstr = "type=inv expected=sock",
+               .result = REJECT,
+       },
+       {
+               "reference tracking: alloc, check, free in one subbranch",
+               .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_MOV64_REG(BPF_REG_0, BPF_REG_2),
+                       BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 16),
+                       /* if (offsetof(skb, mark) > data_len) exit; */
+                       BPF_JMP_REG(BPF_JLE, BPF_REG_0, BPF_REG_3, 1),
+                       BPF_EXIT_INSN(),
+                       BPF_LDX_MEM(BPF_W, BPF_REG_6, BPF_REG_2,
+                                   offsetof(struct __sk_buff, mark)),
+                       BPF_SK_LOOKUP,
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 1), /* mark == 0? */
+                       /* Leak reference in R0 */
+                       BPF_EXIT_INSN(),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2), /* sk NULL? */
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
+                       BPF_EMIT_CALL(BPF_FUNC_sk_release),
+                       BPF_EXIT_INSN(),
+               },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .errstr = "Unreleased reference",
+               .result = REJECT,
+       },
+       {
+               "reference tracking: alloc, check, free in both subbranches",
+               .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_MOV64_REG(BPF_REG_0, BPF_REG_2),
+                       BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 16),
+                       /* if (offsetof(skb, mark) > data_len) exit; */
+                       BPF_JMP_REG(BPF_JLE, BPF_REG_0, BPF_REG_3, 1),
+                       BPF_EXIT_INSN(),
+                       BPF_LDX_MEM(BPF_W, BPF_REG_6, BPF_REG_2,
+                                   offsetof(struct __sk_buff, mark)),
+                       BPF_SK_LOOKUP,
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 4), /* mark == 0? */
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2), /* sk NULL? */
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
+                       BPF_EMIT_CALL(BPF_FUNC_sk_release),
+                       BPF_EXIT_INSN(),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2), /* sk NULL? */
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
+                       BPF_EMIT_CALL(BPF_FUNC_sk_release),
+                       BPF_EXIT_INSN(),
+               },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .result = ACCEPT,
+       },
+       {
+               "reference tracking in call: free reference in subprog",
+               .insns = {
+                       BPF_SK_LOOKUP,
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_0), /* unchecked 
reference */
+                       BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
+                       BPF_MOV64_IMM(BPF_REG_0, 0),
+                       BPF_EXIT_INSN(),
+
+                       /* subprog 1 */
+                       BPF_MOV64_REG(BPF_REG_2, BPF_REG_1),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_2, 0, 1),
+                       BPF_EMIT_CALL(BPF_FUNC_sk_release),
+                       BPF_EXIT_INSN(),
+               },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .result = ACCEPT,
+       },
        {
                "pass modified ctx pointer to helper, 1",
                .insns = {
@@ -12627,6 +12985,407 @@ static struct bpf_test tests[] = {
                .prog_type = BPF_PROG_TYPE_SCHED_CLS,
                .result = ACCEPT,
        },
+       {
+               "reference tracking in call: free reference in subprog and 
outside",
+               .insns = {
+                       BPF_SK_LOOKUP,
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_0), /* unchecked 
reference */
+                       BPF_MOV64_REG(BPF_REG_6, BPF_REG_0),
+                       BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 3),
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
+                       BPF_EMIT_CALL(BPF_FUNC_sk_release),
+                       BPF_EXIT_INSN(),
+
+                       /* subprog 1 */
+                       BPF_MOV64_REG(BPF_REG_2, BPF_REG_1),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_2, 0, 1),
+                       BPF_EMIT_CALL(BPF_FUNC_sk_release),
+                       BPF_EXIT_INSN(),
+               },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .errstr = "type=inv expected=sock",
+               .result = REJECT,
+       },
+       {
+               "reference tracking in call: alloc & leak reference in subprog",
+               .insns = {
+                       BPF_MOV64_REG(BPF_REG_4, BPF_REG_10),
+                       BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, -8),
+                       BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 3),
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
+                       BPF_MOV64_IMM(BPF_REG_0, 0),
+                       BPF_EXIT_INSN(),
+
+                       /* subprog 1 */
+                       BPF_MOV64_REG(BPF_REG_6, BPF_REG_4),
+                       BPF_SK_LOOKUP,
+                       /* spill unchecked sk_ptr into stack of caller */
+                       BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_0, 0),
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
+                       BPF_EXIT_INSN(),
+               },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .errstr = "Unreleased reference",
+               .result = REJECT,
+       },
+       {
+               "reference tracking in call: alloc in subprog, release outside",
+               .insns = {
+                       BPF_MOV64_REG(BPF_REG_4, BPF_REG_10),
+                       BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 4),
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 1),
+                       BPF_EMIT_CALL(BPF_FUNC_sk_release),
+                       BPF_EXIT_INSN(),
+
+                       /* subprog 1 */
+                       BPF_SK_LOOKUP,
+                       BPF_EXIT_INSN(), /* return sk */
+               },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .retval = POINTER_VALUE,
+               .result = ACCEPT,
+       },
+       {
+               "reference tracking in call: sk_ptr leak into caller stack",
+               .insns = {
+                       BPF_MOV64_REG(BPF_REG_4, BPF_REG_10),
+                       BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, -8),
+                       BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
+                       BPF_MOV64_IMM(BPF_REG_0, 0),
+                       BPF_EXIT_INSN(),
+
+                       /* subprog 1 */
+                       BPF_MOV64_REG(BPF_REG_5, BPF_REG_10),
+                       BPF_ALU64_IMM(BPF_ADD, BPF_REG_5, -8),
+                       BPF_STX_MEM(BPF_DW, BPF_REG_5, BPF_REG_4, 0),
+                       BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 5),
+                       /* spill unchecked sk_ptr into stack of caller */
+                       BPF_MOV64_REG(BPF_REG_5, BPF_REG_10),
+                       BPF_ALU64_IMM(BPF_ADD, BPF_REG_5, -8),
+                       BPF_LDX_MEM(BPF_DW, BPF_REG_4, BPF_REG_5, 0),
+                       BPF_STX_MEM(BPF_DW, BPF_REG_4, BPF_REG_0, 0),
+                       BPF_EXIT_INSN(),
+
+                       /* subprog 2 */
+                       BPF_SK_LOOKUP,
+                       BPF_EXIT_INSN(),
+               },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .errstr = "Unreleased reference",
+               .result = REJECT,
+       },
+       {
+               "reference tracking in call: sk_ptr spill into caller stack",
+               .insns = {
+                       BPF_MOV64_REG(BPF_REG_4, BPF_REG_10),
+                       BPF_ALU64_IMM(BPF_ADD, BPF_REG_4, -8),
+                       BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
+                       BPF_MOV64_IMM(BPF_REG_0, 0),
+                       BPF_EXIT_INSN(),
+
+                       /* subprog 1 */
+                       BPF_MOV64_REG(BPF_REG_5, BPF_REG_10),
+                       BPF_ALU64_IMM(BPF_ADD, BPF_REG_5, -8),
+                       BPF_STX_MEM(BPF_DW, BPF_REG_5, BPF_REG_4, 0),
+                       BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 8),
+                       /* spill unchecked sk_ptr into stack of caller */
+                       BPF_MOV64_REG(BPF_REG_5, BPF_REG_10),
+                       BPF_ALU64_IMM(BPF_ADD, BPF_REG_5, -8),
+                       BPF_LDX_MEM(BPF_DW, BPF_REG_4, BPF_REG_5, 0),
+                       BPF_STX_MEM(BPF_DW, BPF_REG_4, BPF_REG_0, 0),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2),
+                       /* now the sk_ptr is verified, free the reference */
+                       BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_4, 0),
+                       BPF_EMIT_CALL(BPF_FUNC_sk_release),
+                       BPF_EXIT_INSN(),
+
+                       /* subprog 2 */
+                       BPF_SK_LOOKUP,
+                       BPF_EXIT_INSN(),
+               },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .result = ACCEPT,
+       },
+       {
+               "reference tracking: allow LD_ABS",
+               .insns = {
+                       BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
+                       BPF_SK_LOOKUP,
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 1),
+                       BPF_EMIT_CALL(BPF_FUNC_sk_release),
+                       BPF_LD_ABS(BPF_B, 0),
+                       BPF_LD_ABS(BPF_H, 0),
+                       BPF_LD_ABS(BPF_W, 0),
+                       BPF_EXIT_INSN(),
+               },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .result = ACCEPT,
+       },
+       {
+               "reference tracking: forbid LD_ABS while holding reference",
+               .insns = {
+                       BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
+                       BPF_SK_LOOKUP,
+                       BPF_LD_ABS(BPF_B, 0),
+                       BPF_LD_ABS(BPF_H, 0),
+                       BPF_LD_ABS(BPF_W, 0),
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 1),
+                       BPF_EMIT_CALL(BPF_FUNC_sk_release),
+                       BPF_EXIT_INSN(),
+               },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .errstr = "BPF_LD_[ABS|IND] cannot be mixed with socket 
references",
+               .result = REJECT,
+       },
+       {
+               "reference tracking: allow LD_IND",
+               .insns = {
+                       BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
+                       BPF_SK_LOOKUP,
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 1),
+                       BPF_EMIT_CALL(BPF_FUNC_sk_release),
+                       BPF_MOV64_IMM(BPF_REG_7, 1),
+                       BPF_LD_IND(BPF_W, BPF_REG_7, -0x200000),
+                       BPF_MOV64_REG(BPF_REG_0, BPF_REG_7),
+                       BPF_EXIT_INSN(),
+               },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .result = ACCEPT,
+               .retval = 1,
+       },
+       {
+               "reference tracking: forbid LD_IND while holding reference",
+               .insns = {
+                       BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
+                       BPF_SK_LOOKUP,
+                       BPF_MOV64_REG(BPF_REG_4, BPF_REG_0),
+                       BPF_MOV64_IMM(BPF_REG_7, 1),
+                       BPF_LD_IND(BPF_W, BPF_REG_7, -0x200000),
+                       BPF_MOV64_REG(BPF_REG_0, BPF_REG_7),
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_4),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 1),
+                       BPF_EMIT_CALL(BPF_FUNC_sk_release),
+                       BPF_EXIT_INSN(),
+               },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .errstr = "BPF_LD_[ABS|IND] cannot be mixed with socket 
references",
+               .result = REJECT,
+       },
+       {
+               "reference tracking: check reference or tail call",
+               .insns = {
+                       BPF_MOV64_REG(BPF_REG_7, BPF_REG_1),
+                       BPF_SK_LOOKUP,
+                       /* if (sk) bpf_sk_release() */
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
+                       BPF_JMP_IMM(BPF_JNE, BPF_REG_1, 0, 7),
+                       /* bpf_tail_call() */
+                       BPF_MOV64_IMM(BPF_REG_3, 2),
+                       BPF_LD_MAP_FD(BPF_REG_2, 0),
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_7),
+                       BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
+                                    BPF_FUNC_tail_call),
+                       BPF_MOV64_IMM(BPF_REG_0, 0),
+                       BPF_EXIT_INSN(),
+                       BPF_EMIT_CALL(BPF_FUNC_sk_release),
+                       BPF_EXIT_INSN(),
+               },
+               .fixup_prog1 = { 17 },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .result = ACCEPT,
+       },
+       {
+               "reference tracking: release reference then tail call",
+               .insns = {
+                       BPF_MOV64_REG(BPF_REG_7, BPF_REG_1),
+                       BPF_SK_LOOKUP,
+                       /* if (sk) bpf_sk_release() */
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 1),
+                       BPF_EMIT_CALL(BPF_FUNC_sk_release),
+                       /* bpf_tail_call() */
+                       BPF_MOV64_IMM(BPF_REG_3, 2),
+                       BPF_LD_MAP_FD(BPF_REG_2, 0),
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_7),
+                       BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
+                                    BPF_FUNC_tail_call),
+                       BPF_MOV64_IMM(BPF_REG_0, 0),
+                       BPF_EXIT_INSN(),
+               },
+               .fixup_prog1 = { 18 },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .result = ACCEPT,
+       },
+       {
+               "reference tracking: leak possible reference over tail call",
+               .insns = {
+                       BPF_MOV64_REG(BPF_REG_7, BPF_REG_1),
+                       /* Look up socket and store in REG_6 */
+                       BPF_SK_LOOKUP,
+                       /* bpf_tail_call() */
+                       BPF_MOV64_REG(BPF_REG_6, BPF_REG_0),
+                       BPF_MOV64_IMM(BPF_REG_3, 2),
+                       BPF_LD_MAP_FD(BPF_REG_2, 0),
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_7),
+                       BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
+                                    BPF_FUNC_tail_call),
+                       BPF_MOV64_IMM(BPF_REG_0, 0),
+                       /* if (sk) bpf_sk_release() */
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 1),
+                       BPF_EMIT_CALL(BPF_FUNC_sk_release),
+                       BPF_EXIT_INSN(),
+               },
+               .fixup_prog1 = { 16 },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .errstr = "tail_call would lead to reference leak",
+               .result = REJECT,
+       },
+       {
+               "reference tracking: leak checked reference over tail call",
+               .insns = {
+                       BPF_MOV64_REG(BPF_REG_7, BPF_REG_1),
+                       /* Look up socket and store in REG_6 */
+                       BPF_SK_LOOKUP,
+                       BPF_MOV64_REG(BPF_REG_6, BPF_REG_0),
+                       /* if (!sk) goto end */
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 7),
+                       /* bpf_tail_call() */
+                       BPF_MOV64_IMM(BPF_REG_3, 0),
+                       BPF_LD_MAP_FD(BPF_REG_2, 0),
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_7),
+                       BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
+                                    BPF_FUNC_tail_call),
+                       BPF_MOV64_IMM(BPF_REG_0, 0),
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
+                       BPF_EMIT_CALL(BPF_FUNC_sk_release),
+                       BPF_EXIT_INSN(),
+               },
+               .fixup_prog1 = { 17 },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .errstr = "tail_call would lead to reference leak",
+               .result = REJECT,
+       },
+       {
+               "reference tracking: mangle and release sock_or_null",
+               .insns = {
+                       BPF_SK_LOOKUP,
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
+                       BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 5),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 1),
+                       BPF_EMIT_CALL(BPF_FUNC_sk_release),
+                       BPF_EXIT_INSN(),
+               },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .errstr = "R1 pointer arithmetic on sock_or_null prohibited",
+               .result = REJECT,
+       },
+       {
+               "reference tracking: mangle and release sock",
+               .insns = {
+                       BPF_SK_LOOKUP,
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2),
+                       BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, 5),
+                       BPF_EMIT_CALL(BPF_FUNC_sk_release),
+                       BPF_EXIT_INSN(),
+               },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .errstr = "R1 pointer arithmetic on sock prohibited",
+               .result = REJECT,
+       },
+       {
+               "reference tracking: access member",
+               .insns = {
+                       BPF_SK_LOOKUP,
+                       BPF_MOV64_REG(BPF_REG_6, BPF_REG_0),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 3),
+                       BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_0, 4),
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
+                       BPF_EMIT_CALL(BPF_FUNC_sk_release),
+                       BPF_EXIT_INSN(),
+               },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .result = ACCEPT,
+       },
+       {
+               "reference tracking: write to member",
+               .insns = {
+                       BPF_SK_LOOKUP,
+                       BPF_MOV64_REG(BPF_REG_6, BPF_REG_0),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 5),
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
+                       BPF_LD_IMM64(BPF_REG_2, 42),
+                       BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_2,
+                                   offsetof(struct bpf_sock, mark)),
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
+                       BPF_EMIT_CALL(BPF_FUNC_sk_release),
+                       BPF_LD_IMM64(BPF_REG_0, 0),
+                       BPF_EXIT_INSN(),
+               },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .errstr = "cannot write into socket",
+               .result = REJECT,
+       },
+       {
+               "reference tracking: invalid 64-bit access of member",
+               .insns = {
+                       BPF_SK_LOOKUP,
+                       BPF_MOV64_REG(BPF_REG_6, BPF_REG_0),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 3),
+                       BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_0, 0),
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
+                       BPF_EMIT_CALL(BPF_FUNC_sk_release),
+                       BPF_EXIT_INSN(),
+               },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .errstr = "invalid bpf_sock access off=0 size=8",
+               .result = REJECT,
+       },
+       {
+               "reference tracking: access after release",
+               .insns = {
+                       BPF_SK_LOOKUP,
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_0),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2),
+                       BPF_EMIT_CALL(BPF_FUNC_sk_release),
+                       BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1, 0),
+                       BPF_EXIT_INSN(),
+               },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .errstr = "!read_ok",
+               .result = REJECT,
+       },
+       {
+               "reference tracking: direct access for lookup",
+               .insns = {
+                       /* Check that the packet is at least 64B long */
+                       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_MOV64_REG(BPF_REG_0, BPF_REG_2),
+                       BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 64),
+                       BPF_JMP_REG(BPF_JGT, BPF_REG_0, BPF_REG_3, 9),
+                       /* sk = sk_lookup_tcp(ctx, skb->data, ...) */
+                       BPF_MOV64_IMM(BPF_REG_3, sizeof(struct bpf_sock_tuple)),
+                       BPF_MOV64_IMM(BPF_REG_4, 0),
+                       BPF_MOV64_IMM(BPF_REG_5, 0),
+                       BPF_EMIT_CALL(BPF_FUNC_sk_lookup_tcp),
+                       BPF_MOV64_REG(BPF_REG_6, BPF_REG_0),
+                       BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 3),
+                       BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_0, 4),
+                       BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
+                       BPF_EMIT_CALL(BPF_FUNC_sk_release),
+                       BPF_EXIT_INSN(),
+               },
+               .prog_type = BPF_PROG_TYPE_SCHED_CLS,
+               .result = ACCEPT,
+       },
 };
 
 static int probe_filter_length(const struct bpf_insn *fp)
-- 
2.17.1

Reply via email to