bpf: Fix register equivalence tracking.
[linux-block.git] / kernel / bpf / verifier.c
index c43a5e8f0818f47ddb91fe4c9b9a779b0c7f712b..39d7f44e7c9226350e76d300bf261caa6dcc2a89 100644 (file)
@@ -1010,14 +1010,9 @@ static const int caller_saved[CALLER_SAVED_REGS] = {
 static void __mark_reg_not_init(const struct bpf_verifier_env *env,
                                struct bpf_reg_state *reg);
 
-/* Mark the unknown part of a register (variable offset or scalar value) as
- * known to have the value @imm.
- */
-static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm)
+/* This helper doesn't clear reg->id */
+static void ___mark_reg_known(struct bpf_reg_state *reg, u64 imm)
 {
-       /* Clear id, off, and union(map_ptr, range) */
-       memset(((u8 *)reg) + sizeof(reg->type), 0,
-              offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type));
        reg->var_off = tnum_const(imm);
        reg->smin_value = (s64)imm;
        reg->smax_value = (s64)imm;
@@ -1030,6 +1025,17 @@ static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm)
        reg->u32_max_value = (u32)imm;
 }
 
+/* Mark the unknown part of a register (variable offset or scalar value) as
+ * known to have the value @imm.
+ */
+static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm)
+{
+       /* Clear id, off, and union(map_ptr, range) */
+       memset(((u8 *)reg) + sizeof(reg->type), 0,
+              offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type));
+       ___mark_reg_known(reg, imm);
+}
+
 static void __mark_reg32_known(struct bpf_reg_state *reg, u64 imm)
 {
        reg->var_off = tnum_const_subreg(reg->var_off, imm);
@@ -7001,14 +7007,18 @@ static void reg_set_min_max(struct bpf_reg_state *true_reg,
                struct bpf_reg_state *reg =
                        opcode == BPF_JEQ ? true_reg : false_reg;
 
-               /* For BPF_JEQ, if this is false we know nothing Jon Snow, but
-                * if it is true we know the value for sure. Likewise for
-                * BPF_JNE.
+               /* JEQ/JNE comparison doesn't change the register equivalence.
+                * r1 = r2;
+                * if (r1 == 42) goto label;
+                * ...
+                * label: // here both r1 and r2 are known to be 42.
+                *
+                * Hence when marking register as known preserve it's ID.
                 */
                if (is_jmp32)
                        __mark_reg32_known(reg, val32);
                else
-                       __mark_reg_known(reg, val);
+                       ___mark_reg_known(reg, val);
                break;
        }
        case BPF_JSET:
@@ -7551,7 +7561,8 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
                                reg_combine_min_max(&other_branch_regs[insn->src_reg],
                                                    &other_branch_regs[insn->dst_reg],
                                                    src_reg, dst_reg, opcode);
-                       if (src_reg->id) {
+                       if (src_reg->id &&
+                           !WARN_ON_ONCE(src_reg->id != other_branch_regs[insn->src_reg].id)) {
                                find_equal_scalars(this_branch, src_reg);
                                find_equal_scalars(other_branch, &other_branch_regs[insn->src_reg]);
                        }
@@ -7563,7 +7574,8 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
                                        opcode, is_jmp32);
        }
 
-       if (dst_reg->type == SCALAR_VALUE && dst_reg->id) {
+       if (dst_reg->type == SCALAR_VALUE && dst_reg->id &&
+           !WARN_ON_ONCE(dst_reg->id != other_branch_regs[insn->dst_reg].id)) {
                find_equal_scalars(this_branch, dst_reg);
                find_equal_scalars(other_branch, &other_branch_regs[insn->dst_reg]);
        }