Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | { /* This is equivalent to the following program: * * r6 = skb->sk; * r7 = sk_fullsock(r6); * r0 = sk_fullsock(r6); * if (r0 == 0) return 0; (a) * if (r0 != r7) return 0; (b) * *r7->type; (c) * return 0; * * It is safe to dereference r7 at point (c), because of (a) and (b). * The test verifies that relation r0 == r7 is propagated from (b) to (c). */ "jne/jeq infer not null, PTR_TO_SOCKET_OR_NULL -> PTR_TO_SOCKET for JNE false branch", .insns = { /* r6 = skb->sk; */ BPF_LDX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, offsetof(struct __sk_buff, sk)), /* if (r6 == 0) return 0; */ BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 8), /* r7 = sk_fullsock(skb); */ BPF_MOV64_REG(BPF_REG_1, BPF_REG_6), BPF_EMIT_CALL(BPF_FUNC_sk_fullsock), BPF_MOV64_REG(BPF_REG_7, BPF_REG_0), /* r0 = sk_fullsock(skb); */ BPF_MOV64_REG(BPF_REG_1, BPF_REG_6), BPF_EMIT_CALL(BPF_FUNC_sk_fullsock), /* if (r0 == null) return 0; */ BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2), /* if (r0 == r7) r0 = *(r7->type); */ BPF_JMP_REG(BPF_JNE, BPF_REG_0, BPF_REG_7, 1), /* Use ! JNE ! */ BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_7, offsetof(struct bpf_sock, type)), /* return 0 */ BPF_MOV64_IMM(BPF_REG_0, 0), BPF_EXIT_INSN(), }, .prog_type = BPF_PROG_TYPE_CGROUP_SKB, .result = ACCEPT, .result_unpriv = REJECT, .errstr_unpriv = "R7 pointer comparison", }, { /* Same as above, but verify that another branch of JNE still * prohibits access to PTR_MAYBE_NULL. */ "jne/jeq infer not null, PTR_TO_SOCKET_OR_NULL unchanged for JNE true branch", .insns = { /* r6 = skb->sk */ BPF_LDX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, offsetof(struct __sk_buff, sk)), /* if (r6 == 0) return 0; */ BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 9), /* r7 = sk_fullsock(skb); */ BPF_MOV64_REG(BPF_REG_1, BPF_REG_6), BPF_EMIT_CALL(BPF_FUNC_sk_fullsock), BPF_MOV64_REG(BPF_REG_7, BPF_REG_0), /* r0 = sk_fullsock(skb); */ BPF_MOV64_REG(BPF_REG_1, BPF_REG_6), BPF_EMIT_CALL(BPF_FUNC_sk_fullsock), /* if (r0 == null) return 0; */ BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 3), /* if (r0 == r7) return 0; */ BPF_JMP_REG(BPF_JNE, BPF_REG_0, BPF_REG_7, 1), /* Use ! JNE ! */ BPF_JMP_IMM(BPF_JA, 0, 0, 1), /* r0 = *(r7->type); */ BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_7, offsetof(struct bpf_sock, type)), /* return 0 */ BPF_MOV64_IMM(BPF_REG_0, 0), BPF_EXIT_INSN(), }, .prog_type = BPF_PROG_TYPE_CGROUP_SKB, .result = REJECT, .errstr = "R7 invalid mem access 'sock_or_null'", .result_unpriv = REJECT, .errstr_unpriv = "R7 pointer comparison", }, { /* Same as a first test, but not null should be inferred for JEQ branch */ "jne/jeq infer not null, PTR_TO_SOCKET_OR_NULL -> PTR_TO_SOCKET for JEQ true branch", .insns = { /* r6 = skb->sk; */ BPF_LDX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, offsetof(struct __sk_buff, sk)), /* if (r6 == null) return 0; */ BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 9), /* r7 = sk_fullsock(skb); */ BPF_MOV64_REG(BPF_REG_1, BPF_REG_6), BPF_EMIT_CALL(BPF_FUNC_sk_fullsock), BPF_MOV64_REG(BPF_REG_7, BPF_REG_0), /* r0 = sk_fullsock(skb); */ BPF_MOV64_REG(BPF_REG_1, BPF_REG_6), BPF_EMIT_CALL(BPF_FUNC_sk_fullsock), /* if (r0 == null) return 0; */ BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 3), /* if (r0 != r7) return 0; */ BPF_JMP_REG(BPF_JEQ, BPF_REG_0, BPF_REG_7, 1), /* Use ! JEQ ! */ BPF_JMP_IMM(BPF_JA, 0, 0, 1), /* r0 = *(r7->type); */ BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_7, offsetof(struct bpf_sock, type)), /* return 0; */ BPF_MOV64_IMM(BPF_REG_0, 0), BPF_EXIT_INSN(), }, .prog_type = BPF_PROG_TYPE_CGROUP_SKB, .result = ACCEPT, .result_unpriv = REJECT, .errstr_unpriv = "R7 pointer comparison", }, { /* Same as above, but verify that another branch of JNE still * prohibits access to PTR_MAYBE_NULL. */ "jne/jeq infer not null, PTR_TO_SOCKET_OR_NULL unchanged for JEQ false branch", .insns = { /* r6 = skb->sk; */ BPF_LDX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, offsetof(struct __sk_buff, sk)), /* if (r6 == null) return 0; */ BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 8), /* r7 = sk_fullsock(skb); */ BPF_MOV64_REG(BPF_REG_1, BPF_REG_6), BPF_EMIT_CALL(BPF_FUNC_sk_fullsock), BPF_MOV64_REG(BPF_REG_7, BPF_REG_0), /* r0 = sk_fullsock(skb); */ BPF_MOV64_REG(BPF_REG_1, BPF_REG_6), BPF_EMIT_CALL(BPF_FUNC_sk_fullsock), /* if (r0 == null) return 0; */ BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2), /* if (r0 != r7) r0 = *(r7->type); */ BPF_JMP_REG(BPF_JEQ, BPF_REG_0, BPF_REG_7, 1), /* Use ! JEQ ! */ BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_7, offsetof(struct bpf_sock, type)), /* return 0; */ BPF_MOV64_IMM(BPF_REG_0, 0), BPF_EXIT_INSN(), }, .prog_type = BPF_PROG_TYPE_CGROUP_SKB, .result = REJECT, .errstr = "R7 invalid mem access 'sock_or_null'", .result_unpriv = REJECT, .errstr_unpriv = "R7 pointer comparison", }, { /* Maps are treated in a different branch of `mark_ptr_not_null_reg`, * so separate test for maps case. */ "jne/jeq infer not null, PTR_TO_MAP_VALUE_OR_NULL -> PTR_TO_MAP_VALUE", .insns = { /* r9 = &some stack to use as key */ BPF_ST_MEM(BPF_W, BPF_REG_10, -8, 0), BPF_MOV64_REG(BPF_REG_9, BPF_REG_10), BPF_ALU64_IMM(BPF_ADD, BPF_REG_9, -8), /* r8 = process local map */ BPF_LD_MAP_FD(BPF_REG_8, 0), /* r6 = map_lookup_elem(r8, r9); */ BPF_MOV64_REG(BPF_REG_1, BPF_REG_8), BPF_MOV64_REG(BPF_REG_2, BPF_REG_9), BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem), BPF_MOV64_REG(BPF_REG_6, BPF_REG_0), /* r7 = map_lookup_elem(r8, r9); */ BPF_MOV64_REG(BPF_REG_1, BPF_REG_8), BPF_MOV64_REG(BPF_REG_2, BPF_REG_9), BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem), BPF_MOV64_REG(BPF_REG_7, BPF_REG_0), /* if (r6 == 0) return 0; */ BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 2), /* if (r6 != r7) return 0; */ BPF_JMP_REG(BPF_JNE, BPF_REG_6, BPF_REG_7, 1), /* read *r7; */ BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_7, offsetof(struct bpf_xdp_sock, queue_id)), /* return 0; */ BPF_MOV64_IMM(BPF_REG_0, 0), BPF_EXIT_INSN(), }, .fixup_map_xskmap = { 3 }, .prog_type = BPF_PROG_TYPE_XDP, .result = ACCEPT, }, |